Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getString, nulls, and try

 
Michael Brewer
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing some code that uses JDBC to connect to SQL Server 7 through ODBC. I'm using getString to get the value stored in one of my nvarchar columns. I'm allowing nulls in this column. When I use getString inside of a try, I get a SQLException. I tried using wasNull to avoid the error, but I think the try prevents me from doing that. Is there some other way I can handle this?
In VB, I would have used an iif statment to test the column for null, and if it is to return an empty string.
[ June 27, 2002: Message edited by: Michael Brewer ]
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try getObject. I believe that should allow you to retrieve nulls. You can then test if it's actually null after that.
 
Michael Brewer
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bosun Bello:
Try getObject. I believe that should allow you to retrieve nulls. You can then test if it's actually null after that.

OK, getObject sounds good. I should then test for null and cast it to a String, right? The only problem is I'm not only a greenhorn on this site, but also when it comes to Java. How do I test the object for null?
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object yourObject = yourResultset.getObject("colum")
if(yourObject != null)
do whatever.....
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The null value shouldn't be causing you problems. If you use getString() on a null column value, java transates that into a null String. So the error, if any, would be a null pointer exception. It must be incompatablility between the getString() method and your nVarchar column type. It should say in your driver documentation which method you should use to retrieve an nVarchar data type.
Jamie
 
Michael Brewer
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jamie Robertson:
It must be incompatablility between the getString() method and your nVarchar column type. It should say in your driver documentation which method you should use to retrieve an nVarchar data type.

Actually, I just doubled checked and the datatype is ntext. I have changed it to varchar to allow for 8000 character storage. This works. I also found that there is a bug in the JDBC ODBC driver involving ntext datatypes.
 
sateesh arumbaka
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I too had the same issue....(JDBC returning null for nvarchar types) I am using SQL Server 2000

After searching a lot on the NET....

I realised there is no work around for this issue other than to change the datatype of the column from nvarchar to varchar.

I contacted my dba and asked her to change the nvarchar datatypes columns to varchar. She wasnt ready for that because it was a production database.

So I used a CAST function to convert the nvarchar type to varchar types


for example :
select CAST( columnName as varchar(300)) as columnName from tableName


this worked for me...

hope this helps
Sateesh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic