Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.sql.SQLException: ORA-00928: missing SELECT keyword. when inserting record to DB using JDBC

I get an error when I try to insert some rows to a db. so here is the code

try {
    String insertStmt = "INSERT into " +
                        "MY_TABLE('RECORD_TYPE', 'FILE_TYPE', 'DATE', 'BATCH_NO', 'RECORD_COUNT')" +
                        "VALUES(?, ?, ?, ?, ?);";

    PreparedStatement pstmt = super.con.prepareStatement(insertStmt);

    pstmt.setString(1, input[0]);
    pstmt.setString(2, input[1]);
    pstmt.setString(3, input[2]);
    pstmt.setString(4, input[3]);
    pstmt.setString(5, input[4]);

    System.out.println("Insert rows : " + pstmt.executeUpdate());

} catch (SQLException sqle) {
    System.out.println(sqle.getMessage());
    sqle.printStackTrace();
} catch (Exception e) {
    System.out.println(e.getMessage());
    e.printStackTrace();
} finally {
    con.close();
}

and everything on the db is of varchar type, double checked the columns (they all are the same name), took out the quotes off the column name (same result) no success. to add it up, the error message is not very helpful.

any suggestions would be appreciated.

like image 629
chip Avatar asked Nov 25 '11 04:11

chip


2 Answers

You need to change the SQL statement. (Never use reserved words as identifiers)

String insertStmt = "INSERT into \"MY_TABLE\" (RECORD_TYPE,FILE_TYPE, 
              \"DATE\",BATCH_NO,RECORD_COUNT) VALUES (?, ?, ?, ?, ?)";

Use " (double quotes) to escape the reserved words/keywords.

like image 67
KV Prajapati Avatar answered Oct 26 '22 01:10

KV Prajapati


I can spot two problems:

  1. No need for single quotes around column names. But you may wrap it in double quotes. It is necessary if you are using reserved keywords for column names or table names. Here DATE.
  2. You need a space before VALUES.

So you need to change insertStmt to somthing like this:

String insertStmt = "INSERT into " +
    "MY_TABLE(RECORD_TYPE, FILE_TYPE, \"DATE\", BATCH_NO, RECORD_COUNT) " +
    "VALUES(?, ?, ?, ?, ?);";
like image 4
Jomoos Avatar answered Oct 25 '22 23:10

Jomoos