Solved

getColumnType does not find a match using MSSQL nvarchar

Posted on 2002-04-23
10
549 Views
Last Modified: 2012-06-27
Hello,

The following code should illustrate the problem:

private Vector getNextRow( ResultSet rs,
               ResultSetMetaData rsmd )
     throws SQLException
   {
   Vector currentRow = new Vector();

   for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
      switch( rsmd.getColumnType( i ) ) {
        case Types.VARCHAR:
             currentRow.addElement(
           new String( rs.getString( i ) ) ) ;
          break;
        case Types.INTEGER:
           currentRow.addElement(
           new Long( rs.getLong( i ) ) );
       break;
     case Types.BIT:
        currentRow.addElement(                  new Boolean (rs.getBoolean( i ) ) ) ;
          break;
        case Types.OTHER:
        currentRow.addElement(
        new String( rs.getString( i ) ) );
          break;
        default:
           System.out.println( "Type was: " +
              rsmd.getColumnTypeName( i ) );
         }
     
    return currentRow;
}

Please excuse the improper spacing (cut and paste into this box never matches the actual code formatting).

The problem is that several fields from the MSQL Database are nvarchar. These slip through the loop because there isn't a types.match for nvarchar. Is there a better way to capture any field types for nvarchar or nchar using the ResultsSetMetaData class? Also, does anybody have any examples of the types.Other variable and it's use?

Any suggestion appreciated!
0
Comment
Question by:mmeinz
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 6963753
so what does

rsmd.getColumnType( i )

return ?
0
 

Author Comment

by:mmeinz
ID: 6963786
It does return all integer and bit field types...but skips the nvarchar fields.
0
 
LVL 92

Expert Comment

by:objects
ID: 6964093
Just use and let JDBC sort out the type for you:

  for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
    currentRow.addElement(rs.getObject( i )) ;
   return currentRow;
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6964835
my suggestion was to print the columnType value

System.out.println(rsmd.getColumnType( i ));
0
 

Author Comment

by:mmeinz
ID: 6966310
Thanks objects and heyhey...

objects - This is a good work around, but I'm trying to target specific field types and thus need to determine how to handle the nvarchar field type.

heyhey - I printed out the column types...and it returned "nvarchar -9". I have previously tried placing the NVARCHAR in place of VARCHAR in the case statement above. It just ignores the type definition of "NVARCHAR" as a TYPES definition.

Any continued suggestions are greatly appreciated.
0
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

 
LVL 92

Expert Comment

by:objects
ID: 6967032
> objects - This is a good work around, but I'm trying to
> target specific field types and thus need to
> determine how to handle the nvarchar field type.

Sorry I thought you wanted to process all columns.
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 6967046
Try:

case -9:
0
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 6967322
since -9 doesn't have a constant defined in java.sql.Types, what objects suggests above is probably the best choice. To make the code cleaner, you might want to define your own constant:
   public static final int NVARCHAR = -9;
then
   case NVARCHAR:
      ...
0
 

Author Comment

by:mmeinz
ID: 7009917
I apologize for the long delay. I thought I already took care of this earlier but obviously didn't. Casing on the  "-9" worked, I was surprised. Thanks for your response.
0
 
LVL 92

Expert Comment

by:objects
ID: 7009923
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

758 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

20 Experts available now in Live!

Get 1:1 Help Now