Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Prepared Statement

 
Aju Babu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()".

Good Luck,
Avi.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic