在之前所使用的全部的数据库操作严格来讲是属于JDBC1.0中就规定的操作模式,而最新的JDBC是4.0版本,但是没人去使用,从JDBC2.0开始增加了一些神奇功能:可滚动的结果集、可以利用结果集执行增加、更新、删除操作、批处理操作。
public class TestDemo {
private static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String DBURL = "jdbc:oracle:thin:@localhost:1521:orcl";
private static final String USER = "scott";
private static final String PASSWORD = "tiger";
public static void main(String[] args) throws Exception {
Class.forName(DBDRIVER);
Connection conn = DriverManager.getConnection(DBURL, USER, PASSWORD);
Statement stmt = conn.createStatement();
stmt.addBatch("INSERT INTO member(mid,name) VALUES(myseq.nextval,'测试A')");
stmt.addBatch("INSERT INTO member(mid,name) VALUES(myseq.nextval,'测试B')");
stmt.addBatch("INSERT INTO member(mid,name) VALUES(myseq.nextval,'测试C')");
stmt.addBatch("INSERT INTO member(mid,name) VALUES(myseq.nextval,'测试D')");
stmt.addBatch("INSERT INTO member(mid,name) VALUES(myseq.nextval,'测试E')");
stmt.addBatch("INSERT INTO member(mid,name) VALUES(myseq.nextval,'测试F')");
int[] result = stmt.executeBatch(); // 执行批处理
System.out.println(Arrays.toString(result));
conn.close();
}
}
在批处理操作的过程之中,由于JDBC具备有自动的事务提交,所以一旦中间的语句出现了错误,那么结果就是错误前的语句正常执行,错误后的语句就不执行了,很明显这不应该。
public class TestDemo {
private static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String DBURL = "jdbc:oracle:thin:@localhost:1521:orcl";
private static final String USER = "scott";
private static final String PASSWORD = "tiger";
public static void main(String[] args) throws Exception {
Class.forName(DBDRIVER);
Connection conn = DriverManager.getConnection(DBURL, USER, PASSWORD);
Statement stmt = conn.createStatement();
conn.setAutoCommit(false);
try {
stmt.addBatch("INSERT INTO member(mid,name) VALUES(myseq.nextval,'测试A')");
stmt.addBatch("INSERT INTO member(mid,name) VALUES(myseq.nextval,'测试B')");
stmt.addBatch("INSERT INTO member(mid,name) VALUES(myseq.nextval,'测试C')");
stmt.addBatch("INSERT INTO member(mid,name) VALUES(myseq.nextval,'测试D')");
stmt.addBatch("INSERT INTO member(mid,name) VALUES(myseq.nextval,'测试E')");
stmt.addBatch("INSERT INTO member(mid,name) VALUES(myseq.nextval,'测试F')");
int[] result = stmt.executeBatch(); // 执行批处理
System.out.println(Arrays.toString(result));
conn.commit(); // 如果没有错误进行提交
} catch (Exception e) {
e.printStackTrace();
conn.rollback(); // 如果出现异常则进行回滚
}
conn.close();
}
}