这是下面我要执行批处理PreparedStatement的代码。没有批处理,这些语句运行良好,但我无法批处理 使用“?”插入参数。
出现以下错误:
java.sql.BatchUpdateException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?,?,?,?)' at line 1
同样,我也无法使用Statement批处理插入。
public void insertValuesInTotalResponseWithPSwithBatch(List<A> details) throws SQLException {
Connection conn;
int batch_count = 0;
conn = MySqlConnectionUtility.getConnection();
PreparedStatement ps = null;
conn.setAutoCommit(false);
for (A record : details) {
if (!record.getLon().trim().equals("bac")) {
System.out.println("the record is " + record);
String query = "INSERT INTO AwithbatchwitPS " + " VALUES (?,?,?,?,?,?)";
ps = conn.prepareStatement(query);
ps.setInt(1, Integer.parseInt(record.getOne()));
ps.setFloat(2, Float.parseFloat(record.getTwo()));
ps.setFloat(3, Float.parseFloat(record.getThree()));
ps.setString(4, record.getDayofyear());
ps.setDouble(5, Double.parseDouble(record.getFive()));
ps.setInt(6, Integer.parseInt(record.getCount()));
System.out.println(ps);
ps.addBatch(query);
System.out.println("Btach1 coutn1 is " + batch_count);
batch_count++;
System.out.println("Btach2 coutn2 is " + batch_count);
if (batch_count % 200 == 0) {
try {
ps.executeBatch();
} catch (Exception e) {
System.out.println("Btach coutn is " + batch_count);
// e.printStackTrace();
} finally {
conn.commit();
ps.clearBatch();
}
batch_count = 0;
}
}
details_size = details.size();
}
if (batch_count != 0) {
try {
ps.executeBatch();
conn.commit();
} catch (BatchUpdateException e) {
// e.printStackTrace();
}
batch_count = 0;
}
}