H2: executeBatch doesn't work

960 views Asked by At

I have the following code

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

The code executed. No errors no exceptions. However table student is empty. I use h2 1.3.176 in embedded mode. What is wrong?

1

There are 1 answers

0
fmgp On BEST ANSWER

You forgot to add batch set of parameters:

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