How to specify NCHAR data binding as a system or connection

How to specify NCHAR data binding as a system or connection property

Date: February 16, 2004

How to specify NCHAR data binding as a system or connection property

After completing this How-to you should be able to:
  • Use defaultNChar property when working with the SQL NCHAR datatypes (NCHAR,NVARCHAR2 and NCLOB).
  • Improve the performance by overriding the default property for CHAR datatypes.

Introduction

This code sample demonstrates how to use the defaultNChar properly when working with the SQL NCHAR datatypes (NCHAR,NVARCHAR2 and NCLOB).

Reader should be familiar with

To understand this document the readers should have basic knowledge about JDBC and Oracle Database.

Description

Unicode is a universal character encoding scheme that allows the user to store information from any major language using a single character set. Unicode provides a unique code value for every character, regardless of the platform, program, or language.
The SQL NCHAR datatypes(NCHAR, NVARCHAR2, and NCLOB) allow the user to store all Unicode data in the database. Unicode characters can be stored in columns of these datatypes regardless of the setting of the database character set.

To insert Java strings into NCHAR, NVARCHAR2, and NCLOB columns, developers had to invoke setFormOfUse() on each national-language column.  This is because, by default, oracle.jdbc.OraclePreparedStatement treats all columns as CHAR, VARCHAR2, or CLOB. 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 yourApplicationName

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().

The code example below assumes that oracle.jdbc.defaultNChar is set to true and hence you do not have to call setFormOfUse() explicitly.


String query = "INSERT INTO product_detail(itemid,itemtype,description,langid) VALUES ( ?, ?, ?, ?)";

PreparedStatement pstmt = conn.prepareStatement(query) ;

pstmt.setInt(1,100); /* NUMBER Column */

pstmt.setString(2,"Book"); /* NVARCHAR2 Column */

pstmt.setString(3,"Oracle Database 10g"); /* NVARCHAR2 Column */

pstmt.setString(4,"Eng"); /* VARCHAR2 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,Const.CHAR) for each CHAR referred to in the statement.
The code below makes use of setFormOfUse() to overcome the performance overhead.


String query = "INSERT INTO product_detail(itemid,itemtype,description,langid) VALUES ( ?, ?, ?, ?)";

OraclePreparedStatement pstmt = (OraclePreparedStatement)conn.prepareStatement(query) ;

pstmt.setInt(1,100); /* NUMBER column */

pstmt.setString(2,"Book"); /* NVARCHAR2 column */

pstmt.setString(3,"Oracle Database 10g"); /* NVARCHAR2 column */

pstmt.setFormOfUse(4, Const.CHAR); /* Set the CHAR to access VARCHAR2 column */

pstmt.setString(4,"Eng"); /* VARCHAR2 column */

pstmt.execute();



You can view the complete source code here.

Pre-requisites for running the example

You will need the following to run this example -

  • JDK1.4.x or higher. This can be downloaded from here
  • Oracle Database 10g or higher running SQL*Net TCP/IP Listener. This can be downloaded from here.

  • Oracle Database 10g JDBC driver. This can be downloaded from here.

Deploying and Running the Sample

The source code for this sample is available here.  Copy and save this file under your working directory.

Step 1 Extract the sample source file by executing the following command. This will create NChar directory under your working directory.

jar -xvf NChar.jar
Step 1

From the sql prompt connect to the database with scott/tiger. Run the following command to create the product_detail table;

CREATE TABLE product_detail (
itemid NUMBER(4) , itemtype NVARCHAR2(50), description
NVARCHAR2(500), langid VARCHAR2(50)
);
Step 2 Include the ojdbc14.jar in the classpath by executing the following command.

export CLASSPATH=$ORACLE_HOME/jdbc/lib/ojdbc14.jar:$CLASSPATH

where, ORACLE_HOME is the directory where oracle database is installed.
Step 3

From the command prompt, move to NChar directory and compile the source file by executing the following command,

javac oracle/otnsamples/jdbc/NChar.java

Step 4

Run the Java program by executing the following command,

java oracle.otnsamples.jdbc.NChar

Alternatively you can also run the sample by specifying oracle.jdbc.defaultNChar as a system property.
Comment the connProps.setProperty("oracle.jdbc.defaultNChar","true") line in the NChar.java file and compile the source as given in Step3. Then execute the following command to run the sample,

java -Doracle.jdbc.defaultNChar=true oracle.otnsamples.jdbc.NChar

Summary

This How-To document explained how to use defaultNChar property when using NCHAR datatypes.  


Please enter your comments about this sample in the OTN Sample Code Discussion Forum.

 





E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy