This works fine.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import oracle.jdbc.OraclePreparedStatement;
import oracle.jdbc.OracleTypes;
public class NChar {
public static void main(String[] args) {
try {
String url = "jdbc:oracle:thin:@dev-idr.xyz.com:1521:training";
Connection conn;
Statement stmt;
Class.forName("oracle.jdbc.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","dev_user");
/* Set the password */
connProps.setProperty("password","secret");
/* Set the defaultNChar property to true */
//connProps.setProperty("oracle.jdbc.defaultNChar","true");
//connProps.setProperty("oracle.jdbc.convertNcharLiterals","true");
conn = DriverManager.getConnection(url,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.setFormOfUse(3,(short)OraclePreparedStatement.FORM_NCHAR );
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 === start fetching");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from product_detail");
while(rs.next()){
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
System.out.println(rs.getString(4));
}
conn.close();
} catch( SQLException sqEx ) {
sqEx.printStackTrace();
} catch( Exception ex ) {
ex.printStackTrace();
}
}
}
===================================
If you want to use an existing connection
insert(........){
sLogger
.debug("Inserting ElementBean to FS_CONFIG_ELEMENT table ..");
OraclePreparedStatement lStmt = null;
String sql = "INSERT INTO FS_CONFIG_ELEMENT FSCE (CONFIG_ID,ELEMENT_ID,SORT_ORDER,LANG_CD,ELEMENT_VALUE,PART_ID,PART_FOOTNOTE_INDICATOR) VALUES (?,?,?,?,?,?,?)";
// Transaction should be started before calling this.
//Should we close this connection or is it the same connection that ODS is using ?
java.sql.Connection lConnection = ((JDBCObjectDataSource)lODS).getConnection();
lStmt = (OraclePreparedStatement)lConnection.prepareStatement(sql);
lStmt.setInt(1, aFSElementBean.getConfigId());
lStmt.setInt(2, aFSElementBean.getElementId());
lStmt.setInt(3, aFSElementBean.getSortOrder());
lStmt.setString(4, aFSElementBean.getLangCd());
//NCLOB or NCHAR or NVARCHAR Column
lStmt.setFormOfUse(5,(short)OraclePreparedStatement.FORM_NCHAR);
lStmt.setString(5, aFSElementBean.getDefaultValue());
lStmt.setInt(6, aFSElementBean.getPartId());
lStmt.setString(7, aFSElementBean.getPartFootNoteInd());
// Execute the query //
lStmt.execute();
sLogger.debug("Inserting ElementBean successful.");
}
// ELEMENT_VALUE here can be NCHAR or NVARCHAR or NCLOB
|