2013-09-29 41 views
0

下面是查詢產生由prepareStatement在Java中:org.postgresql.util.PSQLException:錯誤:語法錯誤附近«»在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' ) 

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

該查詢INT 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 

的錯誤跟蹤涉及到的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值和所有其它的值在查詢中顯示。提前致謝!

+2

發佈您的Java代碼和堆棧跟蹤。 –

+0

並使用query.setString作爲參數。不要把它們放到SQL語句中。 – Thilo

+0

是的,我使用它查詢是preparedStatement的最終結果 –

回答

2

如果查詢包含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: The column index is out of range: X, number of columns: 0,b因爲PreparedStatement.setXXX方法期望佔位符?在SQL語句中。
在另一方面,當插入語句包含佔位符(我假設你的INSERT 確實含有佔位符,因爲你沒有得到上面的除外):

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

然後pstmt.setString...語句將正常工作,然而,這樣的說法:

conexion.createStatement().execute(query); 

將拋出一個異常:PSQLException: ERROR: syntax error near ','
如果你的目的是要兩次執行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); 
+0

謝謝!我沒有意識到2個執行語句,我只需要1個插入,所以我只是刪除:\t conexion.createStatement()。execute(query); –