Solved

getColumnType does not find a match using MSSQL nvarchar

Posted on 2002-04-23
10
566 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven project error 5 54
couple of eclipse 5 35
How to determine if a string is a valid SHA value 7 28
sql import cannot be resolved jsp 3 17
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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 …

806 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