NetBeans 表示用户 'root'@'localhost' 的访问被拒绝(使用密码:是)
NetBeans says that Access denied for user 'root'@'localhost' (using password: YES)
我是 java 的新手,正在开发中。我正在使用 NetBeans 8.0 和查询浏览器 1.1.2 创建数据库和表。输入用户详细信息后点击登录按钮后,一条消息显示如下
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
我使用查询浏览器创建了数据库,因为我是 MYSQL workbench 的新手,而不是查询浏览器。
下面是Home_Page代码
public class Home_Page extends javax.swing.JFrame {
public String User;
Model.JDBC j=new JDBC();
public Home_Page(String U_ID) {
initComponents();
User=U_ID;
Toolkit t = Toolkit.getDefaultToolkit();
Dimension d = t.getScreenSize();
int w = (int) d.getWidth();
int h = (int) d.getHeight()-40;
setBounds(0, 0, w, h);
jLabel2.setText(new Controler.getDate().date());
time();
jLabel3.setText(U_ID);
ResultSet rs;
try {
rs = j.getData("select ulevel from user U_ID='"+U_ID+"'");
if(rs.next()==true){
if(rs.getString(1).equals("Admin")){
home.setEnabled(true);
EMPLOYEE.setEnabled(true);
employee.setEnabled(true);
user.setEnabled(true);
machine.setEnabled(true);
branch.setEnabled(true);
}else if(rs.getString(1).equals("user")){
home.setEnabled(true);
EMPLOYEE.setEnabled(false);
employee.setEnabled(false);
user.setEnabled(false);
machine.setEnabled(false);
branch.setEnabled(false);
}else{
System.out.println("else");
}
}
} catch (Exception ex) {
Logger.getLogger(Home_Page.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void time() {
new Thread(new Runnable() {
public void run() {
while (true) {
String times = new Date().toString();
String h = times.split(" ")[3];
jLabel1.setText(h);
try {
Thread.sleep(60);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
}
@SuppressWarnings("unchecked")
private void branchActionPerformed(java.awt.event.ActionEvent evt) {
jDesktopPane1.removeAll();
Branch b=new Branch();
jDesktopPane1.add(b);
b.setVisible(true);
jDesktopPane1.updateUI();
// TODO add your handling code here:
}
public void clear() {
this.dispose();
}
private void logoutActionPerformed(java.awt.event.ActionEvent evt) {
java.awt.Toolkit.getDefaultToolkit().beep(); int t = JOptionPane.showConfirmDialog(null, "Are You Sure...?");
if(t==JOptionPane.YES_OPTION){
this.dispose();
clear();
new Viewer.login().setVisible(true);
}
else if(t==JOptionPane.NO_OPTION)
{
}
else
{
}
}
private void userActionPerformed(java.awt.event.ActionEvent evt) {
jDesktopPane1.removeAll();
User_ u=new User_();
jDesktopPane1.add(u);
u.setVisible(true);
jDesktopPane1.updateUI();
// TODO add your handling code here:
}
private void employeeActionPerformed(java.awt.event.ActionEvent evt) {
jDesktopPane1.removeAll();
Employee_ emp=new Employee_();
jDesktopPane1.add(emp);
emp.setVisible(true);
jDesktopPane1.updateUI();
// TODO add your handling code here:
}
private void machineActionPerformed(java.awt.event.ActionEvent evt) {
jDesktopPane1.removeAll();
machine_ m = new machine_();
jDesktopPane1.add(m);
m.setVisible(true);
jDesktopPane1.updateUI();
// TODO add your handling code here:
}
private void homeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void EMPLOYEEActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
public static void main(String args[]) {
Calendar expiry = Calendar.getInstance();
expiry.set(2013, 9 , 27,0,0); // Expire at 31 Jan 2010
Calendar now = Calendar.getInstance();
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Home_Page(" ").setVisible(true);
}
});
}
}
下面是我的登录表单代码
public class login extends javax.swing.JFrame {
public login() {
initComponents();
}
@SuppressWarnings("unchecked")
private void loginActionPerformed(java.awt.event.ActionEvent evt) {
if (eid.getText().trim().length() == 0 | pwd.getPassword() == null) {
JOptionPane.showMessageDialog(null, "Username or Password cannot be blank", "Error", 0);
} else {
try {
ResultSet rs = new DBG1().getData("select * from user where eid = '" +eid.getText() + "'");
Vector v = new Vector();
if (rs.next()) {
v.add(rs.getString(1));
}
if (v.isEmpty()) {
JOptionPane.showMessageDialog(null, "Invalid Username or Password", "Error", 0);
eid.setText("");
pwd.setText("");
eid.grabFocus();
} else {
ResultSet rs1 = new DBG1().getData("select * from user where eid ='" + eid.getText() + "'");
while (rs1.next()) {
String type = (rs1.getString(3));
if (eid.getText().trim().equalsIgnoreCase(rs1.getString("eid")) && pwd.getText().equals(rs1.getString("pwd")) && type.equals("Admin")) {
this.dispose();
new Home_Page(eid.getText()).setVisible(true);
// new Home_Page(eid.getText()).setVisible(true);
} else if (eid.getText().trim().equalsIgnoreCase(rs1.getString("eid")) && pwd.getText().equals(rs1.getString("pwd")) && type.equals("user")) {
this.dispose();
new Home_Page(eid.getText()).setVisible(true);
}
else {
JOptionPane.showMessageDialog(null, "Invalid Username or Password", "Error", 0);
eid.setText("");
pwd.setText("");
eid.grabFocus();
}
}
this.dispose();
}
} catch (Exception e) {
System.out.println(e);
}
}
// TODO add your handling code here:
}
private void cancelActionPerformed(java.awt.event.ActionEvent evt) {
new login().dispose();
System.out.println(pwd.getPassword());
System.out.println("Cleared");
this.dispose();
// TODO add your handling code here:
}
private void eidActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Viewer.login().setVisible(true);
}
});
}
}
当我调试项目时,它应该首先 运行 加载 window(此处未包含)然后登录 window.Once 我输入详细信息并按下登录按钮, 以上错误显示在 "POST_NEW debug panel"
我已经研究并遵循了在此之前发布的所有类似类型问题中提到的所有说明 site.Yet 我在这个 condition.Could 任何人请指出我应该在哪里修改我的代码或我应该如何修改摆脱这个错误并进入我的项目Home_Page
确保你的数据库名称是"root"并且你的数据库密码是否为空...并正确设置数据库身份验证以进行授权..
尝试使用用户名 root 和密码为空。如果这不起作用,您可以试试这个:Access denied for user 'username'@'localhost' (using password: YES) in C:\webdev\wamp\www\membershipSite\classes\Mysql.php on line 9
终于解决了。 :)
发生的事情是,我将 "root" 这个词作为 "root" 用户名的密码。当我将密码更改为 "root" 时,即使出现另一个错误,我也可以继续前进。我现在正在努力。
再次感谢大家。 :)
代码如下
public Connection getCon() throws Exception{
String driver ="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/FINAL_POST?useUnicode=true&characterEncoding=UTF-8";
Class.forName(driver);
Connection con=DriverManager.getConnection(url, "root", "root");
return con;
}
运行 Netbeans as Administrator 因为 netbeans 不是 mysql 在 Current User Preveligous 中,
谢谢
我是 java 的新手,正在开发中。我正在使用 NetBeans 8.0 和查询浏览器 1.1.2 创建数据库和表。输入用户详细信息后点击登录按钮后,一条消息显示如下
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
我使用查询浏览器创建了数据库,因为我是 MYSQL workbench 的新手,而不是查询浏览器。
下面是Home_Page代码
public class Home_Page extends javax.swing.JFrame {
public String User;
Model.JDBC j=new JDBC();
public Home_Page(String U_ID) {
initComponents();
User=U_ID;
Toolkit t = Toolkit.getDefaultToolkit();
Dimension d = t.getScreenSize();
int w = (int) d.getWidth();
int h = (int) d.getHeight()-40;
setBounds(0, 0, w, h);
jLabel2.setText(new Controler.getDate().date());
time();
jLabel3.setText(U_ID);
ResultSet rs;
try {
rs = j.getData("select ulevel from user U_ID='"+U_ID+"'");
if(rs.next()==true){
if(rs.getString(1).equals("Admin")){
home.setEnabled(true);
EMPLOYEE.setEnabled(true);
employee.setEnabled(true);
user.setEnabled(true);
machine.setEnabled(true);
branch.setEnabled(true);
}else if(rs.getString(1).equals("user")){
home.setEnabled(true);
EMPLOYEE.setEnabled(false);
employee.setEnabled(false);
user.setEnabled(false);
machine.setEnabled(false);
branch.setEnabled(false);
}else{
System.out.println("else");
}
}
} catch (Exception ex) {
Logger.getLogger(Home_Page.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void time() {
new Thread(new Runnable() {
public void run() {
while (true) {
String times = new Date().toString();
String h = times.split(" ")[3];
jLabel1.setText(h);
try {
Thread.sleep(60);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
}
@SuppressWarnings("unchecked")
private void branchActionPerformed(java.awt.event.ActionEvent evt) {
jDesktopPane1.removeAll();
Branch b=new Branch();
jDesktopPane1.add(b);
b.setVisible(true);
jDesktopPane1.updateUI();
// TODO add your handling code here:
}
public void clear() {
this.dispose();
}
private void logoutActionPerformed(java.awt.event.ActionEvent evt) {
java.awt.Toolkit.getDefaultToolkit().beep(); int t = JOptionPane.showConfirmDialog(null, "Are You Sure...?");
if(t==JOptionPane.YES_OPTION){
this.dispose();
clear();
new Viewer.login().setVisible(true);
}
else if(t==JOptionPane.NO_OPTION)
{
}
else
{
}
}
private void userActionPerformed(java.awt.event.ActionEvent evt) {
jDesktopPane1.removeAll();
User_ u=new User_();
jDesktopPane1.add(u);
u.setVisible(true);
jDesktopPane1.updateUI();
// TODO add your handling code here:
}
private void employeeActionPerformed(java.awt.event.ActionEvent evt) {
jDesktopPane1.removeAll();
Employee_ emp=new Employee_();
jDesktopPane1.add(emp);
emp.setVisible(true);
jDesktopPane1.updateUI();
// TODO add your handling code here:
}
private void machineActionPerformed(java.awt.event.ActionEvent evt) {
jDesktopPane1.removeAll();
machine_ m = new machine_();
jDesktopPane1.add(m);
m.setVisible(true);
jDesktopPane1.updateUI();
// TODO add your handling code here:
}
private void homeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void EMPLOYEEActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
public static void main(String args[]) {
Calendar expiry = Calendar.getInstance();
expiry.set(2013, 9 , 27,0,0); // Expire at 31 Jan 2010
Calendar now = Calendar.getInstance();
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Home_Page(" ").setVisible(true);
}
});
}
}
下面是我的登录表单代码
public class login extends javax.swing.JFrame {
public login() {
initComponents();
}
@SuppressWarnings("unchecked")
private void loginActionPerformed(java.awt.event.ActionEvent evt) {
if (eid.getText().trim().length() == 0 | pwd.getPassword() == null) {
JOptionPane.showMessageDialog(null, "Username or Password cannot be blank", "Error", 0);
} else {
try {
ResultSet rs = new DBG1().getData("select * from user where eid = '" +eid.getText() + "'");
Vector v = new Vector();
if (rs.next()) {
v.add(rs.getString(1));
}
if (v.isEmpty()) {
JOptionPane.showMessageDialog(null, "Invalid Username or Password", "Error", 0);
eid.setText("");
pwd.setText("");
eid.grabFocus();
} else {
ResultSet rs1 = new DBG1().getData("select * from user where eid ='" + eid.getText() + "'");
while (rs1.next()) {
String type = (rs1.getString(3));
if (eid.getText().trim().equalsIgnoreCase(rs1.getString("eid")) && pwd.getText().equals(rs1.getString("pwd")) && type.equals("Admin")) {
this.dispose();
new Home_Page(eid.getText()).setVisible(true);
// new Home_Page(eid.getText()).setVisible(true);
} else if (eid.getText().trim().equalsIgnoreCase(rs1.getString("eid")) && pwd.getText().equals(rs1.getString("pwd")) && type.equals("user")) {
this.dispose();
new Home_Page(eid.getText()).setVisible(true);
}
else {
JOptionPane.showMessageDialog(null, "Invalid Username or Password", "Error", 0);
eid.setText("");
pwd.setText("");
eid.grabFocus();
}
}
this.dispose();
}
} catch (Exception e) {
System.out.println(e);
}
}
// TODO add your handling code here:
}
private void cancelActionPerformed(java.awt.event.ActionEvent evt) {
new login().dispose();
System.out.println(pwd.getPassword());
System.out.println("Cleared");
this.dispose();
// TODO add your handling code here:
}
private void eidActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Viewer.login().setVisible(true);
}
});
}
}
当我调试项目时,它应该首先 运行 加载 window(此处未包含)然后登录 window.Once 我输入详细信息并按下登录按钮, 以上错误显示在 "POST_NEW debug panel" 我已经研究并遵循了在此之前发布的所有类似类型问题中提到的所有说明 site.Yet 我在这个 condition.Could 任何人请指出我应该在哪里修改我的代码或我应该如何修改摆脱这个错误并进入我的项目Home_Page
确保你的数据库名称是"root"并且你的数据库密码是否为空...并正确设置数据库身份验证以进行授权..
尝试使用用户名 root 和密码为空。如果这不起作用,您可以试试这个:Access denied for user 'username'@'localhost' (using password: YES) in C:\webdev\wamp\www\membershipSite\classes\Mysql.php on line 9
终于解决了。 :) 发生的事情是,我将 "root" 这个词作为 "root" 用户名的密码。当我将密码更改为 "root" 时,即使出现另一个错误,我也可以继续前进。我现在正在努力。 再次感谢大家。 :)
代码如下
public Connection getCon() throws Exception{
String driver ="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/FINAL_POST?useUnicode=true&characterEncoding=UTF-8";
Class.forName(driver);
Connection con=DriverManager.getConnection(url, "root", "root");
return con;
}
运行 Netbeans as Administrator 因为 netbeans 不是 mysql 在 Current User Preveligous 中, 谢谢