This is a code for a login form where i used JDBC connection the code has no error but when i run it it always goes to else statement
Connection con;
Statement st;
ResultSet rs;
try{
Class.forName("java.sql.Driver");
con=DriverManager.getConnection ("jdbc:mysql://localhost/database","root","password");
st=con.createStatement();
rs=st.executeQuery("select * from users;");
while(rs.next()){
String userID=rs.getString("userID");
String password=rs.getString("password");
if(userID.equals(txtuserID.getText())
&& (password.equals(txtpassword.getPassword())) {
// ***HERE IS MY PROBLEM I WANT TO CHECK IF WHATS IN THE
// TEXT FIELD OR PASSWORD FIELD IS THE SAME FROM MySQL***
JOptionPane.showMessageDialog(null,"you have logged in");
new MainForm().setVisible(true);
} else {
JOptionPane.showMessageDialog(null,"Incorrect username and password");
}
}
} catch(Exception e) {
JOptionPane.showMessageDialog(null,"Error in Connectivity: " +e.getMessage());
}
The JPasswordField getPassword() method returns char[]. Convert to String before comparison : ( actually construct a new String with those chars ...)
if(userID.equals(txtuserID.getText()) &&
(password.equals(new String((txtpassword.getPassword()))) {
}