File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes null columns Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "null columns" Watch "null columns" New topic

null columns

Paul Skaar

Joined: May 06, 2003
Posts: 2
I am querying a database as part of a school project. One of the columns can be null and when I get the results back I see the word null instead of a space. I got it to work by assigning the column to a String and checking that for null but how do you get the wasNull() to work? Here is my code:
String middle_init = results.getString(3);
if(middle_init == null) {
middle_init = " ";
else {
Vani Yama

Joined: May 12, 2003
Posts: 7
I think the problem is not in your query, but in your INSERT or UPDATE statements.
sqlStr = new StringBuffer("UPDATE test SET ");
sqlStr.append("first_name = '" + getFirstName() + "', ");
sqlStr.append("last_name = '" + getLastName() + "' ");
sqlStr.append(" where id = " + getId());
With the above code, the value in the database is stored as a string "null", if getFirstName() or getLastName() methods return null.
I think this is what is happening in your code too causing results.getString() to return "null" as the string.
If this is the problem, the way to solve it is
sqlStr = new StringBuffer(
"UPDATE test SET " +
"first_name = ? " +
" AND " +
"last_name = ? " +
" WHERE " +
"id = ? ");

pStmt = conn.prepareStatement(sqlStr.toString());
pStmt.setString(1, getFirstName());
pStmt.setString(2, getLastName());
pStmt.setInt(3, getId());
This should fix your problems with the query you are doing and you should be able to use wasNull().

Let me know, if this does not work.

Vani Yama
Paul Skaar

Joined: May 06, 2003
Posts: 2
Sorry, need to clarify.
The code I displayed worked fine, I was just trying to figure out how to do the same thing basically with the wasNull method of a result set.
No confusion intended, thoughts welcomed.
I agree. Here's the link:
subject: null columns
jQuery in Action, 3rd edition