This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem of using setCharacterStream method to insert varchar2 data in ORACLE816

 
Jason Wu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,i have a problem in inserting into table in oracle816.My OS platform is win2000 Simplified.I want to insert some chinese simplified data in Oracle.Although data are inserted,but the result data is not in proper order.For example,the sql "insert into table(A,B,C) values ('a....','b....','c....')" can be executed successfully.But the result data in table is
A B C
---------
a............ b.......... c.......
NOTE:"u........" means the length of character string is over 2000
And the returned result order is wrong.
Can anyone give some ideas?Here are my code,you can try it.Thanks.
BTW:my database server characterSet is ZHS16GBK,you can follow this to setup your testing envirement.
c:\svrmgrl
sql>connect internal
sql>select name,value$ from props$;
sql>update props$ set value$='ZHS16GBK' where name='NLS_CHARACTERSET';
sql>commit;
sql>update props$ set value@='ZHS16GBK' where name='NLS_NCHAR_CHARACTERSET';
sql>commit;
=======================================================================================================
import java.io.*;
import java.sql.*;
import java.util.*;
import oracle.sql.*;
public class testDB {
public static void main(String[] args) {
Connection conn = null;
int maxRowSize = 1000;
try{
long before = System.currentTimeMillis();
Class.forName("oracle.jdbc.driver.OracleDriver");
//conn = DriverManager.getConnection("jdbc racle ci8:@sem","bdx","bdx");
conn = DriverManager.getConnection("jdbc racle:thin:@why:1521 radb","ecomm","ecomm");
//createTable(conn); // this is to create table
case1(conn);
//dropTable(conn);
}catch(Exception e){
System.out.println("the exception="+e.getMessage());
e.printStackTrace();
} finally {
try {
conn.close();
} catch (Exception e) {}
}
}
private static void case1(Connection dbConnection) {
int resultCount = -2;
try {
String c2000 = "", c4000 = "", c4001 = "";
for(int i = 0; i < 2000; i++) {
c2000 += "a";
}
for(int i = 0; i < 2000; i++) {
c4000 += "b";
c4001 += "c";
}
//char[] cr = new char[c4001.length()];
//c4001.getChars(0, c4001.length(), cr, 0);
Reader sr2000 = new StringReader(c2000);
Reader sr4000 = new StringReader(c4000.toString());
Reader sr4001 = new StringReader(c4001.toString());
//Reader sr4001 = new CharArrayReader(cr);
System.out.println("c2000 length:" + c2000.length());
System.out.println("c4000 length:" + c4000.length());
System.out.println("c4001 length:" + c4001.length());
String queryStr = "insert into test(c4000, c4001, c2000) values(?,?,?)";
PreparedStatement pst = dbConnection.prepareStatement(queryStr);
pst.setCharacterStream(1, sr4000, c4000.length());
//pst.setString(2, "abcd");
pst.setCharacterStream(2, sr4001, c4001.length());
pst.setCharacterStream(3, sr2000, c2000.length());
/*
pst.setString(1,c4000);
pst.setString(2,c4001);
pst.setString(3,c2000);
*/
pst.executeUpdate();
pst.close();
} catch(Exception se) {
System.out.println("Unable to update" + se);
}
}
private static void createTable(Connection dbConnection) {
int resultCount = -2;
try {
String queryStr = "create table test(c2000 varchar2(4000), c4000 varchar2(4000), c4001 varchar2(4000))";
PreparedStatement pst = dbConnection.prepareStatement(queryStr);
resultCount = pst.executeUpdate();
pst.close();
} catch(Exception se) {
System.out.println("Unable to create" + se);
}
}
private static void dropTable(Connection dbConnection) {
int resultCount = -2;
try {
String queryStr = "drop table test";
PreparedStatement pst = dbConnection.prepareStatement(queryStr);
resultCount = pst.executeUpdate();
pst.close();
} catch(Exception se) {
System.out.println("Unable to drop" + se);
}
}
private static Timestamp toTimestamp(long time){
return new Timestamp(time);
}
private static String[] toArray(ArrayList arr){
String[] strArr = new String[arr.size()];
for (int i=0;i strArr[i] = (String)arr.get(i);
}
return strArr;
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic