I am new to making applications and never used database but know the basics of it. Found out a code to make a simple login page on the internet but the output returned is always "Incorrect Credentials".
Here is the code for the Submit button.
if(jTextField2.getText().length()==0) // Checking for empty field
JOptionPane.showMessageDialog(null, "Empty fields detected ! Please fill up all fields");
else if(jPasswordField1.getPassword().length==0) // Checking for empty field
JOptionPane.showMessageDialog(null, "Empty fields detected ! Please fill up all fields");
else{
String user = jTextField2.getText(); // Collecting the input
char[] pass = jPasswordField1.getPassword(); // Collecting the input
String pwd = String.copyValueOf(pass); // converting from array to string
if(validate_login(user,pwd))
JOptionPane.showMessageDialog(null, "Correct Login Credentials");
else
JOptionPane.showMessageDialog(null, "Incorrect Login Credentials");
}
}
and here is the code for validate login-
private boolean validate_login(String username,String password) {
try{
Class.forName("com.mysql.jdbc.Driver"); // MySQL database connection
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mmtc_law?zeroDateTimeBehavior=convertToNull [root on Default schema]?" + "user=root&password=");
PreparedStatement pst = conn.prepareStatement("Select * from login where username=? and password=?");
pst.setString(1, username);
pst.setString(2, password);
ResultSet rs = pst.executeQuery();
if(rs.next())
return true;
else
return false;
}
catch(Exception e){
e.printStackTrace();
return false;
}
this is my phpmyadmin page- enter image description here
still when i enter the username and password as "admin" the output is "Incorrect Credentials". Please help. Thanks in advance!