demo1运行出错

demo1运行出错

http://img1.sycdn.imooc.com//climg/5fe470c00936037c25601408.jpghttp://img1.sycdn.imooc.com//climg/5fe470b209471da222991302.jpg

http://img1.sycdn.imooc.com//climg/5fe470c8096c3a0310750612.jpg

已经试过了添加finally但没有解决问题

正在回答

登陆购买课程后可参与讨论,去登陆

2回答

同学你好,

1、图一中同学这里的报错信息是如下这样吗?

http://img1.sycdn.imooc.com//climg/5fe4730809dbdd3511420044.jpg

Client does not support authentication protocol requested by server; consider upgrading MySQL client

如果是,可能是MySQL版本的原因,这里同学可以打开cmd窗口,使用mysql --version 命令查看自己的MySQL版本:

http://img1.sycdn.imooc.com//climg/5fe473d709bd56bb05450041.jpg

如果是8开头的版本,可以参考教辅区的

http://img1.sycdn.imooc.com//climg/5fe473f809bd0b8a03930070.jpg

来修改自己的代码

2、图二报错是因为同学重复定义,这里将下图黄色框里的内容删除即可

http://img1.sycdn.imooc.com//climg/5fe4746c09d09ae905200250.jpg

3、建议同学下次将代码以及报错信息完整贴出,便于老师复制运行(不是截图哟~是直接复制到"我要回答"中即可)

祝学习愉快~

  • 孫瑪戈 提问者 #1
    package com.imooc.jdbc.demo1;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    import org.junit.Test;

    import com.mysql.jdbc.Driver;

    public class JDBCDemo1 {

    @Test
    /**
    * JDBC资源的释放
    */
    public void demo1() {
    try {
    // 1.加载驱动
    DriverManager.registerDriver(new Driver());
    // 2.获得连接
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "abc");
    // 3.1创建执行SQL语句的对象,并执行SQL
    String sql = "select * from user";
    Statement stmt = conn.createStatement();
    // 3.2执行sql
    ResultSet resultSet = stmt.executeQuery(sql);
    while (resultSet.next()) {
    int uid = resultSet.getInt("uid");
    String username = resultSet.getString("username");
    String password = resultSet.getString("password");
    String name = resultSet.getString("name");

    System.out.println(uid + " " + username + " " + password + " " + name);
    }
    // 4.释放资源
    resultSet.close();
    stmt.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }

    老师还是不行,错误如下

    Thu Dec 24 19:12:23 CST 2020 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
    java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:127)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.imooc.jdbc.demo1.JDBCDemo1.demo1(JDBCDemo1.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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)

    已经导入了新的jar包http://img1.sycdn.imooc.com//climg/5fe4782b09a6987803670336.jpg

    2020-12-24 19:14:54
提问者 孫瑪戈 2020-12-24 19:16:26

老师还是不行,已经导入jar包http://img1.sycdn.imooc.com//climg/5fe4782b09a6987803670336.jpg

package com.imooc.jdbc.demo1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;

import com.mysql.jdbc.Driver;

public class JDBCDemo1 {

@Test
/**
* JDBC资源的释放
*/
public void demo1() {
try {
// 1.加载驱动
DriverManager.registerDriver(new Driver());
// 2.获得连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "abc");
// 3.1创建执行SQL语句的对象,并执行SQL
String sql = "select * from user";
Statement stmt = conn.createStatement();
// 3.2执行sql
ResultSet resultSet = stmt.executeQuery(sql);
while (resultSet.next()) {
int uid = resultSet.getInt("uid");
String username = resultSet.getString("username");
String password = resultSet.getString("password");
String name = resultSet.getString("name");

System.out.println(uid + " " + username + " " + password + " " + name);
}
// 4.释放资源
resultSet.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
​Thu Dec 24 19:12:23 CST 2020 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:127)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.imooc.jdbc.demo1.JDBCDemo1.demo1(JDBCDemo1.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
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)


  • 提问者 孫瑪戈 #1

    搞定解决了

    2020-12-24 21:39:09
  • 好帮手慕小脸 回复 提问者 孫瑪戈 #2

    解决问题就好,棒棒的!继续加油~

    祝学习愉快~

    2020-12-25 09:51:38
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师