posts - 397,comments - 0,views - 25332

创建一张表

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   淤泥不染  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示