zoukankan      html  css  js  c++  java
  • c3p0数据源的第一次尝试

    开始补习 以前学习过的基础

    正在尝试从c3p0 获取到connection

    好的,首先上代码吧

     public static DataSource ds = null;
        static {
            ComboPooledDataSource cpds = new ComboPooledDataSource();
            try {
                cpds.setDriverClass("com.mysql.jdbc.Driver");
                cpds.setJdbcUrl("jdbc:mysql://localhost:3306/student");
    
    
                cpds.setUser("root");
                cpds.setPassword("123456");
                cpds.setInitialPoolSize(5);
                cpds.setMaxPoolSize(15);
    
                ds = cpds;
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) throws SQLException {
            System.out.println(ds.getConnection());
        }

    嗯,很简单,一遍就能懂。

    jar 包的话,就是c3p0,mysql驱动。

    这是手动设置的c3p0,还有配置版的

    <?xml version="1.0" encoding="utf-8" ?>
    <c3p0-config>
        <default-config>
            <property name="user">root</property>
            <property name="password">123456</property>
            <property name="driverClass">com.mysql.jdbc.Driver</property>
            <property name="jdbcUrl">
                jdbc:mysql://localhost:3306/student
            </property>
            <property name="checkoutTimeout">30000</property>
            <property name="initialPoolSize">10</property>
            <property name="maxIdleTime">30</property>
            <property name="maxPoolSize">100</property>
            <property name="minPoolSize">10</property>
            <property name="maxStatements">200</property>
        </default-config>
        <named-config name="student">
            <property name="initialPoolSize">5</property>
            <property name="maxPoolSize">15</property>
            <property name="driverClass">com.mysql.jdbc.Driver</property>
            <property name="jdbcUrl">
                jdbc:mysql://localhost:3306/student
            </property>
            <property name="user">root</property>
            <property name="password">123456</property>
        </named-config>
    </c3p0-config>

    一定要放在src目录下!!!!

    然后用这个配置的话,是在

    ComboPooledDataSource cpds  =new ComboPooledDataSource();
    这样的话是用默认的c3p0 配置

    ComboPooledDataSource cpds  =new ComboPooledDataSource("student");
    这样的话就是用name 为student的那个c3p0 配置。
  • 相关阅读:
    LeetCode
    算法
    GitHub
    GitHub
    git
    将博客搬家至CSDN
    base64与图片互转
    windows下mongodb数据库搭建过程遇到问题
    mongodb数据插入语句与navicat导入mongodb的json结构
    Visual C++安装失败解决:Error 0x80240017: Failed to execute MSU package.
  • 原文地址:https://www.cnblogs.com/LiuShuang-GoJava/p/10631303.html
Copyright © 2011-2022 走看看