正在回答 回答被采纳积分+1
4回答
qq_慕容1283338
2019-10-19 16:14:23
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | package com.imooc.jdbcUtils; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; /* * JDBC的工具类 * @author hj */ public class JDBCUtils { private static final String driverClass; private static final String url; private static final String username; private static final String password; static { //加载属性文件并解析 Properties props = new Properties(); //通常情况下使用类的加载器的方式获取属性文件的输入流 InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"); try { props.load(is); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } driverClass = props.getProperty("driverClass"); url = props.getProperty("url"); username = props.getProperty("username"); password = props.getProperty("password"); } /* * 注册驱动的方法 */ public static void loadDriver() throws ClassNotFoundException { Class.forName(driverClass); } /* * 获得连接的方法: */ public static Connection getConnection() throws Exception { loadDriver(); Connection con = DriverManager.getConnection(url, username, password); System.out.println(username+" "+password); return con; } /* * 资源释放 */ public static void release(Statement s,Connection con) { if (s != null ) { try { s.close(); } catch (SQLException e) { e.printStackTrace(); } s = null ; } if (con != null ) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } con = null ; } } public static void release(Statement s,Connection con,ResultSet rs) { if (s != null ) { try { s.close(); } catch (SQLException e) { e.printStackTrace(); } s = null ; } if (rs != null ) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } rs = null ; } if (con != null ) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } con = null ; } } } |
配置文件
1 2 3 4 | driverClass=com.mysql.cj.jdbc.Driver url=jdbc:mysql: //localhost:3306/jdbctest?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true username=root password= 1234 |
我用下面这种方式代替调用工具类中的函数,就能运行成功:
这是怎么回事啊,确信用户密码真的没有错!
qq_慕容1283338
2019-10-19 10:58:00
加上之后又报下面的错误了。。。。。。
相似问题
登录后可查看更多问答,登录/注册
3. Java 数据库开发与实战应用
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧