使用Oracle数据库中的Java-JDBC读取数据时的字符编码 [英] Character encoding while reading data using Java-JDBC from Oracle database

查看:582
本文介绍了使用Oracle数据库中的Java-JDBC读取数据时的字符编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将数据存储在oracle 10g db中,其中包含法语字符集。要求是使用Java读取数据并生成输出文件。



我通过SQL * plus检查了Oracle数据库中的数据的有效性,它看起来不错。 / p>

从Windows:

 设置NLS_LANG = AMERICAN.AL32UTF8 
sqlplus scott / tiger
sql>从MYTABLE t中选择billing_address,其中ADDRESS_ID = 1;
billing_address
-----------------------
MONTRÉALQUÉ

现在,当我从Java中读取表以生成输出文件时,字符全部乱码,我看到问号代替É。



当我尝试在Java中读/写数据时,是否需要设置任何特殊编码。



使用 ojdbc14.jar 并将编码设置为UTF-8。






<

  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);
}


解决方案

其实问题是在将数据加载到oracle数据库时设置的初始字符集。我们更改了sql * loader控件文件中的字符集,现在可以正常工作。


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);
        }

解决方案

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.

这篇关于使用Oracle数据库中的Java-JDBC读取数据时的字符编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆