Java数据库连接池(C3P0、DBCP)

C3P0

第一步、引入Jar包

C3P0 jar包

第二步、创建配置文件
在src目录下创建c3p0-config.xml文件


<c3p0-config>

  
  <default-config>
    
    <property name="driverClass">com.mysql.jdbc.Driverproperty>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/数据库名?useSSL=falseproperty>
    <property name="user">rootproperty>
    <property name="password">rootproperty>

    
    <property name="initialPoolSize">5property>
    
    <property name="minPoolSize">2property>
    
    <property name="maxPoolSize">20property>
  default-config>

  
  <named-config name="xiaowen"> 
    <property name="driverClass">com.mysql.jdbc.Driverproperty>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/数据库名?useSSL=falseproperty>
    <property name="user">rootproperty>
    <property name="password">rootproperty>
  named-config>


c3p0-config>

useSSL=false,参考:Mysql报警告:WARN: Establishing SSL connection

第三步、获取连接

package com.xiaowen.jdbc.utils;

import java.sql.Connection;
import java.sql.SQLException;


import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3P0Utils {
    private static ComboPooledDataSource dataSource=null;
    public static Connection getConnection() {
        try {
            dataSource=new ComboPooledDataSource();//使用默认配置
            //dataSource=new ComboPooledDataSource("xiaowen");//使用带有命名的配置
            return dataSource.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException();
        }
    }
}

测试方法:

    /**
     * 添加用户
     */
    @Test
    public void testAddUser1() {
        Connection conn=null;
        PreparedStatement pst=null;
        try {
            conn=C3P0Utils.getConnection();
            String sql="insert into tbl_user values(null,?,?)";
            pst=conn.prepareStatement(sql);
            pst.setString(1, "吕小布");
            pst.setString(2, "123");
            int rows=pst.executeUpdate();
            if(rows>0) {
                System.out.println("添加成功");
            }else {
                System.out.println("添加失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        finally {
            try {
                conn.close();
                pst.close();
            } catch (Exception e) {
            }

        }
    }

DBCP
第一步、引入Jar包
DBCP jar包

第二步、创建配置文件
在src目录下创建后缀名为properties的文件,如:db.properties

里面内容是JDBC四件套

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/web08?useUnicode=true&characterEncoding=utf8
username=root
password=root

第三步、获取连接

package com.xiaowen.jdbc.utils;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;

public class DBCPUtils {
    private static DataSource dataSource;

    static {

        try {
            // 1、加载properties文件
            InputStream is = DBCPUtils.class.getClassLoader().getResourceAsStream("db.properties");
            // 2、加载输入流
            Properties props = new Properties();
            props.load(is);
            //2、创建数据源
            dataSource=BasicDataSourceFactory.createDataSource(props);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static DataSource getDataSource() {
        return dataSource;
    }
    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

测试类:

    @Test
    public void testUpdateUserById() {
        Connection conn=null;
        PreparedStatement pst;
        try {
            conn=DBCPUtils.getConnection();
            String sql="update tbl_user set upassword=? where uid=?";
            pst=conn.prepareStatement(sql);
            pst.setString(1, "456");
            pst.setInt(2, 2);
            int rows=pst.executeUpdate();
            if(rows>0) {
                System.out.println("更新成功");
            }else {
                System.out.println("更新失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

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