Skip to content
Advertisement

org.postgresql.util.PSQLException: ERROR: syntax error near «,» in Java

The below is the query generate by a prepareStatement in Java:

JavaScript

The Java code is:

JavaScript

The query is executed int the try statement and insert the values properly in the DB, BUT it also throws the below exception, at line 192: here ‘val’:

JavaScript

The error trace relate to postgres is here:

JavaScript

By the way, the table has a bigserial value and all the others values showed in the query. Thanks in advance!

Advertisement

Answer

If the query contains string constant within the values clause, as you have shown in the question:

JavaScript

then this part of code will work fine:

JavaScript

however this part of code won’t work:

JavaScript

It will throw an PSQLException: The column index is out of range: X, number of columns: 0, because PreparedStatement.setXXX methods expect placeholders ? in the SQL statement.
On the other hand, when the insert statement contains placeholders (I assume that your INSERT does contain placeholders, because you haven’t got the above exception):

JavaScript

then pstmt.setString... statements will work fine, however this statement:

JavaScript

will throw an exception: PSQLException: ERROR: syntax error near ','
If your intent is to execute the INSERT twice, the first one using placeholders, and the second one using string values, you must do it in this way:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement