import java.sql.Connection;
import java.sql.SQLException;import com.mchange.v2.c3p0.ComboPooledDataSource;public class DatabaseAccessHelper { private static ComboPooledDataSource dataSource; static {
try {
dataSource = new ComboPooledDataSource(); //dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/pb");
dataSource.setUser("root");
dataSource.setPassword("123456"); dataSource.setMinPoolSize(3);
dataSource.setMaxPoolSize(20);
dataSource.setInitialPoolSize(5);
dataSource.setNumHelperThreads(5); dataSource.setCheckoutTimeout(500);
dataSource.setAutoCommitOnClose(false); dataSource.setMaxIdleTime(18000);
dataSource.setIdleConnectionTestPeriod(3600);
dataSource.setTestConnectionOnCheckin(true);
dataSource.setPreferredTestQuery("select count(*) from systemurl"); } catch (Exception e) {
throw new RuntimeException(e);
}
} public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
} public static void close() {
dataSource.close();
}
}注意注释部分,给或不给或给错误的驱动,C3P0均能正确连接,求解。

解决方案 »

  1.   

    "给或不给或给错误的驱动"你是说不添加jar包还是不写注释那行啊如果不写注释那行的话,貌似和servlet3的特性一样吧,之前的servlet连db都要有Class.forName();但是servlet3的话就不用写了,会自动搜索并加载。如果是不给jar包也能行,就不知道啦水平有限,仅供参考
      

  2.   

    c3p0很容易出现比较怪异的事情,可能是出来的时间早吧,,,,有时会出现查询根本不是你想要的结果出来,,建议用dbcp的方式来连接
      

  3.   

    大概不是缓存,是会自动查找jdbc驱动
      

  4.   

    JDBC 4 引入的新特性■ Automatic loading of java.sql.Driver
    DriverManager.getConnection has been modified to utilize the Java SE 
    Service Provider mechanism to automatically load JDBC Drivers. This removes 
    the need to invoke Class.forName.简单的说,所有的java.sql.Diver在加载的时候,会到DriverManager注册一下。程序调用DriverManager.getConnection的时候,DriverManager会拿connnectionUrl到所有注册过的Driver上挨个试,直到成功得到Connection对象。