Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

JDBC returning null for nvarchar types

Posted on 2004-08-19
29
Medium Priority
?
651 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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 750 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…
Suggested Courses

824 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