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:

insert into schema.table(cedula, actividad, mercado, venta_mensual, fortalezas, crecer,
 financiamiento, monto, patente, contador, regimen_tri, problemas, bn_servicios, cursos ) 
values ('val', 'GAM', 'GAM', '0', 'Calidad', 'Sí', 'Sí', '122', 'Sí', 'Sí', 'ddd', 'aaa','ccc', 'bbb'  )

The Java code is:

try {
    PreparedStatement pstmt = conexion.prepareStatement(query); 
    pstmt.setString(1, n.getCedula()); 
        //the rest of the sets of the statement continue here from 1 to 13
        pstmt.executeUpdate(); 
    conexion.createStatement().execute(query);
        return true
} catch (SQLException e) {
    e.printStackTrace(); // This error 
    return false;
}

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’:

 org.postgresql.util.PSQLException: ERROR: error de sintaxis en o cerca de «,»
 org.postgresql.util.PSQLException: ERROR: syntax error near ',' java

The error trace relate to postgres is here:

at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:374)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:366)

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:

query = "insert into table(cedula, actividad, mercado) "
        + " values ('val', 'GAM', 'GAM' )";

then this part of code will work fine:

conexion.createStatement().execute(query);

however this part of code won’t work:

pstmt.setString(1, n.getCedula()); 
//the rest of the sets of the statement continue here from 1 to 13

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):

query = "insert into tabla(cedula, actividad, mercado) "
    + " values ( ?, ?, ? )";

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

   conexion.createStatement().execute(query);

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:

query1 = "insert into tabla(cedula, actividad, mercado) "
        + " values ('val', 'GAM', 'GAM' )";
query2 = "insert into tabla(cedula, actividad, mercado) "
        + " values ( ? , ? , ? )";

PreparedStatement pstmt = conexion.prepareStatement(query2); 
pstmt.setString(1, n.getCedula()); 
  //the rest of the sets of the statement continue here from 1 to 13
pstmt.executeUpdate(); 

conexion.createStatement().execute(query1);
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement