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:
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.
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.
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,