H2:executeBatch不起作用

时间:2015-06-10 15:10:02

标签: java h2

我有以下代码

String query = "INSERT INTO student (age,name) VALUES (?,?)";
conn.setAutoCommit(true);  
ps = conn.prepareStatement(query);            
for (Student student:list) {
     ps.setInt(1, student.getAge());
     ps.setString(2, student.getName());
}
int[]temp=ps.executeBatch();
System.out.println("TEMP:"+temp.length);//returns 0

执行的代码。没有错误没有例外。但表学生是空的。我在嵌入模式下使用h2 1.3.176。有什么问题?

1 个答案:

答案 0 :(得分:3)

您忘记添加一组批参数:

String query = "INSERT INTO student (age,name) VALUES (?,?)";
conn.setAutoCommit(true);  
ps = conn.prepareStatement(query);            
for (Student student:list) {
    ps.setInt(1, student.getAge());
    ps.setString(2, student.getName());
    ps.addBatch(); // <--
}
int[]temp=ps.executeBatch();
System.out.println("TEMP:"+temp.length);