package com.chu.two;
import java.sql.*;
public class TestTansaction {
public static void main(String[] args) throws SQLException {
Connection con = null; //放在外面防止关不掉
PreparedStatement pst = null;
ResultSet re = null;
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/school", "root", "123456");
con.setAutoCommit(false); //关闭自动提交,自动开启事务
pst = con.prepareStatement("select * from ak"); //可以不带参数
re = pst.executeQuery();
// con.rollback(); 出错默认回滚
while (re.next()) {
System.out.println(re.getInt("id"));
}
re.close();
pst.close();
con.close();
}
}
JDBC工具类测试
最新推荐文章于 2023-05-28 16:50:04 发布