Character encoding while reading data using Java-JDBC from Oracle database

牧云@^-^@ 提交于 2019-12-21 21:24:15

问题


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 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?



回答2:


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.



来源:https://stackoverflow.com/questions/3378724/character-encoding-while-reading-data-using-java-jdbc-from-oracle-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!