本帖最后由 iphilip 于 2014-02-17 18:05:11 编辑

解决方案 »

  1.   

     DBPool pool = DBPool.getInstance();这个是取什么?DBPool是静态类的吧?工具类取conn一般不是DBPool.getConnection()的吗?在jsp内操作数据库其实是非常不可取的》
      

  2.   

    DBPool不是静态类,只是有个静态方法,源码下面贴出来了,用的开源的包。
    我在论坛问这个问题不出讨论这样设计是否合理,当然这也很重要。我的主要问题是Eclipse里面这个jsp页面就无法打开,可能是引用自己写的这个类的路径有问题,也不知道怎么在Eclipse里面设置,后面我变通的把自己写的类打包后,再引入进来,解决这个问题了。但是这样调试不是很方便!public class DBPool {
    private static DBPool dbPool;
    private ComboPooledDataSource dataSource; static {
    dbPool = new DBPool();
    } public DBPool() {
    try {
    System.out.println("OK");
    dataSource = new ComboPooledDataSource();
    dataSource.setUser("root");
    dataSource.setPassword("mypwd");
    dataSource.setDriverClass( "com.mysql.jdbc.Driver" );
    dataSource.setJdbcUrl( "jdbc:mysql://localhost/mybase" ); 
    // 设置初始连接池的大小!
    dataSource.setInitialPoolSize(2);
    // 设置连接池的最小值! 
    dataSource.setMinPoolSize(1);
    // 设置连接池的最大值! 
    dataSource.setMaxPoolSize(10);
    // 设置连接池中的最大Statements数量!
    dataSource.setMaxStatements(50);
    // 设置连接池的最大空闲时间!
    dataSource.setMaxIdleTime(60);
    } catch (PropertyVetoException e) {
    e.printStackTrace();
    throw new RuntimeException(e);
    }
    } public final static DBPool getInstance() {
    return dbPool;
    } public final Connection getConnection() {
    try {
    return dataSource.getConnection();
    } catch (SQLException e) {
    throw new RuntimeException("无法从数据源获取连接 ", e);
    }
    } public static void main(String[] args) throws SQLException {
    Connection con = null;
    try {
    con = DBPool.getInstance().getConnection();
    ResultSet rs = con.createStatement().executeQuery("SELECT * FROM users");
    while (rs.next()) {
    System.out.println(rs.getObject(1));
    System.out.println(rs.getObject(2));
    System.out.println(rs.getObject(3));
    }
    } catch (Exception e) {
    } finally {
    if (con != null)
    con.close();
    }
    }
    }
      

  3.   

    明显是找不到class,你的类class放的位置检查一下。
      

  4.   

    是的,找不到类。用的Eclipse(eclipse-jee-kepler-SR1-win32-x86_64)IDE,就是不知道怎么指定路径。所以只好变通的把自己写的类打包,然后右击工程 设置build path--Add External jar, 这样显然比较麻烦,不知道怎样能直接设置下。
      

  5.   

    检查一下类 cn/mysite/DBPool 的路径,或者有没有打包进来
      

  6.   

    确认项目cn/mysite/DBPool这个类是否存在
      

  7.   

    可能你DBPool class大小写错了...