用户'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'