-1

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. 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. 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.*;

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) {}

} #Edit :- I am sorry I forgot to provide the Create Account Page which was the problem. Can you please kindly go through it?

  • I think that this can help you https://stackoverflow.com/questions/11922323/java-sql-sqlexception-access-denied-for-user-rootlocalhost-using-password – Cláudio Sumburane Sep 28 '17 at 15:50
  • I think that this can help you: https://stackoverflow.com/questions/11922323/java-sql-sqlexception-access-denied-for-user-rootlocalhost-using-password – Cláudio Sumburane Sep 28 '17 at 15:52

1 Answers1

0

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);
Kirsteen
  • 136
  • 5
  • Actually I saw this driver thing and i wrote the exact code you have written here but it gave the same error. Now I did not need this line before so I have never written it. – Shreyas Mishra Sep 28 '17 at 16:26