eclipse运行报错

eclipse运行报错

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(DriverManager.java:664)

at java.sql.DriverManager.getConnection(DriverManager.java:247)

at com.imooc.jdbc.demo1.test2.demo(test2.java:20)

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:59)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)

at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)

at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)

at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)

at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)

at org.junit.runners.ParentRunner.run(ParentRunner.java:413)

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:542)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)


老师,请问出现这个情况是什么问题?root localhost 没接到吗? 

正在回答 回答被采纳积分+1

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

2回答
提问者 勤劳学生 2020-07-13 13:40:54

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;


import org.junit.Test;




public class JDBCDemo1 {

  @Test

  /**

   * JDBC 入门程序

   */

  public void demo1() {

  Connection conn=null;

  Statement stmt =null;

  ResultSet rs=null;

  

  //加载驱动

  try {

//DriverManager.registerDriver(new Driver());

  Class.forName("com.mysql.cj.jdbc.Driver");

//获得连接

conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/Database_dbName?allowPublicKeyRetrieval=true&useSSL=false", "root","123456");

//创建执行SQL语句的对象,并且执行SQL

  String sql="select * from user";

   stmt =conn.createStatement();

  

   rs=stmt.executeQuery(sql);

  while(rs.next()) {

  int uid=rs.getInt("uid");

  String username =rs.getString("username");

  String password=rs.getString("password");

  String name=rs.getString("name");

  

  System.out.println(uid+" "+username+" "+password+" "+name);

  }

    

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

//释放资源

if(rs!=null) {

try {

rs.close();

} catch (SQLException sqlEx) {

// TODO Auto-generated catch block

}

rs=null;

}

  if(stmt!=null) {

  try {

stmt.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

}

  stmt=null;

  }

if(conn!=null) {

try {

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

conn=null;

}

  

}

  

  }

}


java.sql.SQLSyntaxErrorException: Unknown database 'database_dbname'

老师 我做了修正错误 然后并且把 mysql 8.0.16jar 的build path 

可是 出现了 dbname 报错 ,请老师帮忙


  • 同学你好,测试同学的代码是可以的,但根据报错信息提示:是没有database_dbname这个数据库,同学先检查一下是否创建了数据库。或者数据库的名字是否有拼写错误呢? 祝学习愉快~
    2020-07-13 13:53:03
好帮手慕小脸 2020-07-12 17:32:11

同学你好!报错信息:连接数据库的密码错误

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

建议同学检查一下数据库密码是否正确呢?如果不正确,修改正确后再试试

祝学习愉快~

  • 提问者 勤劳学生 #1
    Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure 数据库出现来这个 说是数据库 是 cj.jdbc 这个如何更改呢??
    2020-07-13 11:17:15
  • 提问者 勤劳学生 #2
    com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Client does not support authentication protocol requested by server; consider upgrading MySQL client 就算重新弄了也出现这个 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 { DriverManager.registerDriver(new Driver()); //获得连接 Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest", "root","kk644219"); //创建执行SQL语句的对象,并且执行SQL String sql="select * from user"; Statement stmt =conn.createStatement(); 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); } //释放资源 resultSet.close(); stmt.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
    2020-07-13 12:06:28
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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