zoukankan      html  css  js  c++  java
  • jdbc 开启事务

    package com.itheima.tx;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import java.sql.Types;
    
    import org.junit.Test;
    
    import com.itheima.utils.JdbcUtil;
    /**
     * 转账   事务控制
     * update account set money=money-100 where name='aaa';
    update account set money=money+100 where name='bbb';
     * @author wangli
     * 
     * 
     * 开事务 
     *       con.setAutoCommit(false);
     * 
     * 提交 
     *       con.commit();
     * 回滚
     *       con.rollback();
     *
     */
    public class TransactionDemo1 {
        @Test   
        public void testTransaction(){
            Connection con = null;
            PreparedStatement st =null;
            PreparedStatement st2 = null;
            try {
                con = JdbcUtil.getConnection();
                //设置隔离级别,一定是放在开启事务前
                con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
                con.setAutoCommit(false);// 放在得到st之前
                st = con.prepareStatement("update account set money=money-100 where name='aaa'");
                st.executeUpdate();
                //int i=1/0;
                st2 = con.prepareStatement("update account set money=money+100 where name='bbb'");
                st2.executeUpdate();
                con.commit();//提交 
            } catch (Exception e) {
                e.printStackTrace();
                if(con!=null){
                    try {
                        con.rollback();//回滚
                    } catch (SQLException e1) {
                        e1.printStackTrace();
                    }
                }
            }finally{
                JdbcUtil.release(null, st, con);
            }
        }
    }
  • 相关阅读:
    qmake Manual (EN) 1
    {转}linux gcc gdb使用
    qmake 简介
    {转}linux makefile 详细教程
    {转}Linux下C开发之——gcc,gdb的使用
    关于“做一个聊天+信息分享客户端”的设想(SNS?)
    {转}算法的力量
    hdu 2047 简单递推公式
    RONOJ 6 金明的预算方案
    hdu 2446 二分搜索解题报告
  • 原文地址:https://www.cnblogs.com/baijin05/p/5073298.html
Copyright © 2011-2022 走看看