Java's answer to NCHAR/ NVARCHAR? [message #91226] |
Wed, 05 June 2002 03:47 |
PD
Messages: 3 Registered: November 1999
|
Junior Member |
|
|
I have a Oracle8i database (with UTF-8 as its National Language), and using 8i classes12.zip as the OCI8 JDBC Driver. I got a table called UNICODE_ENTRY, with 2 columns UNICODE_TEXT(NVARCHAR(255)) and ENCODING(VARCHAR2(128)).
I'm trying to enter a chinese String encoded in UTF-8 into the UNICODE_TEXT column using the following code segment
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
sql.append( "INSERT INTO unicode_entry ( unicode_text, encoding ) " );
sql.append( "VALUES( ?, ? )" );
statement = connection.prepareStatement(sql.toString());
java.io.InputStream ins = new java.io.ByteArrayInputStream(text.getBytes(encoding));
java.io.Reader reader = new java.io.BufferedReader(new java.io.InputStreamReader(ins, encoding));
statement.setCharacterStream(1, reader, text.length() );
statement.setString(2, encoding);
statement.executeUpdate();
But this doesnt seem to work, it giving the following error...
java.sql.SQLException: ORA-12704: character set mismatch
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:168)
at oracle.jdbc.oci8.OCIDBAccess.check_error(OCIDBAccess.java:1597)
at oracle.jdbc.oci8.OCIDBAccess.executeFetch(OCIDBAccess.java:1209)
at oracle.jdbc.oci8.OCIDBAccess.parseExecuteFetch(OCIDBAccess.java:1321)
at oracle.jdbc.driver.OracleStatement.executeNonQuery(OracleStatement.java:1446)
at oracle.jdbc.driver.OracleStatement.doExecuteOther(OracleStatement.java:1371)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1900)
at oracle.jdbc.driver.OracleStatement.executeUpdate(OracleStatement.java:693)
at UnicodeAccess.writeToOracleDatabase(UnicodeAccess.java:239)
at UnicodeAccess.main(UnicodeAccess.java:43)
I tried using simply statement.setString(1, text); That didnt work either, gave the same error.
Then I tried using Statement instead of PreparedStatement, that didnt work either. Same error!
The probable solution is to use the following lines of code:
statement = connection.prepareStatement(sql.toString());
((OraclePreparedStatement)statement).setFormOfUse(1, OraclePreparedStatement.FORM_NCHAR);
statement.setString(1,text);
But that would work only with a 9i Database and a 9i Driver. I need a solution to the 8i platform...
Can anyone suggest me where I'm going wrong, or how to simply enter a chinese String encoded into UTF-8 to NVARCHR2 column in Oracle8i DB?
Thanks in advance
PD
|
|
|
|
|
This works [message #91538 is a reply to message #91496] |
Fri, 25 October 2002 08:02 |
Bruce Koski
Messages: 3 Registered: October 2002
|
Junior Member |
|
|
reparedStatement ps = con.prepareStatement(
"insert into T (F) values(translate(? using nchar_cs)")
ps.setString(1, unicodeVariable)
ps.executeUpdate()
|
|
|