Solved

JDBC returning null for nvarchar types

Posted on 2004-08-19
29
628 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
  • 11
  • 6
  • 5
  • +2
29 Comments
 
LVL 35

Expert Comment

by:girionis
Comment Utility
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:mayankeagle
Comment Utility
Which driver are you using?
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
JDBC/ODBC, do you think thsi could be the problem?
0
 
LVL 2

Author Comment

by:mattizzle
Comment Utility
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
Comment Utility
I never had problems with JDBC/ODBC getting the meta data. What exactly is the nvarchar type?
0
 
LVL 30

Accepted Solution

by:
mayankeagle earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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:mayankeagle
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
Are you sure the column doesn't contain null :)
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 2

Author Comment

by:mattizzle
Comment Utility
ummmm.... yeah. ;)
0
 
LVL 2

Author Comment

by:mattizzle
Comment Utility
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
Comment Utility
> 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 92

Expert Comment

by:objects
Comment Utility
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
Comment Utility
I'm not needing or expecting any points on this one thanks
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
Comment Utility
> 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
Comment Utility
Are you using the latest driver version?
0
 
LVL 2

Author Comment

by:mattizzle
Comment Utility
I'm using whatever version is included in 1.4.2x.

-M
0
 
LVL 2

Author Comment

by:mattizzle
Comment Utility
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:mayankeagle
Comment Utility
>> 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:mayankeagle
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countAbc challenge 9 49
JDeveloper 12c for 32 bit 4 34
Checkbox and ListView in Android Layout 4 36
backtracking recursion  code 19 39
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now