老师,帮我看看这个
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 | package com.ht.JDBC.DemoOne; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import org.junit.Test; public class JDBCTest { @Test public void test() { Connection con = null ; Statement stmt = null ; ResultSet rs = null ; try { //注册驱动,调用工具类 Class.forName(JDBCUntil.driverClass); con = DriverManager.getConnection(JDBCUntil.url,JDBCUntil.root,JDBCUntil.password); stmt = con.createStatement(); String insertSql = "insert test2(name,price,desp) values('一加手机',3600,'Oneplus')" ; int i = stmt.executeUpdate(insertSql); if (i> 0 ) { String resultSet = "select * from test2" ; rs = stmt.executeQuery(resultSet); while (rs.next()) { int id = rs.getInt( "id" ); String name = rs.getString( "name" ); float price = rs.getFloat( "price" ); String desp = rs.getString( "desp" ); System.out.println(id+ " " +name+ " " +price+ " " +desp); } } } catch (Exception e) { e.printStackTrace(); } finally { JDBCUntil.release(con, stmt, rs); } } } |
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 | package com.ht.JDBC.DemoOne; 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; public class JDBCUntil { public static final String driverClass; public static final String url; public static final String root; public static final String password; static { //加载文件属性并解析 Properties pro = new Properties(); //如何获得文件输入流的属性 //通常情况下使用类的加载器的方式进行获取 InputStream iso = JDBCUntil. class .getClassLoader().getResourceAsStream( "jdbc.properties" ); try { pro.load(iso); } catch (IOException e) { e.printStackTrace(); } driverClass =pro.getProperty( "driverClass" ); // "com.mysql.cj.jdbc.Driver";//proper.getProperty("driverClass"); url = pro.getProperty( "url" ); //"jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8";//proper.getProperty("url"); root = pro.getProperty( "root" ); //"root";// proper.getProperty("root"); password = pro.getProperty( "password" ); //"harden1998";// proper.getProperty("password"); } /** * 注册驱动的方法 * @throws Exception */ public static void loadDriver() throws Exception { Class.forName( "com.mysql.cj.jdbc.Driver" ); } /** * 获得连接的方法 * @throws Exception */ public static Connection getConnection(Connection con) throws Exception { loadDriver(); //加载驱动↑方法 con = DriverManager.getConnection(url,root,password); return con ; } /** * 资源的释放 */ public static void release(Connection con,Statement stmt) { if (con != null ) { try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } con = null ; } if (stmt != null ) { try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } stmt = null ; } } public static void release(Connection con, Statement stmt,ResultSet rs ) { if (rs != null ) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } rs = null ; } if (con != null ) { try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } con = null ; } if (stmt != null ) { try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } stmt = null ; } } } |
driverClass = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8
root = root
password = 1234
4
收起
正在回答 回答被采纳积分+1
4回答
芝芝兰兰
2019-10-10 10:13:31
同学你好。报错信息是com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Public Key Retrieval is not allowed
建议将url后添加 allowPublicKeyRetrieval=true 然后再试一下~
1 | url = jdbc:mysql: //localhost:3306/test?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true |
如果解答了同学的疑问,望采纳~
祝学习愉快~
3. Java 数据库开发与实战应用
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧