4.28 DBCP和C3P0

1 分别使用DBCP和C3P0连接池,对数据库的一张表进行 增删改查操作,并测试事务,如果发生异常事务回滚(截图展示结果)

DBCP工具

package com.ak.day02.HomeWork;

import org.apache.commons.dbcp.BasicDataSource;

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

public class DBCPUtils {
    public static final String DRIVERNAME = "com.mysql.jdbc.Driver";
    public static final String URL = "jdbc:mysql://localhost:3306/mydb";
    public static final String USERNAME = "root";
    public static final String PASSWORD = "000000";
    //创建连接池对象
    public static BasicDataSource dataSource = new BasicDataSource();
    //配置数据
    static {
        dataSource.setDriverClassName(DRIVERNAME);
        dataSource.setUrl(URL);
        dataSource.setUsername(USERNAME);
        dataSource.setPassword(PASSWORD);
    }
    //获取连接方法
    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
    //释放资源
    public static void close(Connection connection, Statement statement){
        if (connection != null && statement != null){
            try {
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    //释放资源
    public static void close(Connection connection, Statement statement, ResultSet resultSet){
        if (connection != null && statement != null && resultSet != null){
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    // 提供  提交事务的方法
    public static void commit(Connection connection){
        try {
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    // 提供 事务回滚的方法
    public static void rollback(Connection connection){
        try {
            connection.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

DBPC插入和查询

package com.ak.day02.HomeWork;

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

public class DBCPUtils_Test {
    public static void main(String[] args) throws SQLException {
        Connection connection = DBCPUtils.getConnection();
        PreparedStatement preparedStatement
                = connection.prepareStatement("insert into books values (5,?,?,?,null,?,999)");
        preparedStatement.setString(1,"《金瓶梅》");
        preparedStatement.setString(2,"兰陵笑笑生");
        preparedStatement.setDouble(3,69.8);
        preparedStatement.setString(4,"禁书,奇书");
        //执行sql
        preparedStatement.executeUpdate();
        //查询
        PreparedStatement preparedStatement1 = connection.prepareStatement("select*from books");
        ResultSet resultSet = preparedStatement1.executeQuery();
        while (resultSet.next()){
            String b_id = resultSet.getString("b_id");
            String b_name = resultSet.getString("b_name");
            String authors = resultSet.getString("authors");
            String price = resultSet.getString("price");
            String note = resultSet.getString("note");
            System.out.println("编号:"+b_id+",书名:"+b_name+",作者:"+authors+",价格:"+price+"元,注解:"+note);
        }
        //释放资源
        DBCPUtils.close(connection,preparedStatement);
        System.out.println("执行完毕");
    }
}

4.28 DBCP和C3P0_第1张图片

事务

package com.ak.day02.HomeWork;

import com.ak.day02.JDBCUtils;
import org.junit.Before;

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

public class DBCPRollback {
    public static void main(String[] args) throws SQLException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = DBCPUtils.getConnection();
            //转账前查询
            preparedStatement = connection.prepareStatement("select *from account where eid in (1,2)");
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                String ename = resultSet.getString("ename");
                String money = resultSet.getString("money");
                System.out.println("转账前:"+ename+"="+money);
            }
            //开启事务
            connection.setAutoCommit(false);
            //获取预处理对象
            preparedStatement = connection.prepareStatement("update account set money = money+? where ename = ?");
            preparedStatement.setInt(1, -500);
            preparedStatement.setString(2, "张琳");
            preparedStatement.executeUpdate();
//            //模拟异常
//            int num = 7/0;
            preparedStatement.setInt(1, 500);
            preparedStatement.setString(2, "安可");
            preparedStatement.executeUpdate();
            //提交事务
            DBCPUtils.commit(connection);
            //转账后查询
            preparedStatement = connection.prepareStatement("select *from account where eid in (1,2)");
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                String ename = resultSet.getString("ename");
                String money = resultSet.getString("money");
                System.out.println("转账后:"+ename+"="+money);
            }
        } catch (Exception e) {
            e.printStackTrace();
            //回滚事务
            DBCPUtils.rollback(connection);
        } finally {
            //释放资源
            DBCPUtils.close(connection,preparedStatement,resultSet);
        }
    }
}

4.28 DBCP和C3P0_第2张图片

 C3P0工具

package com.ak.day02.HomeWork;

import com.mchange.v2.c3p0.ComboPooledDataSource;

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

public class c3p0Utils {
    public static final String DRIVERNAME = "com.mysql.jdbc.Driver";
    public static final String URL = "jdbc:mysql://localhost:3306/mydb";
    public static final String USERNAME = "root";
    public static final String PASSWORD = "000000";
    //创建连接池对象
    public static ComboPooledDataSource dataSource = new ComboPooledDataSource();
    //配置数据
    static {
        dataSource.setDataSourceName(DRIVERNAME);
        dataSource.setJdbcUrl(URL);
        dataSource.setUser(USERNAME);
        dataSource.setPassword(PASSWORD);
    }
    //获取连接
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }
    //释放资源
    public static void close(Connection connection, Statement statement){
        if (connection!=null&&statement!=null){
            try {
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    //释放资源
    public static void close(Connection connection, Statement statement, ResultSet resultSet){
        if (connection!=null&&statement!=null&&resultSet!=null){
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

 C3P0插入和查询

package com.ak.day02.HomeWork;

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

public class c3p0Test {
    public static void main(String[] args) throws SQLException {
        Connection connection = c3p0Utils.getConnection();
        PreparedStatement preparedStatement
                = connection.prepareStatement("insert into books values (5,?,?,?,null,?,999)");
        preparedStatement.setString(1,"《金瓶梅》");
        preparedStatement.setString(2,"兰陵笑笑生");
        preparedStatement.setDouble(3,69.8);
        preparedStatement.setString(4,"禁书,奇书");
        preparedStatement.executeUpdate();
        //查询
        PreparedStatement preparedStatement1 = connection.prepareStatement("select*from books");
        ResultSet resultSet = preparedStatement1.executeQuery();
        while (resultSet.next()){
            String b_id = resultSet.getString("b_id");
            String b_name = resultSet.getString("b_name");
            String authors = resultSet.getString("authors");
            String price = resultSet.getString("price");
            String note = resultSet.getString("note");
            System.out.println("编号:"+b_id+",书名:"+b_name+",作者:"+authors+",价格:"+price+"元,注解:"+note);
        }
        //释放资源
        c3p0Utils.close(connection,preparedStatement);
        System.out.println("执行完毕");
    }
}

4.28 DBCP和C3P0_第3张图片

 事务

package com.ak.day02.HomeWork;

import com.ak.day02.JDBCUtils;

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

public class c3p0Rollback {
    public static void main(String[] args) throws SQLException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = c3p0Utils.getConnection();
            //转账前查询
            preparedStatement = connection.prepareStatement("select *from account where eid in (1,2)");
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                String ename = resultSet.getString("ename");
                String money = resultSet.getString("money");
                System.out.println("转账前:"+ename+"="+money);
            }
            //开启事务
            connection.setAutoCommit(false);
            //获取预处理对象
            preparedStatement = connection.prepareStatement("update account set money = money+? where ename = ?");
            preparedStatement.setInt(1, -500);
            preparedStatement.setString(2, "张琳");
            preparedStatement.executeUpdate();
//            //模拟异常
//            int num = 7/0;
            preparedStatement.setInt(1, 500);
            preparedStatement.setString(2, "安可");
            preparedStatement.executeUpdate();
            //提交事务
            c3p0Utils.commit(connection);
            //转账后查询
            preparedStatement = connection.prepareStatement("select *from account where eid in (1,2)");
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                String ename = resultSet.getString("ename");
                String money = resultSet.getString("money");
                System.out.println("转账后:"+ename+"="+money);
            }
        } catch (Exception e) {
            e.printStackTrace();
            //回滚事务
            c3p0Utils.rollback(connection);
        } finally {
            //释放资源
            c3p0Utils.close(connection,preparedStatement,resultSet);
        }
    }
}

4.28 DBCP和C3P0_第4张图片

你可能感兴趣的:(mysql,java,数据库)