JDBC(下)

事务
(一)事务的概念
在这里插入图片描述
(二)MySQL的事务操作
在这里插入图片描述
(三)JDBC事务的操作

操作:

1、开启事务 conn.setAutoCommit(false);

2、提交事务 conn.commit();

3、回滚事务 conn.rollback();

(四)事务的隔离级别(了解)
在这里插入图片描述
二、数据库连接池
(一)连接池简介
在这里插入图片描述
(二)手动实现连接池
在这里插入图片描述
代码

import com.wwz.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//模拟数据库连接池
public class MyDataBasePool {
    private MyDataBasePool(){}

    //初始化连接数量
    private static int conn_size = 10;
    //定义一个集合作为连接池
    private static List<Connection> connPools = new ArrayList<>();

    //初始化连接池
    static {
        for (int i = 0; i < conn_size; i++) {
            Connection connection = JdbcUtils.getConnection();
            connPools.add(connection);
        }
        System.out.println("连接池初始化成功,创建连接数目为:"+conn_size);
    }

    //从连接池中获取连接
    public static Connection getConnFromPools(){
        Connection conn = connPools.get(0);
        //从池中获得连接后,数据库连接池会相应减少该连接
        connPools.remove(conn);
        System.out.println("用户正在使用"+conn+"连接,当前连接池中剩余连接数目为:"+connPools.size());
        return conn;
    }

    //用完归还连接
    public static void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){
        try {
            if (resultSet != null){
                resultSet.close();
            }
            if (preparedStatement != null){
                preparedStatement.close();
            }
            if (connection != null){
                connPools.add(connection);
                System.out.println("用户归还连接"+connection+"后,当前池子中剩余连接数目为:"+connPools.size());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TestConnPools {

    @Test
    public void testPools(){
        Connection connFromPools = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //从连接池获取连接
            connFromPools = MyDataBasePool.getConnFromPools();
            preparedStatement = connFromPools.prepareStatement("select * from account");
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                int i = resultSet.getInt(1);
                String s = resultSet.getString(2);
                System.out.println(i+"\t"+s);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //归还连接
            MyDataBasePool.close(connFromPools,preparedStatement,resultSet);
        }


    }
}

(三)DBCP连接池
在这里插入图片描述
代码
在这里插入图片描述
(四)c3p0连接池
在这里插入图片描述
代码
在这里插入图片描述
三、DBUtils工具
(一)DBUtils工具简介
在这里插入图片描述
在这里插入图片描述
(二)JavaBean
在这里插入图片描述
(三)准备工作
在这里插入图片描述
(三)DBUtils之DML操作
在这里插入图片描述
(四)DBUtils之DQL操作
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值