Самый простой способ найти - это отладить ваш код, поместив точки останова в код и исследуя / отслеживая значения переменных.Большинство IDE имеют эти функции отладки.В дополнение к ответу Эллиотта Фриша, если я реструктурирую ваш код, как показано ниже, то в случае неверного / неправильного SQL управление переходит в блок catch
, и вы видите, что значение rs
остается null
.
public void executeQuery(Connection conn, String username,String password) {
String sql = "SELCT * FROM user WHERE username = '" + username + "' and password = '" + password + "'";
ResultSet rs = null;
Statement stm = null;
try {
stm = conn.createStatement();
rs= stm.executeQuery(sql);
while(rs.next()) {
//Extract ResultSet here as per needed logic
}
} catch (SQLException e) {
// Your control comes here if query is wrong , put a break point at below line & examine value of rs
e.printStackTrace();
}finally {
// Close resources not needed after this method call like - result sets , statements & connection
}
}