Andrew Mairose
Andrew Mairose

Reputation: 10995

DB2 Syntax Error in Java, Works Fine in SQL editor

I have the following java code to create a prepared statement from the passed in connection. This is only a portion of the code and I've changed the name of the table for anonymity.

private static final String preparedStatementSQL = "INSERT INTO TABLE (STORE_ID,"
        + "BRAND_NAME,BUSINESS_DATE,HOUR,FCST_SYSSLS_AMT,FCST_USERSLS_AMT,FCST_SYSTRN_CNT,"
        + "FCST_USERTRN_CNT,ACTION_TIMESTAMP) VALUES (?,?,?,?,?,?,?,?,(current timestamp))";

private PreparedStatement ps = null;

public TABLE(Connection con) throws SQLException{
    this.ps = con.prepareStatement(preparedStatementSQL);
}

When it runs the following line:

this.ps = con.prepareStatement(preparedStatementSQL);

I get the following SQL error:

java.sql.SQLSyntaxErrorException: Syntax error: Encountered "HOUR" at line 1, column 67.

I copied the statement into my SQL editor (SQuirreL) and put in some made up values, and it worked fine (no syntax error!).

It is an IBM DB2 database.

I tried deleting the column names to just do:

INSERT INTO TABLE VALUES (?,?,?,?,?,?,?,?,(current timestamp))

And it fixed the problem, but I have to have it use the (COLUMNS) VALUES (?... ) format.

Anyone have any ideas?

Upvotes: 0

Views: 792

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269973

HOUR is a reserved word in DB2. The list is here.

You should put double quotes (") around it in the column list.

TABLE is also a reserved word. I assume that you have a real table name there, though.

Upvotes: 1

Related Questions