美文网首页
c3p0连接池使用方法

c3p0连接池使用方法

作者: 毛仑上保罗先生 | 来源:发表于2018-01-05 10:16 被阅读0次

    maven下载jar

     <dependency>
                <groupId>com.mchange</groupId>
                <artifactId>c3p0</artifactId>
                <version>0.9.5.2</version>
     </dependency>
    

    新建JdbcUtil

    package api.utils;
    
    import java.beans.PropertyVetoException;
    import java.sql.Connection;
    import java.sql.SQLException;
    import javax.sql.DataSource;
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    
    
    /**
     * @author Paul.Huang
     * @data 2017/12/9 17:52
     * @Description
     */
    public class JdbcUtil {
        private static DataSource Reader;   //读库
        private static DataSource Write;    //写库
    
        static {
            try {
                //初始化c3p0连接池
                ComboPooledDataSource readercpds = new ComboPooledDataSource();
                readercpds.setDriverClass("com.mysql.jdbc.Driver");
                readercpds.setJdbcUrl("jdbc:mysql://localhost/data_center?characterEncoding=UTF-8");
                readercpds.setUser("root");
                readercpds.setPassword("Root123@");
                readercpds.setInitialPoolSize(5);
                readercpds.setMaxPoolSize(20);
                Reader = readercpds;
                
                ComboPooledDataSource writecpds = new ComboPooledDataSource();
                writecpds.setDriverClass("com.mysql.jdbc.Driver");
                writecpds.setJdbcUrl("jdbc:mysql://localhost/data_center?characterEncoding=UTF-8");
                writecpds.setUser("root");
                writecpds.setPassword("Root123@");
                writecpds.setInitialPoolSize(5);
                writecpds.setMaxPoolSize(20);
                Write = writecpds;
            } catch (PropertyVetoException e) {
                throw new ExceptionInInitializerError("属性不匹配的错误");
            }
        }
    
        /*
         * 写库数据源
         */
        public static DataSource readerDataSource() {
            return Reader;
        }
    
        /*
         * 读库数据源
         */
        public static DataSource writeDataSource() {
            return Write;
        }
    
        /*
         * 写库当前连接
         */
        public static Connection writeConnection() throws SQLException {
            return Write.getConnection();
        }
    
        /*
         * 读库当前连接
         */
        public static Connection readerConnection() throws SQLException {
            return Reader.getConnection();
        }
    }
    

    使用方法

     @Test
        public void select() throws SQLException {
            Connection connection = null;
            Statement statement = null;
            ResultSet resultSet = null;
            try {
                connection = JdbcUtil.readerConnection();
                statement = connection.createStatement();
                resultSet = statement.executeQuery("select * from data_control_price LIMIT 10 ");
                while (resultSet.next()) {
                    System.out.println(resultSet.getString("merchant_code"));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                if (resultSet != null) try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                if (statement != null) try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                if (connection != null) try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    
    不过我在把数据库信息写在xml的时候, c3p0读不出来, 查询下好像已经停止更新了, 所以本人打算把项目的数据库连接池换成国产阿里巴巴的druid

    相关文章

      网友评论

          本文标题:c3p0连接池使用方法

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