day18-事务与连接池 3.jdbc中事务操作介绍
那么我们都是通过程序操作数据库。所以要了解jdbc下怎样对事务操作。jdbc如何操作事务?
自动事务false那就不开了呗相当于开启事务。
package cn.itcast.transaction; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import cn.itcast.utils.JdbcUtils; //代码加上事务了加上回滚了加上提交了 //jdbc中事务操作 public class TransactionTest1 { public static void main(String[] args) throws SQLException { //修改id=2这个人的money=500; String sql = "update account set money=500 where id=2"; Connection con = JdbcUtils.getConnection(); con.setAutoCommit(false);//开启事务,相当于start transaction; Statement st = con.createStatement(); st.executeUpdate(sql); //事务回滚 //con.rollback(); con.commit();//事务提交 st.close(); con.close(); } }
package cn.itcast.transaction; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import cn.itcast.utils.JdbcUtils; //代码加上事务了加上回滚了加上提交了 //jdbc中事务操作 public class TransactionTest2 { public static void main(String[] args) { //修改id=2这个人的money=500; String sql = "update account set money=500 where id=2"; //事务操作的异常不能说想抛就抛 Connection con = null; Statement st = null; try { //如果数据库操作过程中捕获异常了, con = JdbcUtils.getConnection(); con.setAutoCommit(false);//开启事务,相当于start transaction; st = con.createStatement(); st.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); //事务回滚 有异常就把事务回滚,有异常就说明出问题了 //出问题就把数据恢复到原始,那就回滚吧 try { con.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } }finally{//finally里面的内容肯定是能执行到的 try { con.commit();//事务提交 st.close();//资源关闭 释放资源操作 con.close();//资源关闭 } catch (SQLException e) { e.printStackTrace(); } } } }
package cn.itcast.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ResourceBundle; public class JdbcUtils { private static final String DRIVERCLASS; private static final String URL; private static final String USERNAME; private static final String PASSWORD; private static final ThreadLocal<Connection> tl = new ThreadLocal<Connection>(); static { DRIVERCLASS = ResourceBundle.getBundle("jdbc").getString("driverClass"); URL = ResourceBundle.getBundle("jdbc").getString("url"); USERNAME = ResourceBundle.getBundle("jdbc").getString("username"); PASSWORD = ResourceBundle.getBundle("jdbc").getString("password"); } static { try { // 将加载驱动操作,放置在静态代码块中.这样就保证了只加载一次. Class.forName(DRIVERCLASS); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { Connection con = tl.get();// 从ThreadLocal中获取Connection。第一次获取得到的是null. if (con == null) { // 2.获取连接 con = DriverManager.getConnection(URL, USERNAME, PASSWORD); tl.set(con); // 将con装入到ThreadLocal中。 } // tl.remove(); //解除 return con; } // 关闭操作 public static void closeConnection(Connection con) throws SQLException { if (con != null) { con.close(); } } public static void closeStatement(Statement st) throws SQLException { if (st != null) { st.close(); } } public static void closeResultSet(ResultSet rs) throws SQLException { if (rs != null) { rs.close(); } } }