简体   繁体   English

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

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

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 语句中执行并将值正确插入数据库中,但它还在第 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.顺便说一句,该表有一个 bigserial 值,所有其他值都显示在查询中。 Thanks in advance!提前致谢!

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 ?它将抛出PSQLException: The column index is out of range: X, number of columns: 0 ,因为PreparedStatement.setXXX方法需要占位符? in the SQL statement.在 SQL 语句中。
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):另一方面,当插入语句包含占位符时(我假设您的 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 ','将抛出异常: 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:如果您的意图是执行两次 INSERT,第一次使用占位符,第二次使用字符串值,您必须这样做:

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

String sql="INSERT INTO travel values(Source,Destination,VehicleType,PhoneNo)" +" VALUES ('Mysore','Chennai','l',94400000)";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.postgresql.util.PSQLException:错误:“)”处或附近的语法错误 - org.postgresql.util.PSQLException: ERROR: syntax error at or near “)” org.postgresql.util.PSQLException:错误:“-”处或附近的语法错误 - org.postgresql.util.PSQLException: ERROR: syntax error at or near “-” org.postgresql.util.PSQLException:错误:“(”或附近的语法错误 - org.postgresql.util.PSQLException: ERROR: syntax error at or near “(” 引起:org.postgresql.util.PSQLException:错误:“:”处或附近的语法错误 - Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near ":" org.postgresql.util.PSQLException:错误:在“with”处或附近出现语法错误 - org.postgresql.util.PSQLException: ERROR: syntax error at or near "with" org.postgresql.util.PSQLException:错误:“:”处或附近的语法错误 - org.postgresql.util.PSQLException: ERROR: syntax error at or near “:” org.postgresql.util.PSQLException:错误:语法错误在“$ 1”或附近 - org.postgresql.util.PSQLException: ERROR: syntax error at or near “$1” org.postgresql.util.PSQLException:错误:“ 7”或附近的语法错误 - org.postgresql.util.PSQLException: ERROR: syntax error at or near “7” Java 中的错误:org.postgresql.util.PSQLException:错误:语法错误附近; - Error in Java: org.postgresql.util.PSQLException: ERROR: syntax error near ; date_trunc org.postgresql.util.PSQLException:错误:“ $ 1”或附近的语法错误 - date_trunc org.postgresql.util.PSQLException: ERROR: syntax error at or near “$1”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM