美文网首页mysql
2020-08-17C3P0数据库连接池与druid数据库连接池

2020-08-17C3P0数据库连接池与druid数据库连接池

作者: 智障猿 | 来源:发表于2020-08-26 11:39 被阅读0次

    C3P0下载地址
    https://mvnrepository.com/artifact/com.mchange/c3p0

    C3P0数据库连接池

    • 步骤
      ①导入jar包c3p0和mchange-commons-java不要忘记导入数据库驱动jar包
      ②定义配置文件c3p0.properties或者c3p0-config.xml,路径为src目录下
      ③创建核心对象CombopooledDataSource; DataSource dataSource = new ComboPooledDataSource();
      ④获取连接getDataSource
    • 配置文件c3p0-config.xml
    <?xml version="1.0" encoding="utf-8"?>
    <c3p0-config>
        <default-config>
    <!--        连接参数-->
            <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
            <property name="jdbcUrl">jdbc:mysql://localhost:3306/db4?&amp;useSSL=false&amp;serverTimezone=GMT</property>
            <property name="user">root</property>
            <property name="password">545267257zzt</property>
    <!--        连接池参数-->
    <!--        初始化申请的连接数-->
            <property name="initialPoolSize">10</property>
    <!--        创建过的链接并使用后闲置下来的闲置时间,如果超过这个时间就会自动断开这个连接-->
            <property name="maxIdleTime">30</property>
    <!--        最大连接数-->
            <property name="maxPoolSize">20</property>
    <!--        最小连接数-->
            <property name="minPoolSize">5</property>
    <!--        最多可以创建Statements对象的个数. . 就是可以执行baiSQL语句的对象的个数-->
            <property name="maxStatements">200</property>
        </default-config>
    
        <named-config name="mysql">
            <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
            <property name="jdbcUrl">jdbc:mysql://localhost:3306/db4?&amp;useSSL=false&amp;serverTimezone=GMT</property>
            <property name="user">root</property>
            <property name="password">545267257zzt</property>
    
            <property name="initialPoolSize">10</property>
            <property name="maxIdleTime">30</property>
            <property name="maxPoolSize">20</property>
            <property name="minPoolSize">5</property>
            <property name="maxStatements">200</property>
        </named-config>
    
    </c3p0-config>
    
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    import javax.sql.DataSource;
    import java.sql.Connection;
    import java.sql.SQLException;
    
    public class c3p0 {
        public static void main(String[] args) throws SQLException {
            //1.获取DataSource,使用默认配置
            DataSource dataSource = new ComboPooledDataSource();
            //使用配置文件中指定的配置信息,连接不同的数据库
            //DataSource dataSource1 = new ComboPooledDataSource("mysql");
            //2.获取连接
            Connection connection = dataSource.getConnection();
            //sql操作
            //释放连接
            connection.close();
        }
    }
    

    Druid数据库连接池

    • 步骤
    1. 导入druid-x.x.x.jar包
    2. 定义配置文件,是properties形式,可以叫任意名称,可以放置在任意路径下,手动加载
    3. 加载配置文件
      ClassLoader classLoader = druid.class.getClassLoader();
      URL resource = classLoader.getResource("druid.properties");
      String string = resource.getPath();
      properties.load(new FileReader(string));
    4. 获取数据源连接池对象:通过工厂类来获取DruidDataSourceFactory
    5. 获取连接getConnection;
    • 配置文件druid.properties
    driverClassname=com.mysql.cj.jdbc.Driver
    url=jdbc:mysql://localhost:3306/db4?useSSL=false&serverTimezone=UTC
    username=root
    password=545267257zzt
    #初始化连接数
    initialSize=5
    #最大连接数
    maxActive=10
    #最大等待时间
    maxWait=3000
    
    import javax.sql.DataSource;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.net.URL;
    import java.sql.Connection;
    import java.util.Properties;
    
    public class druid {
        public static void main(String[] args) throws Exception {
            Properties properties = new Properties();
            ClassLoader classLoader = druid.class.getClassLoader();
            URL resource = classLoader.getResource("druid.properties");
            String string = resource.getPath();
            properties.load(new FileReader(string));
            DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
            Connection connection = dataSource.getConnection();
            System.out.println(connection);
    
        }
    }
    

    定义druid的工具类

    1. 定义一个类JDBCUtils
    2. 提供静态代码块加载配置文件,初始化连接池对象
    3. 提供方法
      ①获取连接的方法:通过数据库连接池获取连接
      ②释放资源
      ③获取连接池的方法
    package it.heima.util;
    
    import com.alibaba.druid.pool.DruidDataSourceFactory;
    
    import javax.sql.DataSource;
    import java.io.FileReader;
    import java.io.IOException;
    import java.net.URL;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    
    public class jdbcUtil {
    //    1. 定义成员变量DataSource
        private static DataSource ds;
        static {
            Properties properties = new Properties();
            ClassLoader classLoader = jdbcUtil.class.getClassLoader();
            URL resource = classLoader.getResource("druid.properties");
            String path = resource.getPath();
            try {
                properties.load(new FileReader(path));
                ds= DruidDataSourceFactory.createDataSource(properties);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /*
        *获取连接的方法
         */
        public static Connection getConnection() throws SQLException {
            return ds.getConnection();
        }
        /*
        *释放资源
         */
        public static void close(Statement statement,Connection connection){
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        public static void close(Statement statement, Connection connection, ResultSet resultSet){
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    
        /*
        获取连接池的方法
         */
        public static DataSource getDataSource(){
            return ds;
        }
    }
    
    public class jdbcUtilTest {
        public static void main(String[] args) {
            Connection connection = null;
            PreparedStatement preparedStatement = null;
            try {
                connection = jdbcUtil.getConnection();
                String sql = "insert into student (id,name,cid) values (3, ?, 2)";
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1,"zhangzetao");
                preparedStatement.executeUpdate();
                jdbcUtil.close(preparedStatement,connection);
    
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    

    相关文章

      网友评论

        本文标题:2020-08-17C3P0数据库连接池与druid数据库连接池

        本文链接:https://www.haomeiwen.com/subject/eyufjktx.html