Application Server Solutions for Microsoft IIS and ASP.NET
       solutions   products   partners   company   support   downloads         store
JTurbo Self-Help: FAQ
Back to Search >  Back to Search Results

Faq ID 229
Product JTurbo
Category Internationalization
Question How do I insert and retreive International / non-ASCII characters into SQL Server using JTurbo ?
Answer Internally, Java stores all strings using the Unicode character set which uses 2 bytes per character. If your java code uses bytes that are part of a character set other than Unicode (ex. Cp1252, Shift-JIS, or Big5) then you will need to convert those bytes to a Unicode string before passing them to the JTurbo driver. To convert some Cp1252 bytes to a Unicode string you would do the following:
String unicode = new String(cp1252Bytes, "Cp1252" );
JTurbo will send the characters to SQL Server as Unicode and SQL Server will convert the characters to the charset it is using before inserting the data into the database.

When JTurbo retrieves the stored data, it will need to convert the bytes it receives from SQL Server into Unicode. This means you must set the JTurbo charset connection property to the appropriate value so JTurbo will know how to convert the bytes to Unicode. FAQ #107 gives more details about this. The following example shows how the Euro character in the Cp1252 character set would be inserted into and read from SQL Server. Note that in the Cp1252 character set the Euro character has a value of 128 while in the Unicode character set it has a value of 8364.

 
import java.sql.*;
import java.io.*;

public class CP1252
{
	// In code page 1252 the euro character has a value of 128 (0x80) so create a byte array that contains this character  
	private static final byte[] euroInCp1252 = new byte[] { (byte)0x80 };
	private static String  server    = "localhost";
	private static String  database  = "tempdb";
	private static String  username  = "sa";
	private static String  password  = "sa";
	private static String  charset   = "Cp1252";
	private static String  tableName = "MiscCp1252";
      
	public static void main( String args[] ) throws IOException
	{
		// Uncomment to enable logging of debug messages
		//DriverManager.setLogStream( System.out );

		try
		{
			// Load the JTurbo driver
			Class.forName( "com.newatlanta.jturbo.driver.Driver" );

			// Connect to the database making sure to set the charset to "Cp1252"
			Connection con = DriverManager.getConnection("jdbc:JTurbo://" +server+ '/' +database+ "/user=" +username+ "/password=" +password+ "/charset=" +charset);
			
			// Drop the test table so we can start with an empty table
			Statement stmt = con.createStatement();
			try
			{
				stmt.executeUpdate( "drop table " +tableName );
			}
			catch( Exception e )
			{
				// Don''t log exception since an exception is thrown if the table doesn''t exist
			}

			// Create a table to hold the euro character
			stmt.executeUpdate( "create table " +tableName+ " (c char(1))" );

			// Create a Unicode string from the Cp1252 byte array
			String euroInUnicode = new String( euroInCp1252, "Cp1252" );

			// Insert the euro character in the database
			PreparedStatement pstmt = con.prepareStatement( "insert into " +tableName+ " values(?)" );
 			pstmt.setString( 1, euroInUnicode );
			pstmt.executeUpdate();
			pstmt.close();

			// Read the euro character from the database
			ResultSet rs = stmt.executeQuery( "select * from " +tableName );
			rs.next();
			String euroChar = rs.getString( 1 );
			if ( euroChar.length() == 1 )
			{
				// Compare to the Unicode value for the euro character (8364)
				if ( euroChar.charAt( 0 ) == (char)8364 )
				{
					// Extract the String bytes in Cp1252 format
					byte[] euroBytesInCp1252 = euroChar.getBytes( charset );

					// Compare to the Cp1252 value for the euro character (0x80)
					if ( euroBytesInCp1252[ 0 ] == (byte)0x80 )
					{
						System.out.println( "PASSED" );
					}
					else
					{
						System.out.println( "FAILED: Cp1252 value is wrong: " + Integer.toHexString( (int)euroBytesInCp1252[ 0 ] ) );
					}
				}
				else
				{
					System.out.println( "FAILED: unicode value is wrong: " + Integer.toHexString( (int)euroChar.charAt( 0 ) ) );
				}
			}
			else
			{
				System.out.println( "FAILED: length is wrong - " + euroChar.length() );
			}
			
			rs.close();
			stmt.close();
			con.close();
        }
        catch ( Exception e )
        {
            System.out.println( "FAILED: " + e.getMessage() );
	    	e.printStackTrace();
        }
    }
}
 



   
company media information terms of use privacy policy contact us