Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

CHAPTER 7: Result Sets Explored > Handling Null Values

Handling Null Values

A Java variable that holds a reference to an object can hold a nullvalue. But a primitive data type such as an int or a double can't hold a nullvalue. So when you retrieve a null value into a primitive data type, the value retrieved is set to 0. However, you can use the method wasNull() of the ResultSet interface to check if the value you retrieved was a null or not. Note that you must first call one of the getter methods on a column to try to read its value and then call the method wasNull() to see if the value read was null or not:

public boolean wasNull() throws SQLException;

Similarly, to set any column value to null, you can use the setNull() method of the PreparedStatement interface, which also takes in the SQL type of the column as the second parameter:


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial