JDBC returning null for nvarchar types

I have a utility function that converts a row in a ResultSet to a Map.  The problem is that when I hit a SQL Server database, the ResultSet.getObject function returns null on all NVARCHAR fields.  This is odd behavior to say the least - any ideas?

This function works with other databases such as MySQL and MS Access.  Also, in this case I am using the JDBC/ODBC Bridge Driver.

public static Map toMap(ResultSet rs)
        throws SQLException
    {
        HashMap map = new HashMap();
        ResultSetMetaData meta = rs.getMetaData();
        for(int i = 1; i <= meta.getColumnCount(); i++)
        {
            Object val = rs.getObject(i);
            map.put(meta.getColumnName(i), val);
        }

        return map;
    }
LVL 2
mattizzleAsked:
Who is Participating?
 
Mayank SConnect With a Mentor Associate Director - Product EngineeringCommented:
Sorry I missed:

>> am using the JDBC/ODBC Bridge Driver.

in a hurry. My first guess would be that it is an issue with the driver.Try the M$ SQL Server driver.
0
 
girionisCommented:
Hi mattizzle,

can you make sure the object is null? What is the output of this inside the for loop:

System.out.println(val); Is it null?

Regards
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Which driver are you using?
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
girionisCommented:
JDBC/ODBC, do you think thsi could be the problem?
0
 
mattizzleAuthor Commented:
girionis,

Yes, it is null when the field is of type NVARCHAR.  It works fine on others such as date or number fields.

mayankeagle,
I am using the "sun.jdbc.odbc.JdbcOdbcDriver" driver.

-M
0
 
girionisCommented:
I never had problems with JDBC/ODBC getting the meta data. What exactly is the nvarchar type?
0
 
mattizzleAuthor Commented:
girionis,

I am not having trouble getting meta data, I am having trouble with the getObject method returning null when the database field in SQL Server is of type NVARCHAR.

mayankeagle,

I prefer not to change the driver, though I am already aware that that would likely resolve this issue.  I have my reasons for not wanting to change, which (keep things simple) I'd rather not go into.  ;)

-M
0
 
mattizzleAuthor Commented:
girionis,

NVARCAHR is a variable length character field, see:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_na-nop_9msy.asp

-M
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> I have my reasons for not wanting to change, which (keep things simple) I'd rather not go into.

But (a big but) I would recommend that you should give it a try. You can always JAR it along with your other classes and deliver it anywhere you want. That is why you have so many drivers available. Moreover if the vendor supplies a driver for its DB, it is recommended.

>> What exactly is the nvarchar type?

Variable-length unicode data with a maximum length of 4000 characters.
0
 
girionisCommented:
I'd have to aggre wiht mayankeagle, try a different driver just to be sure first and then we can proceed and try to find a work around.
0
 
mattizzleAuthor Commented:
I appreciate the input, but I already know the other driver resolves this issue.   I want to fix the problem with this JDBC/ODBC Bridge driver.  Or at least find someone who can confirm that this is a bug that cannot be resolved.

-M
0
 
CEHJCommented:
>>I am having trouble with the getObject method returning null when the database field in SQL Server is of type NVARCHAR.

Why would you need to call getObject on this type of field. Why not getString?
0
 
mattizzleAuthor Commented:
Because this is a generic method that isn't aware of data types.  I've already thought of asking the meta data for the field type in a switch, the calling the appropriate getter (e.g. getString, getDate, etc.).  But I really would rather leave it up to the driver implementation to handle this since this logic works fine with all other drivers and databases.

-Matt

0
 
objectsCommented:
Are you sure the column doesn't contain null :)
0
 
mattizzleAuthor Commented:
ummmm.... yeah. ;)
0
 
mattizzleAuthor Commented:
objects, I guess you don't get to 2 million+ expert points by not covering all of the bases ;)
0
 
objectsCommented:
> ummmm.... yeah. ;)

Just checking :)
Your code probably should be checking for null anyway.

Also try the latest version of the driver if you aren't already.
0
 
CEHJCommented:
If you don't want to change the driver, try changing from nvarchar to varchar - it may work ;-)
0
 
objectsCommented:
> try changing from nvarchar to varchar

yes that will work as I posted above.
Not a very practical suggestion though.
0
 
mattizzleAuthor Commented:
Yeah, unfortunately I cannot change the data types in the table...  Also, the url objects posted wasn't relevant (unless I am missing something).  I coming to the conclusion that there is no answer.  I guess I will have to log a bug with Sun.

I'll give this question a little bit longer to see if someone comes up with something.

Assuming noone does answer this - how do I distribute points?  I didn't get a (practical) answer, but I dont' want to discount everyone's input.  What's the rule of thumb at EE?

-M
0
 
CEHJCommented:
I'm not needing or expecting any points on this one thanks
0
 
objectsCommented:
> Also, the url objects posted wasn't relevant (unless I am missing something).

"When i call ResultSet.getObject() on a nvarchar column I get null.
If i use a varchar column then i get the right data."


0
 
mattizzleAuthor Commented:
> Also, the url objects posted wasn't relevant (unless I am missing something).

I see - I must have skimmed over that.  I was the same post at javaranch.  

http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=3&t=002853

Either way - I'm stuck with nvarchar in this case, so unfortunately it doens't apply in this case :(

-M
0
 
objectsCommented:
Are you using the latest driver version?
0
 
mattizzleAuthor Commented:
I'm using whatever version is included in 1.4.2x.

-M
0
 
mattizzleAuthor Commented:
Okay - I give up.  I have no other option but to use a different driver.

Anybody know what I should do about points?

-M
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> I have no other option but to use a different driver

You should. That is recommended, like I said. And, well if getObject () doesn't work, then getString () will also not work on it. And I'm assuming that the data-type of the column cannot be changed (the code should not drive the DB design ;-) the DB design could drive the code).

Perhaps the JDBC driver does not have an implementation to read NVARCHAR types.

>> Anybody know what I should do about points?

Whatever you want.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
I've noticed that you're fast enough in your replies and you make sure that you respond to all the Qs that experts ask while trying to help you out. That is enough for me.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.