JSP中引用自己写的连接池类,编译出现java.lang.NoClassDefFoundError: cn.mysite.DBPool 异常 本帖最后由 iphilip 于 2014-02-17 18:05:11 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 DBPool pool = DBPool.getInstance();这个是取什么?DBPool是静态类的吧?工具类取conn一般不是DBPool.getConnection()的吗?在jsp内操作数据库其实是非常不可取的》 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(); } }} 明显是找不到class,你的类class放的位置检查一下。 是的,找不到类。用的Eclipse(eclipse-jee-kepler-SR1-win32-x86_64)IDE,就是不知道怎么指定路径。所以只好变通的把自己写的类打包,然后右击工程 设置build path--Add External jar, 这样显然比较麻烦,不知道怎样能直接设置下。 检查一下类 cn/mysite/DBPool 的路径,或者有没有打包进来 确认项目cn/mysite/DBPool这个类是否存在 可能你DBPool class大小写错了... <base href="<%=basePath%>"> 问题 struts配置问题,求助啊 初学者servlet web.xml配置文件问题请前辈们解答.O(∩_∩)O java可以做网页插件吗? 获取Session值 struts上传文件,客户端先压缩后再上传,可行吗? 使用AJAXRPC让javascript直接调用java方法 关于Jsp中的JAVABEAN 如何在jsp中实现打印 送100分!求下面循环问题的解决方案 动态加载一个未知的类??? struts2 文件下载问题
我在论坛问这个问题不出讨论这样设计是否合理,当然这也很重要。我的主要问题是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();
}
}
}