How to deal with (maybe) null values in a PreparedStatement?

后端 未结 5 1915
不知归路
不知归路 2020-12-12 20:36

The statement is

SELECT * FROM tableA WHERE x = ?

and the parameter is inserted via java.sql.PreparedStatement \'stmt\'

stm         


        
5条回答
  •  没有蜡笔的小新
    2020-12-12 21:24

    If you use for instance mysql you could probably do something like:

    select * from mytable where ifnull(mycolumn,'') = ?;
    

    Then yo could do:

    stmt.setString(1, foo == null ? "" : foo);
    

    You would have to check your explain plan to see if it improves your performance. It though would mean that the empty string is equal to null, so it is not granted it would fit your needs.

提交回复
热议问题