Hi, I'm trying to make a login in Java and MySQL,.My GUI is running but I think my calls is wrong. Furthermore, my "While" won't work and I cannot figure it out. Any suggestions.Hope I post my code correct here, Thanks.
Best.
T
Best.
T
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.*;
import java.sql.*;
public class Login {
static JTextField Name,password;
static JButton submit;
private static class Handler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String user = Name.getText();
String strpass = password.getText();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sunsetresort", "root","root");
Statement st = con.createStatement();
String query = "SELECT Name,password FROM staff WHERE ='"+Name+"'and pass"+password+"'";
System.out.println(query);
ResultSet rs = st.executeQuery(query);
int count = 0;
while(rs.next())
{
count = count + 1;
}
if(count == 1)
{
JOptionPane.showMessageDialog(null,"Access Granted");
}
else if(count > 1)
{
JOptionPane.showMessageDialog(null,"Duplicated User - No Access");
}
else
{
JOptionPane.showMessageDialog(null,"User not found");
}
} catch (SQLException ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Login(){
JFrame main = new JFrame("Login Sunset Resort");
main.setBounds(250,150,500,500);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Name = new JTextField(10);
password = new JTextField(10);
main.setLayout(new GridLayout(0,1));
JPanel pane = new JPanel();
main.add(pane);
pane.add(new JLabel("Username: "));
pane.add(Name);
pane.add(new JLabel("Password: "));
pane.add(password);
submit = new JButton("Login");
pane.add(submit);
submit.addActionListener(new Handler());
main.setVisible(true);
}
public static void main(String args[]){
new Login();
}
}