I want to create a login system with a MS Access database. the data to log in is in the database.
There is a possibility of working online, you then fill in the login details. And press OK to move to the next screen.
And an ability to work offline, then you put “student” at username (leave password empty) and press OK to move to the next screen.
There must be an error given if the credentials are wrong but there should be no error when only “student” is filled
Now the problem is it gives the error as a “student” is entered here, but he should not error to give. Only when the login data is wrong. How do I fix this?
Code:
/**Local*/
try {
String idnr = GebruikersnaamTekst.getText().trim();
if (idnr.matches("Cursist")) {
BasisScherm b = new BasisScherm();
b.setVisible(true);
setVisible(false);
}
} catch (Exception e) {
}
/**Internet*/
try
{
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver);
String db = "jdbc:odbc:db1";
con = DriverManager.getConnection(db);
st = con.createStatement();
}
catch(Exception e)
{
}
try
{
String idnr = GebruikersnaamTekst.getText().trim();
String pass = Password.getText().trim();
String sql = "select idnr,pass from Table1 where idnr='"+idnr+"'and pass='"+pass+"'";
rs = st.executeQuery(sql);
int count = 0;
while(rs.next())
{
count = count + 1;
}
if (count == 1) {
BasisScherm b = new BasisScherm();
b.setVisible(true);
setVisible(false);
}
else
{
JOptionPane.showMessageDialog(null, "Gebruiker niet gevonden!");
}
}
catch(Exception ex )
{
}
}
You’re looking for it to succeed if you match correctly for “Student” right?
It might be an issue of case sensitivity, try this.
That will evaluate true, and be case insensitive.
Also as far as program flow goes, if those are in the same class, or main() function, it will execute the Internet regardless of the local outcome, which could also introduce errors.
Create a global Boolean variable perhaps. To confirm that you’ve done the student check/
Full code: