运行错误。。
package com.imooc.jdbc.utils; 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 51768 * */ 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 inputStream = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"); try{ props.load(inputStream); }catch(Exception e) { e.printStackTrace(); } driverClass = props.getProperty("driverClass"); url = props.getProperty("url"+"?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8"); username = props.getProperty("username"); password = props.getProperty("password"); } /** * 注册驱动方法 * @throws ClassNotFoundException */ public static void loadDriver() throws ClassNotFoundException { Class.forName(driverClass); } /** * 获得连接方法 * @throws Exception */ public static Connection getConnection() throws Exception { loadDriver(); Connection con = DriverManager.getConnection(url,username,password); return con; } /** * 资源释放方法 */ public static void release(Statement stat,Connection con) { if(stat != null) { try { stat.close(); } catch (SQLException e) { e.printStackTrace(); } stat = null; } if(con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } con = null; } } public static void release(ResultSet rs,Statement stat,Connection con) { if(rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } rs = null; } if(stat != null) { try { stat.close(); } catch (SQLException e) { e.printStackTrace(); } stat = null; } if(con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } con = null; } } }
————————————————————————————————————————————
driverClass = com.mysql.cj.jdbc.Driver url = jdbc:mysql://localhost:3306/goodstest username = root password = 0923 —————————————————————————————————————————————————————————————— package com.imooc.jdbc.demo1; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import org.junit.Test; import com.imooc.jdbc.utils.JDBCUtils; public class demo2 { @Test public void demo() { Connection con = null; Statement stat = null; ResultSet rs = null; try { //建立连接(同时加载驱动) con = JDBCUtils.getConnection(); //创建执行SQL语句的对象 stat = con.createStatement(); //创建SQL语句 String sql = "insert goods values(null,'ipad',3300.0,'2019款')"; int i = stat.executeUpdate(sql); if(i > 0) { System.out.println("保存成功!"); //创建查询SQL语句 String sql1 = "select * from goods"; rs = stat.executeQuery(sql1); while(rs.next()) { System.out.println(rs.getInt("id")+" "+rs.getString("name")+" "+rs.getFloat("price")+" "+rs.getString("desp")); } }else { System.out.println("保存失败!"); } }catch(Exception e){ e.printStackTrace(); }finally { //释放资源 JDBCUtils.release(rs, stat, con); } } } ———————————————————————————————————————————————————————————————— java.sql.SQLException: Access denied for user 'root;'@'localhost' (using password: YES) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835) at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455) at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240) at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at com.imooc.jdbc.utils.JDBCUtils.getConnection(JDBCUtils.java:50) at com.imooc.jdbc.demo1.demo2.demo(demo2.java:19) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
24
收起
正在回答
3回答
同学你好,建议同学将url完整的写在jdbc.properties配置文件中,如:
在JDBCUtils类中直接获取url再试试,如:
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
敏捷丶
2019-12-19 20:22:17
java.sql.SQLException: The url cannot be null at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at com.imooc.jdbc.utils.JDBCUtils.getConnection(JDBCUtils.java:51) at com.imooc.jdbc.demo1.demo2.demo(demo2.java:20) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
好帮手慕小班
2019-12-19 17:27:35
同学你好,根据同学贴出的错误信息:java.sql.SQLException: Access denied for user 'root;'@'localhost' (using password: YES)--》 数据库root没有成功登录。
出现这样错误的原因可能有:
1、用户名和密码不匹配,比如:在连接池里密码后多打了一个空格。
2、在配置文件中拼接数据时,建议这样书写:
url = props.getProperty("url"+"?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8");
3、检查一下编码的内容,编码是否一致。
这里测试代码后,可以正常连接操作数据库。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
3. Java 数据库开发与实战应用
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星