企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
~~~ package JDBC; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com */ import JDBC.util.JDBCUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; /** * 事务操作 */ public class JDBCDemo10 { public static void main(String[] args) { Connection conn = null; PreparedStatement pstmt1 = null; PreparedStatement pstmt2 = null; try { //1.获取连接 conn = JDBCUtils.getConnection(); //开启事务 conn.setAutoCommit(false); //2.定义sql //2.1张三 -500 String sql1 = "update account set balance=balance-? where id=?"; //2.2李四+500 String sql2 = "update account set balance=balance+? where id=?"; //3.获取执行sql对象 pstmt1 = conn.prepareStatement(sql1); pstmt2 = conn.prepareStatement(sql2); //4.设置参数 pstmt1.setDouble(1, 500); pstmt1.setInt(2, 1); pstmt2.setDouble(1, 500); pstmt2.setInt(2, 2); //5.执行sql pstmt1.executeUpdate(); pstmt2.executeUpdate(); //手动制造异常 int i = 3 / 0; //提交事务 conn.commit(); } catch (Exception throwables) { //事务回滚 try { conn.rollback(); } catch (SQLException e) { e.printStackTrace(); } throwables.printStackTrace(); } finally { JDBCUtils.close(pstmt1, conn); JDBCUtils.close(pstmt2, null); } } } ~~~