| Oracle® Database JDBC Developer's Guide and Reference 10g Release 1 (10.1) Part Number B10979-02 |
|
|
View PDF |
Oracle's JDBC drivers provide Globalization Support (formerly NLS). Globalization Support allows you retrieve data or insert data into a database in any character set that Oracle supports. If the clients and the server use different character sets, then the driver provides the support to perform the conversions between the database character set and the client character set.
This chapter contains the following sections:
For more information on Globalization Support, Globalization Support environment variables, and the character sets that Oracle supports, see "Oracle Character Datatypes Support" and the Oracle Database Globalization Support Guide. See the Oracle Database Reference for more information on the database character set and how it is created.
The basic JAR files (classes12.jar and ojdbc14.jar) contain all the necessary classes to provide complete globalization support for:
Oracle character sets for CHAR, VARCHAR. LONGVARCHAR, or CLOB data that is not being retrieved or inserted as a data member of an Oracle 8 Object or Collection type.
CHAR or VARCHAR data members of Object and Collection for the character setsUS7ASCII, WE8DEC, WE8ISO8859P1, WE8MSWIN1252, and UTF8.
To use any other character sets in CHAR or VARCHAR data members of Objects or Collections, you must include orai18n.jar in your application's CLASSPATH.
|
Note: Previous releases depended on the filenls_charset12.zip; this file is now obsolete. |
The file orai18n.jar contains many important character set and globalization support files. You can reduce the size of orai18n.jar by including only the character set classes you use in your application
The character set extension class files are located in oracle/i18n/data/ and named in the format lx20OracleCharacterSetId.glb, where OracleCharacterSetId is the hexadecimal representation of the Oracle character set ID. You can determine the decimal representation of any ID by using the SQL function NLS_CHARSET_ID.
For example, if your application connects to a JA16SJIS database, the following SQL statement returns the decimal representation of that character set:
select NLS_CHARASET_ID('ja16sjis') from DUAL;
The example returns 832. You would then manually convert this decimal value to hexadecimal, getting 340. This means that the character set extension class file corresponding to JA16SJIS would be oracle/i18n/data/lx20340.glb.
You can reduce the size of orai18n.jar by including only the character set classes you use in your application. To do so, use the following steps:
Unpack orai18n.jar into a temporary directory.
Delete all files in your temporary directory except the lx20OracleCharacterSetId.glb files that your application uses and the following 18 class files:
oracle/i18n/util/ClassLoaderChooser.class
oracle/i18n/util/ConverterArchive.class
oracle/i18n/util/GDKMessage.class
oracle/i18n/util/GDKOracleMetaData.class
oracle/i18n/util/OraClassLoader.class
oracle/i18n/util/OraResourceBundle.class
oracle/i18n/util/message/Messages.class
oracle/i18n/text/converter/CharacterConverter12Byte.class
oracle/i18n/text/converter/CharacterConverterOGS.class
oracle/i18n/text/converter/CharacterConverter1Byte.class
oracle/i18n/text/converter/CharacterConverterGB18030.class
oracle/i18n/text/converter/CharacterConverterJAEUC.class
oracle/i18n/text/converter/CharacterConverterLC.class
oracle/i18n/text/converter/CharacterConverterLCFixed.class
oracle/i18n/text/converter/CharacterConverterZHTEUC.class
oracle/i18n/text/converter/CharacterConverter2ByteFixed.class
oracle/i18n/text/converter/CharacterConverterSJIS.class
oracle/i18n/text/converter/CharacterConverterShift.class
Make sure that your temporary directory has the same directory structure as the original package. The class files and glb files should be in the following directories:
oracle/i18n/data/—OracleCharacterSetId.glb files
oracle/i18n/util/—6 class files
oracle/i18n/util/message/—1 class file
oracle/i18n/text/converter/—11 class files
Using a different filename than the original orai18n.jar, create a JAR file from the temporary directory and add the JAR file to your CLASSPATH.
You can also include internationalized JDBC error message files selectively. The message files are included in classes*.* under the name oracle/jdbc/driver/Messages_*.properties.
By default, oracle.jdbc.OraclePreparedStatement treats all columns as CHAR. To insert Java strings into NCHAR, NVARCHAR2, and NCLOB columns, developers had to invoke setFormOfUse() on each national-language column. At this release, if you set the system property oracle.jdbc.defaultNChar to true, JDBC treats all character columns as being national-language. The default value for defaultNChar is false.
To set defaultNChar, you specify a command line like:
java -Doracle.jdbc.defaultNChar=true myApplication
If you prefer, your application can specify defaultNChar as a connection property.
After this property is set, your application can access NCHAR, NVARCHAR2, or NCLOB data without invoking setFormOfUse(). For example:
PreparedStatement pstmt =
conn.prepareStatement("insert into TEST values(?,?,?)");
pstmt.setInt(1, 1); // NUMBER column
pstmt.setString(2, myUnicodeString1); // NVARCHAR2 column
pstmt.setString(3, myUnicodeString2); // NCHAR column
pstmt.execute();
However, if you set defaultNChar to true and then access CHAR columns, the database will implicitly convert all CHAR data into NCHAR. This conversion has a substantial performance impact. To avoid this, call setFormOfUse(4,OraclePreparedStatement.FORM_CHAR) for each CHAR referred to in the statement. For example:
PreparedStatement pstmt =
conn.prepareStatement("insert into TEST values(?,?,?)");
pstmt.setInt(1, 1); // NUMBER column
pstmt.setString(2, myUnicodeString1); // NVARCHAR2 column
pstmt.setString(3, myUnicodeString2); // NCHAR column
pstmt.setFormOfUse(4, OraclePreparedStatement.FORM_CHAR);
pstmt.setString(4, myString); // CHAR column
pstmt.execute();
Here are a few examples of commonly used Java methods for JDBC that rely heavily on character set conversion:
The java.sql.ResultSet methods getString() and getUnicodeStream() return values from the database as Java strings and as a stream of Unicode characters, respectively.
The oracle.sql.CLOB method getCharacterStream() returns the contents of a CLOB as a Unicode stream.
The oracle.sql.CHAR methods getString(), toString(), and getStringWithReplacement() convert the following data to strings:
getString(): This converts the sequence of characters represented by the CHAR object to a string and returns a Java String object.
toString(): This is identical to getString(), but if the character set is not recognized, then toString() returns a hexadecimal representation of the CHAR data.
getStringWithReplacement(): This is identical to getString(), except characters that have no Unicode representation in the character set of this CHAR object are replaced by a default replacement character.