Unable to load database file in java

225 Views Asked by At

I am using sqlite database and trying to check that the username and the password put into the input field are match to the database or not. But i'm getting error saying

[SQLITE_NOTADB] File opened that is not a database file (file is not a database)

I couldn't trace where the problem lies as my coding is very clear as shown below please help!

Connection to database :

try {
    Class.forName("org.sqlite.JDBC");
    connection = DriverManager.getConnection("jdbc:sqlite:G:\\java\\GUI\\db.sql");
    JOptionPane.showMessageDialog(null, "Connection Successful !");
    return connection;
} catch (Exception e) {
    JOptionPane.showMessageDialog(null, e);
    return null;
}

Code :

try {

    String query = " select * from employee where UserName=? and Password=? ";
    PreparedStatement preparedStatement = connection.prepareStatement(query);
    preparedStatement.setString(1, userNameField.getText());
    preparedStatement.setString(2, passwordField.getText());

    ResultSet resultSet = preparedStatement.executeQuery();

    int count = 0;

    while (resultSet.next()) {
        count++;

    }

    if (count == 1) {
        JOptionPane.showMessageDialog(null, "UserName and Password is Correct");
    }
    else if (count > 1) {
        JOptionPane.showMessageDialog(null, "UserName or Password is duplicate");
    }
    else {
        JOptionPane.showMessageDialog(null, "UserName and Password is not Correct. Try Again Please ...");
    }

    preparedStatement.close();
    resultSet.close();

} catch (SQLException exception) {

    JOptionPane.showMessageDialog(null, exception);
     System.err.println(exception.getClass().getName() + ": " + exception.getMessage() );

}
0

There are 0 best solutions below