import java.sql.*;
public class TransactionSavepoint {
public static void main(String[] args) throws SQLException {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
Savepoint sp1 = null; try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/hello";
conn = DriverManager.getConnection(url, "root", "admin"); conn.setAutoCommit(false); stmt = conn.createStatement();
stmt.addBatch("insert into student values(1,'Mike')"); sp1 = conn.setSavepoint(); stmt.addBatch("insert into student values(2,'Rose')"); stmt.addBatch("insert into  student values(3,'Jack')"); stmt.executeBatch();
conn.commit();
rs = stmt.executeQuery("select from student");

while(rs.next()){
if (rs.getInt(1) > 2) {
throw new Exception("ID 已经超过3了");
}

}
} catch (Exception e) {
e.printStackTrace(); conn.rollback(sp1);

}
rs.close();
stmt.close();
conn.close();
}
}