【转账案例】
CREATE TABLE t_account( id INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(40), account DOUBLE ); INSERT INTO t_account(NAME,account) VALUES('李星云',1000); INSERT INTO t_account(NAME,account) VALUES('姬如雪',800); SELECT * FROM t_account;
JDBCUtils.java
package zh.jdbc.demo; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCUtils { private JDBCUtils() { } // 获取数据库连接 public static Connection getConnection(String driverClassName, String url, String username, String password) { try { Class.forName(driverClassName); } catch (ClassNotFoundException e) { e.printStackTrace(); } Connection connection = null; try { connection = DriverManager.getConnection(url, username, password); return connection; } catch (SQLException cause) { throw new RuntimeException("获取数据库连接失败", cause); } } // 关闭资源 public static void close(Statement statement, Connection connection) { if (statement != null) { try { statement.close(); } catch (SQLException cause) { throw new RuntimeException("关闭Statement失败", cause); } statement = null; } if (connection != null) { try { connection.close(); } catch (SQLException cause) { throw new RuntimeException("关闭数据库连接失败", cause); } } connection = null; } // 关闭资源 public static void close(ResultSet resultSet, Statement statement, Connection connection) { if (resultSet != null) { try { resultSet.close(); } catch (SQLException cause) { throw new RuntimeException("关闭ResultSet失败", cause); } } resultSet = null; close(statement, connection); } }
AccountDao1.java
package zh.jdbc.demo; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class AccountDao1 { public AccountDao1() { } private String driverClassName = "com.mysql.jdbc.Driver"; private String url = "jdbc:mysql://localhost:3306/day0528"; private String username = "root"; private String password = "zh1277718668"; // 转账 public boolean transfer(String from, String to, double account) { Connection connection = null; PreparedStatement pstatement = null; try { connection = JDBCUtils.getConnection(driverClassName, url, username, password); connection.setAutoCommit(false);// 开启事务 String sql = "update t_account set account = account + ? where name = ?"; pstatement = connection.prepareStatement(sql); pstatement.setDouble(1, 0 - account); pstatement.setString(2, from); pstatement.execute(); pstatement.setDouble(1, account); pstatement.setString(2, to); pstatement.executeUpdate(); connection.commit();// 提交事务 return true; } catch (Exception cause) { try { connection.rollback();// 回滚事务 return false; } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("转账失败", cause); } } finally { JDBCUtils.close(pstatement, connection); } } }
或者,使用批处理
AccountDao2.java
package zh.jdbc.demo; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class AccountDao2 { public AccountDao2() { } private String driverClassName = "com.mysql.jdbc.Driver"; private String url = "jdbc:mysql://localhost:3306/day0528"; private String username = "root"; private String password = "zh1277718668"; // 转账 public boolean transfer(String from, String to, double account) { Connection connection = null; PreparedStatement pstatement = null; try { connection = JDBCUtils.getConnection(driverClassName, url, username, password); connection.setAutoCommit(false);// 开启事务 String sql = "update t_account set account = account + ? where name = ?"; pstatement = connection.prepareStatement(sql); pstatement.setDouble(1, 0 - account); pstatement.setString(2, from); pstatement.addBatch(); pstatement.setDouble(1, account); pstatement.setString(2, to); pstatement.addBatch(); pstatement.executeBatch();// 批处理 connection.commit();// 提交事务 return true; } catch (Exception cause) { try { connection.rollback();// 回滚事务 return false; } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("转账失败", cause); } } finally { JDBCUtils.close(pstatement, connection); } } }
AccountDaoDemo.java
package zh.jdbc.demo; public class AccountDaoDemo { public static void main(String[] args) { //AccountDao1 accountDao = new AccountDao1(); AccountDao2 accountDao = new AccountDao2(); boolean transfer = accountDao.transfer("李星云", "姬如雪", 100); System.out.println(transfer); } }
【结果】
先使用AccountDao1+AccountDaoDemo
再使用AccountDao2+AccountDaoDemo