June 16, 2014

java.sql.SQLException: Protocol violation

Exception in thread "main" java.sql.SQLException: Protocol violation: [1]
                at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:502)
                at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:205)
                at oracle.jdbc.driver.T4C7Ocommoncall.doOLOGOFF(T4C7Ocommoncall.java:64)
                at oracle.jdbc.driver.T4CConnection.logoff(T4CConnection.java:576)
                at oracle.jdbc.driver.PhysicalConnection.close(PhysicalConnection.java:5222)
               at XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Connection con=createConnection();
Con.close();
Con.commit();
Con.close();

Here, commit() is invoked after the connection is closed, hence the above exception occurred.

java.lang.ArrayIndexOutOfBoundsException while executing batch Statements in Java

I have created a PreparedStatement to execute a batch of insert statements. But while executing,  the below exception occurred.


java.lang.ArrayIndexOutOfBoundsException: 22 at oracle.jdbc.driver.T4CNumberAccessor.unmarshalOneRow(T4CNumberAccessor.java:207) at oracle.jdbc.driver.T4C8Oall.readRXD(T4C8Oall.java:745) at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:371) at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:205) at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:548) at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:217) at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:1115) at oracle.jdbc.driver.OraclePreparedStatement.executeForRowsWithTimeout(OraclePreparedStatement.java:13106) at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:13246) at oracle.jdbc.driver.OracleStatementWrapper.executeBatch(OracleStatementWrapper.java:248) at XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 



Solution:

After googling, I realized that the PreparedStatement is not accepting more than 6 parameters. Below is the statement I have used.
PreparedStatement stmtQuestions = conDB.prepareStatement("INSERT INTO "+Properties.DB_TABLE +" values(?,?,?,?,?,?,?)");
         
I changed the statement to below which avoid the above exception and program was executed successfully.
Statement stmtQuestions=conDB.createStatement();
String query="INSERT INTO "+Properties.DB_TABLE +" values('"+id+"',"+sid+","+qid+",'"+sqText+"','"+sqSText+"','"+qType+"','"+qoType+"')";
stmtQuestions.addBatch(query);

May 14, 2014

Using excel file as DB and read data from Java

We can use an excel file as a data base. Using java we can create connection to excel file and execute sql query as we do with database. Use the below code to use excel file as a DB and read data from it using java.

String stExcelFile=”c:\\temp\\test.xls”;
String stSheetName=”Sheet1”;
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection conExcel =
            DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=" +
                                        stExcelFile +
                                        ";DriverID=22;READONLY=false");
        Statement stmtExcel = conExcel.createStatement();
        ResultSet rsExcel =stmtExcel.executeQuery("Select * from [" + stSheetName + "$]");
while (rsExcel.next()) {
System.out.println(“id: ”+rsExcel.getString(“id”)));
System.out.println(“name: ”+rsExcel.getString(“name”)));
}
rsExcel.close();
stmtExcel.close();
conExcel.close();