马士兵老师犯了个错误,不能使用batch这里。这是我在MySQL上得到的结论。不知对否。 import java.sql.*; public class TestTransaction { public static void main(String[] args) { Connection conn = null; Statement pstmt = null; try { conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/student", "root", "5822323"); conn.setAutoCommit(false); pstmt=conn.createStatement(); pstmt.executeUpdate("insert into users values('0022','zhaokai')"); pstmt.executeUpdate("insert into users values('0020','zhaokai')"); pstmt.executeUpdate("insert into users values('0021','zhaokai')"); conn.commit(); } catch (SQLException e) { e.printStackTrace(); try{ conn.rollback(); }catch (SQLException e1) { e1.printStackTrace(); } } finally { try { if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }