aspose file tools
The moose likes JDBC and the fly likes Prepared Statement Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "Prepared Statement" Watch "Prepared Statement" New topic
Author

Prepared Statement

Aju Babu
Greenhorn

Joined: Jul 07, 2004
Posts: 5
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
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

Joined: Oct 11, 2000
Posts: 1112

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Prepared Statement
 
Similar Threads
B&S 2.2.2 updateRecord()
reading excel file using jdbc [help me]
default value for EL functions arguments
Displaying modified VO values in JSP
Values of Hash Map to be 2-dimensional array