Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

getString, nulls, and try

 
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 ]
 
Ranch Hand
Posts: 1512
  • 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: 1512
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object yourObject = yourResultset.getObject("colum")
if(yourObject != null)
do whatever.....
 
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.
 
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
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic