Solved

JDBC returning null for nvarchar types

Posted on 2004-08-19
29
644 Views
Last Modified: 2008-02-01
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;
    }
0
Comment
Question by:mattizzle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 6
  • 5
  • +2
29 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 11842882
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 11842909
Which driver are you using?
0
 
LVL 35

Expert Comment

by:girionis
ID: 11842926
JDBC/ODBC, do you think thsi could be the problem?
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 2

Author Comment

by:mattizzle
ID: 11842950
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
 
LVL 35

Expert Comment

by:girionis
ID: 11842985
I never had problems with JDBC/ODBC getting the meta data. What exactly is the nvarchar type?
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 250 total points
ID: 11842992
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
 
LVL 2

Author Comment

by:mattizzle
ID: 11843075
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
 
LVL 2

Author Comment

by:mattizzle
ID: 11843110
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 11843153
>> 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
 
LVL 35

Expert Comment

by:girionis
ID: 11843263
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
 
LVL 2

Author Comment

by:mattizzle
ID: 11843811
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11846044
>>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
 
LVL 2

Author Comment

by:mattizzle
ID: 11846167
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
 
LVL 92

Expert Comment

by:objects
ID: 11846768
Are you sure the column doesn't contain null :)
0
 
LVL 2

Author Comment

by:mattizzle
ID: 11846858
ummmm.... yeah. ;)
0
 
LVL 2

Author Comment

by:mattizzle
ID: 11846866
objects, I guess you don't get to 2 million+ expert points by not covering all of the bases ;)
0
 
LVL 92

Expert Comment

by:objects
ID: 11846871
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11846897
If you don't want to change the driver, try changing from nvarchar to varchar - it may work ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 11846912
> try changing from nvarchar to varchar

yes that will work as I posted above.
Not a very practical suggestion though.
0
 
LVL 2

Author Comment

by:mattizzle
ID: 11847048
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11847065
I'm not needing or expecting any points on this one thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 11847066
> 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
 
LVL 2

Author Comment

by:mattizzle
ID: 11847082
> 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
 
LVL 92

Expert Comment

by:objects
ID: 11847088
Are you using the latest driver version?
0
 
LVL 2

Author Comment

by:mattizzle
ID: 11847893
I'm using whatever version is included in 1.4.2x.

-M
0
 
LVL 2

Author Comment

by:mattizzle
ID: 11849160
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 11849250
>> 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
 
LVL 30

Expert Comment

by:Mayank S
ID: 11849262
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

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question