oracle.otnsamples.jdbc.NChar (Java2HTML)
/**
* @author Shrinivas Bhat
* @version 1.0
*
* Development Environment : Oracle JDeveloper 10g
* Name of the Application : NChar.java
*
* Creation/Modification History :
*
* Shrinvas Bhat 16-Feb-2004 Created
*
* Overview : This code sample demostrates how to use the defaultNChar
* proeprty when working with the SQL NCHAR datatypes (NCHAR,NVARCHAR2 and NCLOB).
*
* This requires the following table to be created in the database.
*
* CREATE TABLE product_detail (
* itemid NUMBER(4) , itemtype NVARCHAR2(50), description
* NVARCHAR2(500), langid VARCHAR2(50)
* );
*
**/


package oracle.otnsamples.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import java.util.Properties;
import oracle.jdbc.OracleDriver;
import oracle.jdbc.OraclePreparedStatement;
import oracle.jdbc.OracleTypes;

public class NChar {

public static void main(String[] args) {
try {

/* Register the driver */
DriverManager.registerDriver(new OracleDriver());

/* Set the connection properties in a java.util.Properties and pass it as
* a parameter to the getConnection method.
*/

Properties connProps = new Properties();

/* Set the database user name */
connProps.setProperty("user","scott");

/* Set the password */
connProps.setProperty("password","tiger");

/* Set the defaultNChar property to true */
connProps.setProperty("oracle.jdbc.defaultNChar","true");

/* Get the connection */
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:ora10g",
connProps
);

String query =
"INSERT INTO product_detail(itemid,itemtype,description,langid) "+
"VALUES ( ?, ?, ?, ? ) ";
OraclePreparedStatement pstmt = (OraclePreparedStatement)
conn.prepareStatement(query) ;

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

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

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

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

/* CHAR Column */
pstmt.setString(4,"Eng");

/* Execute the query */
pstmt.execute();

System.out.println(" Record inserted successfully ");

} catch( SQLException sqEx ) {
sqEx.printStackTrace();
} catch( Exception ex ) {
ex.printStackTrace();
}
}
}



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