Hi All,
I'm trying to do something like this
PreparedStatement pstmt = con.prepareStatement("SELECT * FROM users WHERE firstname=? AND middlename=?");
pstmt.setString(1, firstname);
if (middlename != null) {
pstmt.setString(2, middlename);
}
else {
pstmt.setNull(2, Types.VARCHAR);
}
ResultSet resultSet = pstmt.executeQuery();
Despite I know some records will match the query when middlename == null, resultSet is always fetching no records. I know that the problem is about using setNull(...) method for parameteres in the WHERE clause since it probably is replacing the question mark by "null" but it should remove the previous equal sign and append " IS NULL" instead.
I have searched on this phorum for a while but all threads have led me to a dead end. Since most of them are from 2000 to 2002 I'm wondering if someone knows now a good solution for this issue without having to prepare 2 different SQL sentences, which sometimes won't be practical at all.
Thanks in advance.