java - org.postgresql.util.PSQLException : ERROR: syntax error near «, » 在 Java 中

标签 java postgresql jdbc

下面是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'  )

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

查询在 try 语句中执行,并将值正确插入到数据库中,但它也会在第 192 行抛出以下异常:here 'val':

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

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

顺便说一下,该表有一个 bigserial 值,所有其他值都显示在查询中。提前致谢!

最佳答案

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

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

那么这部分代码就可以正常工作了:

conexion.createStatement().execute(query);

但是这部分代码将不起作用:

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

它将抛出PSQLException:列索引超出范围:X,列数:0,因为PreparedStatement.setXXX方法需要占位符 在 SQL 语句中。
另一方面,当插入语句包含占位符时(我假设您的 INSERT 确实包含占位符,因为您没有遇到上述异常):

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

then pstmt.setString... 语句可以正常工作,但是这个语句:

   conexion.createStatement().execute(query);

将抛出异常:PSQLException: ERROR: ',' 附近的语法错误
如果您的意图是执行 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);

关于java - org.postgresql.util.PSQLException : ERROR: syntax error near «, » 在 Java 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19073542/

相关文章:

jdbc - 如何在Gradle项目中使用Oracle JDBC驱动程序

java - OSGi - 如何使用 FUSE ESB 添加 JVM 参数?

java - 类包不包含 src.main.java,但它存在并且 eclipse 提示它

java - 如何在源代码中引用预定义的数据库条目

java - JPA 和 PostgreSQL : How do I call a stored procedure with void return type?

java - Spring Data JDBC 跳过锁定

java - 如何让 postgres JDBC 在 Scala Mac OS 中工作?

java - 持续时间或间隔中的 Joda Time 分钟数

ruby-on-rails - 使用 gzip 压缩将图像数据编码为 UTF-8 字符串

sql - 为什么 postgresql 不能执行其 'where' 子句具有键匹配谓词的 select 语句?