package com.mxy.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class JDBCTest11 { public static void main(String[] args) { Connection conn = null; PreparedStatement ps = null; try { //1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); //2.获取连接 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=utf8&useSSL=false", "root", "123456"); //将自动提交机制修改为手动提交 conn.setAutoCommit(false);//开启事务 //3.获取预编译的数据库 *** 作对象 String sql = "update t_act set balance = ? where actno = ?"; ps = conn.prepareStatement(sql); //给?传值 ps.setDouble(1, 10000); ps.setInt(2, 111); int count = ps.executeUpdate(); // String s = null; // s.toString(); //给?传值 ps.setDouble(1, 10000); ps.setInt(2, 222); count += ps.executeUpdate(); System.out.println(count == 2 ? "转账成功" : "转账失败"); //程序能够走到这里说明以上程序没有异常,事务结束,手动提交数据 conn.commit();//提交事务 } catch (ClassNotFoundException | SQLException e) { //回滚事务 if(conn != null){ try { conn.rollback(); } catch (SQLException ex) { ex.printStackTrace(); } } e.printStackTrace(); }finally { if(ps != null){ try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn != null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)