• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null columns

 
Paul Skaar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 = " ";
strBuff.append(middle_init);
}
else {
strBuff.append(middle_init);
}
Thanks!!!
 
Vani Yama
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think the problem is not in your query, but in your INSERT or UPDATE statements.
Example:
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.

Thanks,
Vani Yama
 
Paul Skaar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic