御用配置文件方式实现c3p0

运用配置文件方式实现c3p0时总提示找不到合适的驱动,但是用最基本的方式实现c3p0
时可以找到,请教各位大神如何解决。
报错提示如下

 java.sql.SQLException: No suitable driver
    at java.sql.DriverManager.getDriver(DriverManager.java:315)
    at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:285)
    at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:161)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:161)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:147)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:202)
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1138)
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1125)
    at com.mchange.v2.resourcepool.BasicResourcePool.access$700(BasicResourcePool.java:44)
    at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1870)
    at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:696)

c3p0-config.xml

 <?xml version="1.0" encoding="UTF-8" ?>
<c3p0-cofig>
    <default-config>
        <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/edb_71812</property>
        <property name="user">root</property>
        <property name="password">root</property>

        <!--当连接池中的连接耗尽的时候c3p0一次同时获得连接数-->
        <property name="acquireIncrement">5</property>
        <!--初始化时获取的连接个数,取值在minPoolSize和maxPoolSize之间-->
        <property name="initialPoolSize">10</property>
        <!--连接池中保留的最大连接池个数-->
        <property name="maxPoolSize">20</property>
        <!--连接池中保留的最小连接池个数-->
        <property name="minPoolSize">10</property>
    </default-config>

    <named-config name="sqlserver">
        <property name="driverClass">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="jdbcUrl">jdbc:sqlserver://localhost:1433;databaseName=edb_71812</property>
        <property name="user">sa</property>
        <property name="password">root</property>

        <!--当连接池中的连接耗尽的时候c3p0一次同时获得连接数-->
        <property name="acquireIncrement">5</property>
        <!--初始化时获取的连接个数,取值在minPoolSize和maxPoolSize之间-->
        <property name="initialPoolSize">10</property>
        <!--连接池中保留的最大连接池个数-->
        <property name="maxPoolSize">20</property>
        <!--连接池中保留的最小连接池个数-->
        <property name="minPoolSize">10</property>
    </named-config>
</c3p0-cofig>

MysqlJDBCUtil

 package com.mo.utils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MysqlJDBCUtil {
    private static ComboPooledDataSource ds = null;
    static {
//        System.setProperty("com.mchange.v2.c3p0.cfg.xml","/java/c3p0-config.xml");
        ds = new ComboPooledDataSource();
    }

    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

    public static void free(Connection conn, PreparedStatement ps, ResultSet rs){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(ps!=null){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

2个回答

mysql驱动jar包有添加嘛

使用c3p0的xml文件就行了吧,干嘛还要自己写获取连接和释放资源的操作,这两个功能在c3p0中不是已经封装好了吗?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问