DEVFYI - Developer Resource - FYI

How can I retrieve string data from a database in Unicode format?

JDBC Interview Questions and Answers


(Continued from previous question...)

How can I retrieve string data from a database in Unicode format?

The data is already in Unicode when it arrives
 in your program. Conversion from and to the 
 encoding/charset/CCSID in the database from/to 
 Unicode in the program is part of the JDBC driver's job.

If, for some reason, you want to see the data in 
'\uHHHH' format ( where 'H' is the hex value ),
the following code, while not very efficient,
 should give you some ideas:


public class UniFormat
{

  public static void main( String[] args )
  {
    char[] ac = args[0].toCharArray();
    int iValue;
    String s = null;
    StringBuffer sb = new StringBuffer();

    for( int ndx = 0; ndx < ac.length; ndx++ )
    {
      iValue = ac[ndx];

      if( iValue < 0x10 )
      {
        s = "\\u000";
      }
      else
      if( iValue < 0x100 )
      {
        s = "\\u00";
      }
      else
      if( iValue < 0x1000 )
      {
        s = "\\u0";
      }

      sb.append( s + Integer.toHexString( iValue ) );
    } // end for

    System.out.println("The Unicode format of " + 
                          args[0] + " is " + sb );

  } // end main

} // end class UniFormat

(Continued on next question...)

Other Interview Questions