Can someone please tell me how to use PreparedStataement for Inserting a String which has optional values meaning , it can have some text or NULL - if I use setString(1,stringName) - it fails in places where this string has NULL values
David Harkness
Ranch Hand
Joined: Aug 07, 2003
Posts: 1646
posted
0
If the string is a parameter in a where clause, you'll need to use two different prepared statements: one with "where <column> = ?" and the other with "where <column> is null." There's simply no way around that since the SQL syntax is different. It has nothing to do with JDBC.
If it's a value in an insert or update statement, I thought it should work using setString(index, str) where str == null, but I haven't done hand-coded JDBC much in a while. If that doesn't work (NullPointerException) then you need to use setNull(index).
Aju, If you are using an Oracle database, you can use Oracle's NVL function. Otherwise, you have to explicitly use "setNull()". In other words, you need to check whether the value is null, or not. If it is, then use "setNull()", otherwise use "setString()".