bonecp mysql No suitable driver found for jdbc:mysql

java.sql.SQLException: Unable to open a test connection to the given database. JDBC url = jdbc:mysql://192.168.1.136:3306/shop, username = root. Terminating connection pool. Original Exception: ------
java.sql.SQLException: No suitable driver found for jdbc:mysql://192.168.1.136:3306/shop
    at java.sql.DriverManager.getConnection(DriverManager.java:604)
    at java.sql.DriverManager.getConnection(DriverManager.java:221)
    at com.jolbox.bonecp.BoneCP.obtainRawInternalConnection(BoneCP.java:242)
    at com.jolbox.bonecp.BoneCP.<init>(BoneCP.java:289)
    at Netpet.Web.Data.pool.Pool2.configure(Pool2.java:118)
    at Netpet.Web.Data.pool.Pool2.getConnection(Pool2.java:25)
    at Netpet.Web.Data.ConnectionPool.GetConnection1(ConnectionPool.java:92)
    at Netpet.Web.Data.ConnectionPool.GetConnection(ConnectionPool.java:72)
    at Netpet.Web.Data.ConnectionPool.GetConnection(ConnectionPool.java:56)
    at Netpet.Web.Data.Netpet.BaseConfig.Config(BaseConfig.java:98)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at Netpet.Web.Config.InitConfig.InitAllConfigTrue(InitConfig.java:162)
    at Netpet.Web.Config.InitConfig.InitAllConfig(InitConfig.java:97)
    at Netpet.Web.InitServer.init(InitServer.java:40)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1266)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1185)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1080)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5027)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:618)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1100)
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1618)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
------

    at com.jolbox.bonecp.BoneCP.<init>(BoneCP.java:296)
    at Netpet.Web.Data.pool.Pool2.configure(Pool2.java:118)
    at Netpet.Web.Data.pool.Pool2.getConnection(Pool2.java:25)
    at Netpet.Web.Data.ConnectionPool.GetConnection1(ConnectionPool.java:92)
    at Netpet.Web.Data.ConnectionPool.GetConnection(ConnectionPool.java:72)
    at Netpet.Web.Data.ConnectionPool.GetConnection(ConnectionPool.java:56)
    at Netpet.Web.Data.Netpet.BaseConfig.Config(BaseConfig.java:98)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at Netpet.Web.Config.InitConfig.InitAllConfigTrue(InitConfig.java:162)
    at Netpet.Web.Config.InitConfig.InitAllConfig(InitConfig.java:97)
    at Netpet.Web.InitServer.init(InitServer.java:40)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1266)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1185)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1080)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5027)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:618)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1100)
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1618)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://192.168.1.136:3306/shop
    at java.sql.DriverManager.getConnection(DriverManager.java:604)
    at java.sql.DriverManager.getConnection(DriverManager.java:221)
    at com.jolbox.bonecp.BoneCP.obtainRawInternalConnection(BoneCP.java:242)
    at com.jolbox.bonecp.BoneCP.<init>(BoneCP.java:289)
    ... 31 more

 

 

使用bonecp连接池总是报这个错误,经过半天的测试终于找到了原因,

 

初始化数据库连接时没有写:Class.forName("com.mysql.jdbc.Driver");

 

 

代码如下:

 

public synchronized static void configure(Properties props) throws SQLException{
		if(pools != null && pools.size() > 0){return;}
		
		try {
            // load the database driver (make sure this is in your classpath!)
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
		
		// setup the connection pool
        BoneCPConfig config = new BoneCPConfig();
        config.setJdbcUrl("jdbc:mysql://"+GlobalConfig.mysqlDbIp+":3306/shop"); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb
        config.setUsername(GlobalConfig.mysqlDbUserName); 
        config.setPassword(GlobalConfig.mysqlDbPwd);
        //设置每60秒检查数据库中的空闲连接数
        config.setIdleConnectionTestPeriod(60);
        //设置连接空闲时间
        config.setIdleMaxAge(240);
        //设置每个分区中的最大连接数 30
        config.setMaxConnectionsPerPartition(5);
        //设置每个分区中的最小连接数 10
        config.setMinConnectionsPerPartition(5);
        //当连接池中的连接耗尽的时候 BoneCP一次同时获取的连接数
        config.setAcquireIncrement(5);
        //连接释放处理
        config.setReleaseHelperThreads(3);
        //设置分区  分区数为3
        config.setPartitionCount(3);
        //设置配置参数
        BoneCP connectionPool = new BoneCP(config); // setup the connection pool
        
        pools.put(DatabaseConfig.DEFAULT_NAME, connectionPool);
        
        
        
        
        BoneCPConfig config2 = new BoneCPConfig();
        config2.setJdbcUrl("jdbc:mysql://"+GlobalConfig.mysqlDbIp+":3306/account"); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb
        config2.setUsername(GlobalConfig.mysqlDbUserName); 
        config2.setPassword(GlobalConfig.mysqlDbPwd);
        //设置每60秒检查数据库中的空闲连接数
        config2.setIdleConnectionTestPeriod(60);
        //设置连接空闲时间
        config2.setIdleMaxAge(240);
        //设置每个分区中的最大连接数 30
        config2.setMaxConnectionsPerPartition(5);
        //设置每个分区中的最小连接数 10
        config2.setMinConnectionsPerPartition(5);
        //当连接池中的连接耗尽的时候 BoneCP一次同时获取的连接数
        config2.setAcquireIncrement(5);
        //连接释放处理
        config2.setReleaseHelperThreads(3);
        //设置分区  分区数为3
        config2.setPartitionCount(1);
        //设置配置参数
        BoneCP connectionPool2 = new BoneCP(config2); // setup the connection pool
        
        pools.put(DatabaseConfig.PAY_NAME, connectionPool2);
        
	}
 

 

你可能感兴趣的:(driver)