gpt4 book ai didi

Java-Mysql - 用户 'root' @'localhost' 的访问被拒绝(使用密码 : YES)

转载 作者:太空宇宙 更新时间:2023-11-03 11:52:13 25 4
gpt4 key购买 nike

<分区>

我正在使用以下代码并收到此错误:

Access denied for user 'root'@'localhost' (using password: YES)
java.lang.NullPointerException

我使用了 stackoverflow 中的几种解决方案。但是问题并没有解决!请提供您的解决方案。

只插入了一次,然后我遇到了上面的错误。

我的 Java 代码:

package DBConnection;
import java.sql.Connection;
import java.sql.DriverManager;
import com.mysql.jdbc.PreparedStatement;

public class Main {

public static void main(String[] args) throws Exception {
insert();
}

public static void insert() throws Exception{
final String var1 = "test1";
final String var2 = "تست2";
final String var3 = "test.com";
try{
Connection conn = getConnection();
PreparedStatement inserted = (PreparedStatement) conn.prepareStatement("INSERT INTO wpsjim_target_sites (site_name_en,site_name_fa,site_address) VALUES('"+var1+"','"+var2+"','"+var3+"')");
inserted.execute();
}catch(Exception e){
System.out.println(e);
}finally{
System.out.println("Insert Completed!");
}
}

public static Connection getConnection() throws Exception{
try{
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/simjin";
String username = "root";
String password = "rootpass";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,username,password);
System.out.println("Connected");
return conn;
} catch(Exception e){
System.out.println(e.getMessage());
}
return null;
}
}

更新:当我使用 e.printStackTrace(System.out); 在控制台上显示以下消息:

    java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:871)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1694)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1215)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2255)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2286)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2085)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at DBConnection.Main.getConnection(Main.java:34)
at DBConnection.Main.insert(Main.java:17)
at DBConnection.Main.main(Main.java:9)
java.lang.NullPointerException

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