Solved

getColumnType does not find a match using MSSQL nvarchar

Posted on 2002-04-23
10
583 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mysql jsp example issue 32 96
jmss example java 2 48
Java syntax, or is it Selenium 6 64
ArrayList Adding/settign data 3 38
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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

752 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