C3P0连接MySQL8.0.11配置问题

这篇博客介绍了使用C3P0连接MySQL数据库的两种方法。第一种方法是在程序中直接指定数据库连接的user、url、password和driver,然后设置连接池的相关参数。第二种方法则是通过配置文件c3p0-config.xml来配置数据源,简化了代码中的数据库连接信息。博客还提到了配置文件c3p0-config.xml的新格式,包括driverClass、jdbcUrl等属性的设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C3P0连接MySQL有两种方式
第一种: 相关参数 在程序中指定user url password

   @Test
    public void testc3p0_01() throws Exception {
        //1.创建一个数据源对象
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        //2.通过配置文件mysql.properties
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");
        //给数据源comboPooledDataSource设置相关的参数
        //连接的管理是由comboPooledDataSource管理的
        comboPooledDataSource.setDriverClass(driver);
        comboPooledDataSource.setJdbcUrl(url);
        comboPooledDataSource.setUser(user);
        comboPooledDataSource.setPassword(password);
        //连接数初始化
        comboPooledDataSource.setInitialPoolSize(10);
        //最大连接数
        comboPooledDataSource.setMaxPoolSize(50);

        Connection connection = comboPooledDataSource.getConnection();//这个方法就是从DataSource接口实现的
        System.out.println("连接成功");
    }

第二种方式

public void testc3p0_02() throws SQLException {
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("hsp_edu");
        Connection connection = comboPooledDataSource.getConnection();
        System.out.println("连接成功");
        connection.close();
    }

配置文件c3p0-config.xml 要区别于老的版本

<!--驱动-->
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
  <!-- url-->
  	<property name="jdbcUrl">jdbc:mysql://localhost:3306/db02?useSSL=false&amp;serverTimezone=UTC&amp;characterEncoding=utf-8&amp;autoReconnect=true</property>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值