gpt4 book ai didi

java - 无法连接到 MySql 数据库

转载 作者:搜寻专家 更新时间:2023-10-30 23:17:53 24 4
gpt4 key购买 nike

<分区>

我正在尝试测试与我的数据库的连接,但我不断收到以下错误

java.sql.SQLException: Access denied for user 'db'@'localhost' (using password: YES)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2975)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:798)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3700)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1203)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2572)
at com.mysql.jdbc.Connection.<init>(Connection.java:1485)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at ConnectingToDB.main(ConnectingToDB.java:18)

我的代码如下。错误发生在线路上“conn = DriverManager.getConnection(url+dbName,userName,password);”

public class ConnectingToDB
{
public static void main(String[] args)
{
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "steven";
String driver = "com.mysql.jdbc.Driver";
String userName = "db";
String password = "db";
try
{
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
}
catch (Exception e) {
e.printStackTrace();
}
}

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