用户'root'@'localhost'的JDBC访问被拒绝 [英] JDBC Access denied for user 'root'@'localhost'

查看:390
本文介绍了用户'root'@'localhost'的JDBC访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对我来说这是一个错误. 我使用了正确的密码,因为这是我启动MySQL时输入的密码.

This is an error for me. I have used proper password as this is what I type when I start MySQL.

DefaultTableModel dtm=(DefaultTableModel) jTable1.getModel();
dtm.setRowCount(0);
try{
    Class.forName("java.sql.Driver");
    Connection c=DriverManager.getConnection("jdbc:mysql://localhost/shashvat","root","1234");
    Statement s=c.createStatement();       
    ResultSet r=s.executeQuery("select * from phasetests;");

    while(r.next()){
        Object arr[]={r.getString(1),r.getString(2),r.getString(3),r.getString(4)};
        dtm.addRow(arr);
    }
}catch(Exception e){
System.out.println(e.getMessage());   
}

异常消息:

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

我也尝试过这个: https://stackoverflow.com/a/17908407/5036731

问题仍然存在. 请帮忙.

The problem still persists. Please help.

推荐答案

您的代码应该是这样的.

Your code should be something like this.

DefaultTableModel dtm=(DefaultTableModel) jTable1.getModel();
dtm.setRowCount(0);
try{ 
    //Class.forName("java.sql.Driver"); This is wrong
    Class.forName("com.mysql.jdbc.Driver"); // Declare it like this..
    Connection c=DriverManager.getConnection("jdbc:mysql://localhost:3306/shashvat","root","1234"); // port number was missing
    Statement s=c.createStatement();       
    ResultSet r=s.executeQuery("select * from phasetests;");

    while(r.next()){
        Object arr[]={r.getString(1),r.getString(2),r.getString(3),r.getString(4)};
        dtm.addRow(arr);
    } 
}catch(Exception e){
System.out.println(e.getMessage());   
} 

看到我在这里提到的代码正在工作.我在本地检查.问题应该出在密码模式访问上,您可以参考以下内容.

See the code I mentioned here is working. I checked it locally. The problem should be with the password or schema access for which you can refer following.

用户"root" @'被拒绝访问本地主机"(使用密码:是)-没有权限?

如果特权有问题.通过执行以下命令,将所有特权授予root用户.

If it's a problem with privileges. Grant all privileges to root by executing following command.

GRANT ALL PRIVILEGES ON shashvat.* TO 'root'@'localhost' IDENTIFIED BY 'root'

错误1045(28000):访问权限拒绝用户"root" @"localhost"(使用密码:是)

这篇关于用户'root'@'localhost'的JDBC访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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