I'm trying to write a login() and connection() function for a library management system connected to MYSQL. When I try to retrieve "personnummer" and "password" from the database it returns rs.Next() == false. The table is populated and the query is returning "database connected". Can anyone help me figure out what I've done wrong? Thanks
public class main {
public static void login() {
//UI layer has been removed
loginButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String personnummer = F_personnummer.getText();
char[] password = F_password.getPassword();
if(personnummer.equals("")){
JOptionPane.showMessageDialog(null,"Please enter personnummer (YYMMDDXXXX)");
} else if (password.equals("")){
JOptionPane.showMessageDialog(null,"Please enter password");
} else {
try {
Connection connection=connect();
Statement stmt = connection.createStatement();
stmt.executeUpdate("USE library");
PreparedStatement loginQuery = connection.prepareStatement("SELECT * FROM library.users WHERE personnummer = " + "'" + personnummer + "'" + " AND password ='" + password + "'", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = loginQuery.executeQuery();
if(rs.next()==false) {
JOptionPane.showMessageDialog(null,
} else {
jframe.dispose();
rs.beforeFirst();
while(rs.next()) {
if(personnummer.equals("admin") & password.equals("admin")) {
adminMenu();
} else {
userMenu(personnummer);
}
} // end while ()
} // end else ()
} // end try ()
catch (Exception exception) {
exception.printStackTrace();
}
}
}
});
// UI layer has been removed
}
public static Connection connect() throws Exception{
try{
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/library?useTimezone=true&serverTimezone=UTC";
String username = "root";
String password = "root";
Class.forName(driver);
Connection connection = DriverManager.getConnection(url,username,password);
System.out.println("Connected to database");
return connection;
}
catch (Exception exception) {
System.out.println(exception);
}
return null;
}