views:

174

answers:

3

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
-----------------------
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);
        }
+1  A: 

A couple of things to check...

  1. Your jdbc url should have ?useUnicode=true&characterEncoding=utf8 in it somewhere
  2. Your JVM should have all the different char-sets installed that you need
  3. 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?
Java Drinker
Point 1 is MySQL JDBC driver specific. He's using Oracle. It's however a good direction to look the cause for.
BalusC
A: 

(moved update into question, you should delete this "answer")

Raghavan
A: 

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.

Raghavan