aspose file tools*
The moose likes JDBC and the fly likes getString, nulls, and try Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "getString, nulls, and try" Watch "getString, nulls, and try" New topic
Author

getString, nulls, and try

Michael Brewer
Ranch Hand

Joined: Jun 27, 2002
Posts: 54
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

Joined: Nov 06, 2000
Posts: 1506
Try getObject. I believe that should allow you to retrieve nulls. You can then test if it's actually null after that.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Michael Brewer
Ranch Hand

Joined: Jun 27, 2002
Posts: 54
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

Joined: Nov 06, 2000
Posts: 1506
Object yourObject = yourResultset.getObject("colum")
if(yourObject != null)
do whatever.....
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

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

Joined: Jun 27, 2002
Posts: 54
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

Joined: Nov 22, 2002
Posts: 6
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getString, nulls, and try
 
Similar Threads
How to handl Null Values from Database
Unsure how to do subtraction using joined table values
CastMap getTrimmedString() error?
Database Starnge behaviour
Updatable ResultSet - getString works but getObject does not??