老师,帮我看看这个
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); } } }
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 然后再试一下~
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积分~
来为老师/同学的回答评分吧
0 星