【发布时间】:2018-06-06 17:25:00
【问题描述】:
我正在尝试在我的数据库中执行大量 INSERT 命令。
try{
int Records[];
Statement title = write.getConnection().createStatement();
Title titleFilter = application.Title.getTitle();
ResultSet rs = titleFilter.getTitleData();
while(rs.next()){;
String add = ("INSERT INTO title VALUES ("
+ "'" + rs.getInt(1) + "'"+","
+ "'" +rs.getString(2)+ "'" +","
+ "'" +rs.getString(3) + "'"+","
+ "'" +rs.getInt(4)+ "'" +","
+ "'" +rs.getInt(5)+ "'" +","
+ "'" +rs.getInt(6) + "'"+","
+ "'" +rs.getString(7)+ "'" +","
+ "'" +rs.getInt(8) + "'"+","
+"'" + rs.getInt(9)+ "'" +","
+ "'" +rs.getInt(10)+ "'" +","
+ "'" +rs.getString(11)+ "'" +","
+"'" + rs.getString(12) + "'"+")"
);
title.addBatch(add);
System.out.println(add);
title.executeBatch();
}
我知道在添加表达式后立即执行批处理有点愚蠢。我改变它以发现我的错误。
每次我尝试运行程序时,此代码部分只插入六个表达式。我改变了很多东西来找到我的错误,但我想我永远找不到。此外,我得到了这个异常
org.postgresql.util.PSQLException: ERROR: syntax error at or near ")"
Position: 48
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2310)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2023)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:217)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:421)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:318)....
【问题讨论】:
-
哦,谢谢。。这里也是从 IDE 复制过来的,我的代码里没有
-
你应该在这里使用
PreparedStatement。 -
使用准备好的语句,只需在while循环中设置参数。
-
为什么你的整数值用引号括起来?
"'" + rs.getInt(#) + "'" -
在循环中使用
addBatch+executeBatch有什么意义?您不妨致电executeUpdate。如果要进行批处理,请将executeBatch移到循环之外。 --- 看在上帝的份上,使用PreparedStatement和参数标记,以防止SQL Injection 攻击和失败的SQL 语句。