My aim is to test if the user and password inserted, existed in Table1. However, if I typed (pink,floyd) which exists in the database count still null and it appears the message “user doesn’t exist”.
x
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String user=f_user.getText().trim();
String pass=f_pass.getText().trim();
String sql="select user, pass from "Table1" where user='"+user+"' and pass='"+pass+"'";
rs=stat.executeQuery(sql);
int count=0;
while(rs.next()){
count++;
}
if (count==0) JOptionPane.showMessageDialog(null, "user doesn't exist");
else JOptionPane.showMessageDialog(null, "acces permitted");
} catch ( Exception ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
}
Here’s my database :
Advertisement
Answer
It turns that the stocked user and pass parameters in Table1 hasn’t the same length of input parameters . So , we need to change :
String sql="select user, pass from "Table1" where user='"+user+"' and pass='"+pass+"'";
to
String sql="select * from "Table1" where trim("user")= '"+user+"' and trim("pass")= '"+pass+"'";