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?