使用c3p0连接池无法连接数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public void demo1() { Connection connection = null ; PreparedStatement preparedStatement = null ; ResultSet resultSet = null ; try { //建立连接池 ComboPooledDataSource dateSource = new ComboPooledDataSource(); //建立连接 connection = dateSource.getConnection(); String sql = "select * from user" ; preparedStatement = connection.prepareStatement(sql); resultSet = preparedStatement.executeQuery(); while (resultSet.next()){ System.out.println(resultSet.getInt( "uid" )+resultSet.getString( "username" )+resultSet.getString( "password" )+resultSet.getString( "name" )); } } catch (Exception e) { e.printStackTrace(); } finally { //在使用连接池时已经自动将connection的销毁操作变成了归还操作 JDBCUtil.release(resultSet, preparedStatement, connection); } } |
1 2 3 4 5 6 7 8 9 10 11 12 | <? xml version = "1.0" encoding = "UTF-8" ?> < c3p0-config > < default-config > < property name = "driverClass" >com.mysql.jdbc.Driver</ property > < property name = "jdbcUrl" >jdbc:mysql://localhost:3306/jdbctest?useSSL=false</ property > < property name = "user" >root</ property > < property name = "password" >root</ property > < property name = "maxPoolSize" >20</ property > < property name = "initialPoolSize" >5</ property > </ default-config > </ c3p0-config > |
使用c3p0后一直报错没有合适驱动,尝试网上诸多方法后依旧无果,jar包重新添加依赖无用,希望老师解惑,谢谢!
1
收起
正在回答 回答被采纳积分+1
3回答
慕容5097141
2018-07-30 16:26:55
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | public class C3P0Util { private static final ComboPooledDataSource dataSource = new ComboPooledDataSource(); /** * 连接数据库 * @return connection * @throws SQLException */ public static Connection getConnection() throws SQLException { Connection connection = dataSource.getConnection(); return connection; } /** * 释放资源 */ public static void release(ResultSet rs,Statement statement,Connection connection){ if (rs!= null ){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } rs= null ; } if (statement!= null ){ try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } statement= null ; } if (connection!= null ){ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } connection= null ; } } public static void release(Statement statement,Connection connection){ if (statement!= null ){ try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } statement= null ; } if (connection!= null ){ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } connection= null ; } } } |
相似问题
登录后可查看更多问答,登录/注册
Java数据库开发与实战应用2018版
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧