org.postgresql.util。PSQLException:错误:在Java中,«,»的语法错误。

时间:2022-05-21 02:00:39

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

下面是由Java的prepareStatement生成的查询:

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:

Java代码是:

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

查询在try语句中执行,并在DB中正确插入值,但它也会抛出以下异常,在第192行:这里是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:

错误跟踪与postgres有关:

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!

顺便说一下,这个表有一个bigserievalue,以及查询中显示的所有其他值。提前谢谢!

1 个解决方案

#1


2  

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

如果查询包含values子句中的字符串常量,如您在问题中所示:

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

它将抛出一个PSQLException:列索引超出范围:X,列数:0,因为PreparedStatement。setXXX方法期望占位符?在SQL语句。另一方面,当insert语句包含占位符时(我假设您的insert确实包含占位符,因为您没有得到上述异常):

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

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

然后pstmt.setString…声明将会很好,但这句话:

   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:

将抛出一个异常:PSQLException: ERROR:语法错误,如果您的意图是执行插入两次,第一个使用占位符,第二个使用字符串值,那么您必须这样做:

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

#1


2  

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

如果查询包含values子句中的字符串常量,如您在问题中所示:

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

它将抛出一个PSQLException:列索引超出范围:X,列数:0,因为PreparedStatement。setXXX方法期望占位符?在SQL语句。另一方面,当insert语句包含占位符时(我假设您的insert确实包含占位符,因为您没有得到上述异常):

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

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

然后pstmt.setString…声明将会很好,但这句话:

   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:

将抛出一个异常:PSQLException: ERROR:语法错误,如果您的意图是执行插入两次,第一个使用占位符,第二个使用字符串值,那么您必须这样做:

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