Click here to Skip to main content
15,885,054 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi. i want to create login page that only user who had role as admin can login. here is my databases :

tb_users :
user_id (PK, string)
password

tb_user_auth :
user_id (FK,string)
objects_id // contain user's role(string)

the problem is it seems like i retrieve nothing from objects_id so there's message like this : "check your password and user id". would you kindly tell me what's wrong with my code?

What I have tried:

private void cmd_loginActionPerformed(java.awt.event.ActionEvent evt) {
try{
String sql = "select * from tb_users where user_id=? and password=?";
PST=conn.prepareStatement(sql);
PST.setString(1,txt_userid.getText());
PST.setString(2, txt_pass.getText());
RS=PST.executeQuery();
RS.close();


String sql2 = "select * from tb_userauth";
PST=conn.prepareStatement(sql2);
//PST.setString(1,txt_userid.getText());
RS=PST.executeQuery();
//String objek_id = null;



if(RS.next()){
String objek = "admin";
String objek_id = RS.getString("objects_id");

if (objek_id.equals(objek)){

JOptionPane.showMessageDialog(null,"user id and password correct");
submenu1 sm1 = new submenu1();
sm1.setVisible(true);
sm1.pack();
sm1.setLocationRelativeTo(null);
sm1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.dispose();
}else{
JOptionPane.showMessageDialog(null,"check your password and user id ");
} }

}catch (Exception e){
JOptionPane.showMessageDialog(null,e);
}


}
Posted
Updated 2-Jun-18 21:16pm

1 solution

The obvious answer is: the userid and password do not match, so nothing is returned.
Use the debugger, to single step through your code and check exactly what is happening, and what is in the various variables.

But please, don't do that: Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^] - it's in C#, but the code is pretty obvious.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900