Solved

getColumnType does not find a match using MSSQL nvarchar

Posted on 2002-04-23
10
558 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

867 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

21 Experts available now in Live!

Get 1:1 Help Now