Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling the null value from a resultset

Tags:

java

jdbc

I currently have a result set returned, and in one of the columns the string value may be null (I mean no values at all). I have a condition to implement like following

rs = st.executeQuery(selectSQL); output = rs.getString("column"); 

Since the column may be null in the database, the rs.getString() will throw a NullPointerException when the column is null. If column is null, I want the output to be an empty string like output = "";. I can't check if(rs.getString("column) != null either. How can I tackle this situation?

My real problem:

try {     rs = st.executeQuery(sql);     int i = 0;     while (rs.next()) {         output[i] = rs.getString(column);         // column field in the database contains multiple results, but sometimes         // may be null         i++;     } } catch (SQLException e) {     e.printStackTrace();     // other than tracing the exception i want to fill the array too } return output; 

Now, if one of the column values contains no value, i.e. null, I want output[i] defined as N/A. This problem stems from the fact that the column field is NULL allowed in the database. And sorry for telling you that it's a NPE, while in fact it's a SQLException.

like image 942
javaNOob Avatar asked May 13 '11 11:05

javaNOob


People also ask

How do you handle null in ResultSet?

Use primitive data types and the ResultSet object's wasNull( ) method to test whether the primitive variable that received the value returned by the getXXX( ) method should be set to an acceptable value that you've chosen to represent a NULL.

How does Java handle empty ResultSet?

The right way to check if ResultSet is empty A better way to solve this problem is to get the data from the first row before calling the next() method again and you can do that by using a do-while loop, which checks the condition at the end of the loop as opposed to starting of the loop in case of a while loop.

Can ResultSet be null?

No matter how many rows are returned(starting from 0), the resultSet will never be null. The only case in which a resultset will remain null is when an exception is thrown... but the program flow will jump to the Exception handling block anyways.


1 Answers

Since the column may be null in the database, the rs.getString() will throw a NullPointerException()

No.

rs.getString will not throw NullPointer if the column is present in the selected result set (SELECT query columns) For a particular record if value for the 'comumn is null in db, you must do something like this -

String myValue = rs.getString("myColumn"); if (rs.wasNull()) {     myValue = ""; // set it to empty string as you desire. } 

You may want to refer to wasNull() documentation -

From java.sql.ResultSet boolean wasNull() throws SQLException;  * Reports whether * the last column read had a value of SQL <code>NULL</code>. * 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 <code>wasNull</code> to see if the value read was * SQL <code>NULL</code>. * * @return <code>true</code> if the last column value read was SQL *         <code>NULL</code> and <code>false</code> otherwise * @exception SQLException if a database access error occurs or this method is  *            called on a closed result set */ 
like image 131
d-live Avatar answered Oct 11 '22 00:10

d-live