Statement批量处理和事务代码如下:
package com.ambow.day20.jdbc.JDBCTestCommitAndRollback;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import com.ambow.day19.jdbc.util.JDBCConAndClo;
/*
*1,首先把Auto commit设置为false,不让它自动提交
*2,进行手动提交(commit)
*3,提交完成后回复现场将Auto commit,还原为true,
*4,当异常发生执行catch中SQLException时,记得要rollback(回滚);
* */
public class StatementCommitAndRollbackTest {
public static void main(String args[]) {
Connection con = null;
Statement stm = null;
try {
con = JDBCConAndClo.getConnectionBao();
stm = con.createStatement();
con.setAutoCommit(false);
// 若不出现异常,则继续执行到try语句完,否则跳转到catch语句中
stm.addBatch("insert into student values(23,'tangbao','高数',