Character encoding while reading data using Java-JDBC from Oracle database
We have data stored in oracle 10g db which contains french character set. The requirement is to read the data and generate a output file using Java.
I checked the validity of the data in Oracle db via SQL*plus and it looks good.
From windows:
set NLS_LANG=AMERICAN.AL32UTF8
sqlplus scott/tiger
sql> select billing_address from MYTABLE t where ADDRESS_ID=1 ;
billing_address
----------------------开发者_如何学Python-
MONTRÉAL QUÉ
Now when I read the table from Java to generate a output file, the character is all garbled and I see question marks in place of É.
Is there any special encoding that I need to set when I try to read/write the data in Java.
Am using the ojdbc14.jar
and setting the encoding as UTF-8.
Update: Here's my java code snippet.
Charset cs1 = Charset.forName("UTF-8");
PreparedStatement pStmt = conn.prepareStatement("select * from talbe where address_id=1");
ResultSet rs = pStmt.executeQuery();
Writer w = null;
FileOutputStream fos = null;
if(rs.next()) {
String billingaddress = rs.getString("BILLING_ADDRESS");
fos = new FileOutputStream(new File("myout.dat"));
w = new BufferedWriter(new OutputStreamWriter(fos,cs1));
w.write(billingaddress);
}
A couple of things to check...
- Your jdbc url should have
?useUnicode=true&characterEncoding=utf8
in it somewhere - Your JVM should have all the different char-sets installed that you need
- Maybe something is happening in the code to write to file/read from file
- Can you post some of your java code if your problem still persists?
Actually the problem was with the initial charset that was set while loading the data into the oracle database. we changed the charset in the sql*loader control file and it works fine now.
精彩评论