8

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.

Doug Porter
  • 7,721
  • 4
  • 40
  • 55
chip
  • 3,039
  • 5
  • 35
  • 59
  • 3
    Try without the quotes (should be double quotes if any), without the trailing semicolon, and with a space before `values`. – Thilo Nov 25 '11 at 04:14
  • 4
    There's no space between RECORD_COUNT) and VALUES( .. so your insert command looks like 'INSERT into MY_TABLE(......)VALUES(????)` – Kal Nov 25 '11 at 04:20
  • 3
    I think you'll always need to use the double quotes, since DATE is normally an invalid identifier. (Which is why you shouldn't use it as a column name. And plus, why is it stored as a string?) – Jon Heller Nov 25 '11 at 04:21
  • 4
    You actually don't need a space before `values`, although it normally looks better that way. – Jon Heller Nov 25 '11 at 04:23
  • 2
    Remove the semicolon before the closing double quotes of the query. – One-One Nov 25 '11 at 04:30
  • hey people, thanks for all your suggestions. that space with the values keyword caused the problem. thanks again! :D – chip Nov 25 '11 at 05:23

6 Answers6

5

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.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • hey man, I was about to answer that here. yep, the date column need to be surrounded by double quotes and then escaped. thanks for the answer :D :D :D – chip Nov 25 '11 at 05:31
4

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(?, ?, ?, ?, ?);";
Jomoos
  • 12,823
  • 10
  • 55
  • 92
2

Print insertStmt String in Console and try to fire it in directly backend. It gives you exact error in backend. It seens some spacing or syntax error.

Jwalin Shah
  • 2,451
  • 1
  • 17
  • 22
  • 1
    yep, i tried inserting a record from the db ide and knew that there were some syntax errors. thanks for the answer jwalin. appreciate it – chip Nov 25 '11 at 05:24
1

I just came to this page while searching for ORA-00928, and I'd like to note that my problem was an extra comma at the start of the column list:

INSERT INTO redacted.redacted
  (
  , redacted_id   -- The comma at the start of this line will trigger ORA-00928.
  , another_redacted_id
  , redacted1
  , redacted2
  , redacted3
  , created_at
  , created_by
  , changed_at
  , changed_by
  )
  VALUES
  (?, ?, ?, ?, ?, ?, ?, ?, ?)
Ed Brannin
  • 7,691
  • 2
  • 28
  • 32
0

For others searching for the same error: Other syntactical issues with the query can cause the same exception to be thrown. For example, omitting the word VALUES.

Dean
  • 301
  • 4
  • 11
0

I was running the same issue, and in my case the query was like this:

insert into Address (number, street, id) values (?, ?, ?)

The problem was caused by the number column name since number is a reserved keyword in Oracle, and the exception was sort of misleading ORA-00928: missing SELECT keyword.

After escaping the number column, the statement was executed normally:

insert into Address ("number", street, id) values (?, ?, ?)
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911