gpt4 book ai didi

java - 线程中的异常 "main"java.sql.SQLException : Access denied for user '' @'localhost' (using password: NO)

转载 作者:行者123 更新时间:2023-11-29 09:28:16 24 4
gpt4 key购买 nike

Exception in thread "main" java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)
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:836)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
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.mysql.cj.jdbc.admin.TimezoneDump.main(TimezoneDump.java:70)

无论我做什么,我都会收到此错误。我使用 MySQL Workbench,我更改了 root 用户的密码,我已向用户 root 授予所有权限,我尝试断开连接并再次连接到数据库。没什么,无论我做什么。你能帮我解决这个问题吗?我不知道还能做什么。密码和用户名均正确。

Java 代码:

package Restaurant;

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


public class MainApp {
public void main(String[] args) throws SQLException
{

String url="jdbc:mysql://localhost:3306/new_schema";
Statement sql;

ResultSet rs;
Connection con=DriverManager.getConnection(url, "root", "root");

sql=(Statement)con.createStatement();
rs=sql.executeQuery("select * from restaurant");
while(rs.next())
{
System.out.println("Nume: "+rs.getString("nume")+", Specific: "+rs.getString("specificul")+", Zona: "+rs.getString("zona"));
}


rs.close();
sql.close();
con.close();
}

}

Workbench

最佳答案

您的 eclipse 正在运行类“com.mysql.cj.jdbc.admin.TimezoneDump”(也包含一个 main 方法)而不是您的 MainApp 类。

尝试这个操作:

Right click on MainApp.java -> Run As -> Java Application

关于java - 线程中的异常 "main"java.sql.SQLException : Access denied for user '' @'localhost' (using password: NO),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59225429/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com