SQLITE PreparedStatement始终返回空ResultSet

时间:2018-01-22 15:36:01

标签: java sql sqlite jdbc

我想知道为什么这个方法总是空的ResultSet:

    public boolean checkIfUsersExists(String email) throws SQLException {
    String query = "SELECT * FROM users WHERE email= ?;";
    PreparedStatement preparedStatement = getConnection().prepareStatement(query);
    preparedStatement.setString(1, email);

    log.info(preparedStatement.toString());

    ResultSet resultSet = preparedStatement.executeQuery();

    return resultSet.next();
}

这种方法效果很好:

    public boolean checkIfUsersExists(String email) throws SQLException {
    String query = String.format("SELECT * FROM users WHERE email='%s';", email);
    log.info(query);
    ResultSet resultSet = getConnection()
            .createStatement()
            .executeQuery(query);

    return resultSet.next();
}

我需要使用PreparedStatement来避免SQL注入,但在这种情况下它不起作用。 我使用sqlite 3.21.0.1

0 个答案:

没有答案