简体   繁体   English

JAVA 中用户“root”@“localhost”(使用密码:YES)的访问被拒绝

[英]Access denied for user 'root'@'localhost' (using password: YES) in JAVA

Now I have seen a few of these questions here but my problem is a bit different.现在我在这里看到了一些这样的问题,但我的问题有点不同。 I have created a Create Account page and Login page.我创建了一个创建帐户页面和登录页面。 I have made a database in MYSQL where the various usernames and corresponding passwords are stored.我在 MYSQL 中创建了一个数据库,其中存储了各种用户名和相应的密码。 I created an account using the create account page and then checked through the Login page and saw that it was working.我使用创建帐户页面创建了一个帐户,然后检查登录页面并看到它正在工作。 But then I tried to write a code where automatically a table is created in MYSQL after the corresponding account is created.但是后来我尝试编写一个代码,在创建相应的帐户后,会在 MYSQL 中自动创建一个表。 But I get the above error.但我收到上述错误。 I will show you my Login and Create Account pages.我将向您展示我的登录和创建帐户页面。

LOGIN:-登录:-

import java.awt.*;

import java.awt.event.*;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

import javax.swing.*;

public class Login extends Frame implements ActionListener, WindowListener

{

    Label l1,l2,l3;

    JTextField t1;

    JPasswordField p1;

    JButton b0,b1;

    String url = "jdbc:mysql://localhost:3306/database1";

    String user = "root";

    String password = "Newyear2016!";

    char[] s2;

    String s1,s3;

    Login()

    {

        addWindowListener(this);

        setTitle("Login");

        setSize(500,500);

        setLayout(null);

        setVisible(true);

        l3 = new Label("Login");

        l3.setBounds(120,50,300,50);

        add(l3);

        Font myFont1 = new Font("Helvetica",Font.ITALIC,30);

        l3.setFont(myFont1);

        l1 = new Label("User Name: ");

        l1.setBounds(50,150,100,50);

        add(l1);

        l2 = new Label("Password: ");

        l2.setBounds(50,250,100,50);

        add(l2);

        t1 = new JTextField();

        p1 = new JPasswordField();

        t1.setBounds(170,150,250,50);

        p1.setBounds(170,250,250,50);

        add(t1);

        add(p1);

        t1.addActionListener(this);

        p1.addActionListener(this);

        b0 = new JButton("Login");

        b0.setBounds(170,350,100,50);

        add(b0);

        b0.addActionListener(this);

        b1 = new JButton("Go Back");

        b1.setBounds(300,350,100,50);

        add(b1);

        b1.addActionListener(this);

    }

    public void actionPerformed(ActionEvent e) 

    {

        s1 = t1.getText();

        s2 = p1.getPassword();

        s3 = new String(s2);        

        Object o = e.getSource();

        if(o == b1)

        {

            dispose();

            Home h = new Home();

        }

        mainLoop:

        if(o == b0)

        {

            try

            {

                Connection myConn = 

DriverManager.getConnection(url,user,password);

                Statement myStmt = myConn.createStatement();

                ResultSet myRs = myStmt.executeQuery("select * from login");

                while(myRs.next())

                {


if(s1.equals(myRs.getString("usname"))&&s3.equals(myRs.getString("pwd")))

                    {

                        System.out.println("Username Found");

                        System.out.println("Password Found");

                        JOptionPane.showMessageDialog(null,"Login Successful: 

"+s1 );

                        break mainLoop;

                    }



                }

                JOptionPane.showMessageDialog(null,"Wrong Username/Password");



            }   

catch(Exception exc)

{

    exc.printStackTrace();

}
        }

    }


    public void windowClosing(WindowEvent e) 

{  

        dispose();  

    }

    public void windowActivated(WindowEvent e) {}

    public void windowClosed(WindowEvent e) {}

    public void windowDeactivated(WindowEvent e) {}

    public void windowDeiconified(WindowEvent e) {}

    public void windowIconified(WindowEvent e) {}

    public void windowOpened(WindowEvent e) {}

}

Please tell me what I am doing wrong.请告诉我我做错了什么。 I have checked that I have provided the right password.我已经检查过我是否提供了正确的密码。 So that is not the problem.所以这不是问题。

CREATE ACCOUNT :-创建账户 :-

import java.awt.*;导入 java.awt.*;

import java.awt.event.*;导入 java.awt.event.*;

import java.sql.Connection;导入 java.sql.Connection;

import java.sql.DriverManager;导入 java.sql.DriverManager;

import java.sql.ResultSet;导入 java.sql.ResultSet;

import java.sql.Statement;导入 java.sql.Statement;

import javax.swing.*;导入 javax.swing.*;

public class Login extends Frame implements ActionListener, WindowListener公共类 Login 扩展 Frame 实现 ActionListener、WindowListener

{ {

Label l1,l2,l3;

JTextField t1;

JPasswordField p1;

JButton b0,b1;

String url = "jdbc:mysql://localhost:3306/database1";

String user = "root";

String password = "Newyear2016!";

char[] s2;

String s1,s3;

Login()

{

    addWindowListener(this);

    setTitle("Login");

    setSize(500,500);

    setLayout(null);

    setVisible(true);

    l3 = new Label("Login");

    l3.setBounds(120,50,300,50);

    add(l3);

    Font myFont1 = new Font("Helvetica",Font.ITALIC,30);

    l3.setFont(myFont1);

    l1 = new Label("User Name: ");

    l1.setBounds(50,150,100,50);

    add(l1);

    l2 = new Label("Password: ");

    l2.setBounds(50,250,100,50);

    add(l2);

    t1 = new JTextField();

    p1 = new JPasswordField();

    t1.setBounds(170,150,250,50);

    p1.setBounds(170,250,250,50);

    add(t1);

    add(p1);

    t1.addActionListener(this);

    p1.addActionListener(this);

    b0 = new JButton("Login");

    b0.setBounds(170,350,100,50);

    add(b0);

    b0.addActionListener(this);

    b1 = new JButton("Go Back");

    b1.setBounds(300,350,100,50);

    add(b1);

    b1.addActionListener(this);

}

public void actionPerformed(ActionEvent e) 

{

    s1 = t1.getText();

    s2 = p1.getPassword();

    s3 = new String(s2);        

    Object o = e.getSource();

    if(o == b1)

    {

        dispose();

        Home h = new Home();

    }

    mainLoop:

    if(o == b0)

    {

        try

        {

            Connection myConn = 

DriverManager.getConnection(url,user,password); DriverManager.getConnection(url,user,password);

            Statement myStmt = myConn.createStatement();

            ResultSet myRs = myStmt.executeQuery("select * from login");

            while(myRs.next())

            {

if(s1.equals(myRs.getString("usname"))&&s3.equals(myRs.getString("pwd"))) if(s1.equals(myRs.getString("usname"))&&s3.equals(myRs.getString("pwd")))

                {

                    System.out.println("Username Found");

                    System.out.println("Password Found");

                    JOptionPane.showMessageDialog(null,"Login Successful: 

"+s1 ); "+s1);

                    break mainLoop;

                }


            }
            JOptionPane.showMessageDialog(null,"Wrong Username/Password");



        }   

        catch(Exception exc)

        {
            exc.printStackTrace();

        }

    }

}



public void windowClosing(WindowEvent e) 

{  

    dispose();  

}

public void windowActivated(WindowEvent e) {}

public void windowClosed(WindowEvent e) {}

public void windowDeactivated(WindowEvent e) {}

public void windowDeiconified(WindowEvent e) {}

public void windowIconified(WindowEvent e) {}

public void windowOpened(WindowEvent e) {}

} #Edit :- I am sorry I forgot to provide the Create Account Page which was the problem. #Edit :- 对不起,我忘了提供问题所在的创建帐户页面。 Can you please kindly go through it?你能通过它吗?

Some of your code is not in the code block so its hard to read but did you load the driver class?您的某些代码不在代码块中,因此很难阅读,但是您是否加载了驱动程序类?

String url="jdbc:mysql://localhost:3306/database1";
String userName="root";
String password="Newyear2016!";
try {

    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con=DriverManager.getConnection(url,username,password);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:java.sql.SQLException:用户'root @ localhost'@'localhost'的访问被拒绝(使用密码:是) - Error:java.sql.SQLException: Access denied for user 'root@localhost'@'localhost' (using password: YES) java.sql.SQLException:用户'root@localhost'@'localhost'的访问被拒绝(使用密码:是) - java.sql.SQLException: Access denied for user 'root@localhost'@'localhost' (using password: YES) 渗透:java.sql.SQLException:对用户'root'@'localhost'的访问被拒绝(使用密码:是)。 如何不使用密码? - Osmosis: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES). How not to use a password? 用户'root'@'localhost'的访问被拒绝(使用密码:是):PhpMyAdmin - Access denied for user 'root'@'localhost' (using password: YES): PhpMyAdmin 用户'root'@'localhost'的访问被拒绝(使用密码:是) - Access denied for user 'root'@'localhost' (using password: YES) Springboot:用户'root'@'localhost'的访问被拒绝(使用密码:YES) - Springboot: Access denied for user 'root'@'localhost' (using password: YES) NetBeans 表示用户 'root'@'localhost' 的访问被拒绝(使用密码:YES) - NetBeans says that Access denied for user 'root'@'localhost' (using password: YES) 用户'root'@'localhost'的访问被拒绝(使用密码:是) - Access denied for user 'root'@'localhost' (using password: YES) 用户'root'@'localhost'访问被拒绝(使用密码:YES)在WAMPSERVER中 - Access denied for user 'root'@'localhost' (using password: YES) IN WAMPSERVER 用户'root'@'localhost'拒绝访问(使用密码:YES) - Access denied for User 'root'@'localhost' (using password: YES )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM