创建一张表

CREATE TABLE account(
id int PRIMARY KEY Auto_increment,
name varchar(10),
balance DOUBLE
);

INSERT INTO account (name,balance) VALUES ('zhangsan',1000),('李四',1000);

 

 

 

 

java代码:

  

public static void main(String[] args) throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        PreparedStatement statement1 = null;
        try {
            connection = JDBCUtils.getConnection();
            connection.setAutoCommit(false);
            String sql1 = "update account set balance = balance - ? where id = ?";
            String sql2 = "update account set balance = balance + ? where id = ?";
            statement = connection.prepareStatement(sql1);
            statement1 = connection.prepareStatement(sql2);

            statement.setDouble(1,500);
            statement.setInt(2,1);

            statement1.setDouble(1,500);
            statement1.setInt(2,2);
            //执行SQL
            statement.executeQuery();
            int i = 3/0;
            statement1.executeQuery();
            connection.commit();
        } catch (SQLException e) {
            try {
                if (connection!=null){
                    connection.rollback();
                }
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            statement.close();
        }

    }

出现了异常没事

看数据库的表

 

 

 

 

 

 

 

 

 

 

 

数据库连接池概述

  概念:

     其实就是一个容器(集合)存在数据库链接的容器。

    当系统初始化好后,容器被创建,容器中申请一写连接对象,当用户来访问数据库时,从容器中连接对象,用户访问完后,会将发连接对象返还给容器

  好处:

    节约资源

    用户访问高效

 

图:

 

 

posted on 2022-07-27 17:31  淤泥不染  阅读(16)  评论(0编辑  收藏  举报