Solved

getColumnType does not find a match using MSSQL nvarchar

Posted on 2002-04-23
10
578 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb entity bean example 2 35
Java: anonymous class 4 38
restrict decimal places for double datatype 10 31
How to fix  socket closed error 11 27
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…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

749 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