Java-Mysql - 用户“root"@“localhost"的访问被拒绝(使用密码:YES) [英] Java-Mysql - Access denied for user 'root'@'localhost' (using password: YES)

查看:205
本文介绍了Java-Mysql - 用户“root"@“localhost"的访问被拒绝(使用密码:YES)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用流动代码并收到此错误:

I am using the fllowing code and get this error:

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

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

I used several solutions from the stackoverflow. But the problem is not resolved! Please provide your solution.

只插入一次,但后来我遇到了上述错误.

我的 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); 在控制台上显示流动消息时:

Update: When i use e.printStackTrace(System.out); display the fllowing messages on console:

    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

推荐答案

我认为您的代码中没有任何内容会导致此类型错误,正如您在错误中看到的

i think there is nothing in your code that will cause this type error, as you can see in error

Access denied for user 'root'@'localhost' (using password: YES)

它表明您的用户或密码有问题,因此如果您使用数据库用户的密码,请再次检查您的用户和密码是否正确.

it shows that problem is with your user or password, so check once again that your user and password is correct if you are using password for database user.

这篇关于Java-Mysql - 用户“root"@“localhost"的访问被拒绝(使用密码:YES)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆