IDEA中使用JDBC的事务管理

 @Test
    /**
     * 完成一个转账的案例
     */
    public  void  demo1(){
        Connection conn = null;
        PreparedStatement pstmt = null;
        try{
            /**
             * 完成转账代码:
             * 扣除某个账号的钱
             * 给另一个账号加钱
             */
            //获得连接
            conn = JDBCUtils.getConnection();
            //开启事务
            conn.setAutoCommit(false);
            //编写SQL语句
            String sql = "update account set money = money + ? where name = ?";
            pstmt = conn.prepareStatement(sql);
            //aaa账户扣除1000
            pstmt.setDouble(1,-1000);
            pstmt.setString(2,"aaa");
            pstmt.executeUpdate();
            //bbb账户增加1000
            pstmt.setDouble(1,1000);
            pstmt.setString(2,"bbb");
            pstmt.executeUpdate();
            //提交事务
            conn.commit();
        }catch (Exception e){
            //回滚事务
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            JDBCUtils.release(pstmt,conn);
        }

    }
public class JDBCUtils {

    private static final String USER_NAME;
    private static final String URL;
    private static final String PASSWORD;
    private static final String DRIVER_NAME;

static {
    //获取属性文件中的内容:
    ResourceBundle resource = ResourceBundle.getBundle("db");//test为属性文件名,放在包com.mmq下,如果是放在src下,直接用test即可
    USER_NAME = resource.getString("username");
    URL = resource.getString("url");
    PASSWORD = resource.getString("password");
    DRIVER_NAME = resource.getString("driver_name");
}


    //获取绝对路径
String path = Thread.currentThread().getContextClassLoader().getResource("main/java/classinfo.properties").getPath();
//    FileReader fileReader = new FileReader(path);
//    //以流的形式返回
//    InputStream fileReader = Thread.currentThread().getContextClassLoader().getResourceAsStream("main/java/classinfo.properties");
//    Properties properties = new Properties();
//properties.load(fileReader);
//fileReader.close();
//    //通过key获取value
//    String classname = properties.getProperty("className");
//System.out.println(classname);

//    Properties properties = new Properties();
//    FileReader fileReader = new FileReader(path);
//    InputStream fileReader = Thread.currentThread().getContextClassLoader().getResourceAsStream("src/db.properties");
//
//    //    String path = JDBCUtils.class.getClassLoader().getResource("db.properties").getPath();
//    File file = new File(path);
//        try{
//        FileInputStream inputStream = new FileInputStream(file);
//        properties.load(inputStream);
//        URL = Integer.parseInt(properties.getProperty("url"));
//        tableSize = Integer.parseInt(properties.getProperty("default_size"));
//        gridSize = Integer.parseInt(properties.getProperty("grid_size"));
        currentMap = new int[tableSize][tableSize];
        neighbors = new int[tableSize][tableSize];
//    } catch (FileNotFoundException e) {
//        e.printStackTrace();
//    } catch (IOException e) {
//        e.printStackTrace();
//    }
//    Properties properties = new Properties();
//    try{
//        properties.load(new FileInputStream("src/db.properties"));
//    }catch(FileNotFoundException e){
//        e.printStackTrace();
//    }catch(IOException e){
//        e.printStackTrace();
//    }
    //mysql驱动包名
//    private static final String DRIVER_NAME = "com.mysql.jdbc.Driver";
//    //数据库连接地址
    private static final String URL = "jdbc:mysql://localhost:3306/demo";
//    private static final String URL = "jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8&useSSL=false";
//    //用户名,更换成你自己的用户名,此处为root用户
//    private static final String USER_NAME = "root";
//    //密码,更换成你自己设定的密码,此处为:admin
//    private static final String PASSWORD = "password";
    /**
     * 注册驱动的方法
     */
    public static void loadDriver() {
//        System.out.println(URL);
//        System.out.println(USER_NAME);
//        System.out.println(PASSWORD);
//        System.out.println(DRIVER_NAME);
        try {
            Class.forName(DRIVER_NAME);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获得连接的方法
     */
    public static Connection getConnection() {

        Connection conn = null;
        try {
            loadDriver();
            conn = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  conn;
        }
        /**
         * 释放资源的方法
         */
        public static  void release(Statement stmt,Connection conn){
            if(stmt != null){
                try{
                    stmt.close();
                }catch (SQLException e){
                    e.printStackTrace();
                }
                stmt = null;
            }
            if (conn != null){
                try{
                    conn.close();
                }catch (SQLException e){
                    e.printStackTrace();
                }
                conn = null;
            }
        }


    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值