Batch operation
In this chapter you will learn:
Do batch operation
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Arrays;
/* j a v a 2 s . c om*/
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
Statement st = conn.createStatement();
st.executeUpdate("create table survey (id int,name varchar(30));");
st.close();
st = conn.createStatement();
st.addBatch("insert into survey (id,name ) values (1,'nameValue')");
st.addBatch("insert into survey (id,name ) values (2,null)");
st.addBatch("insert into survey (id,name ) values (3,'Tom')");
int count[] = st.executeBatch();
System.out.println(Arrays.toString(count));
st.close();
conn.close();
}
private static Connection getConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
String url = "jdbc:hsqldb:mem:data/tutorial";
return DriverManager.getConnection(url, "sa", "");
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Statements, ResultSet, Exception, Warning