使用 Java-JDBC 从 Oracle 数据库读取数据时的字符编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3378724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Character encoding while reading data using Java-JDBC from Oracle database
提问by Raghavan
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.
我们将数据存储在 oracle 10g db 中,其中包含法语字符集。要求是使用Java读取数据并生成输出文件。
I checked the validity of the data in Oracle db via SQL*plus and it looks good.
我通过 SQL*plus 检查了 Oracle db 中数据的有效性,看起来不错。
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 é.
现在,当我从 Java 中读取表以生成输出文件时,字符全是乱码,我看到问号代替了 é。
Is there any special encoding that I need to set when I try to read/write the data in Java.
当我尝试在 Java 中读/写数据时,是否需要设置任何特殊的编码。
Am using the ojdbc14.jar
and setting the encoding as UTF-8.
我正在使用ojdbc14.jar
并将编码设置为 UTF-8。
Update: Here's my java code snippet.
更新:这是我的 java 代码片段。
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);
}
采纳答案by Raghavan
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 数据库时设置的初始字符集。我们更改了 sql*loader 控制文件中的字符集,现在可以正常工作了。
回答by Java Drinker
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?
- 你的 jdbc url 应该
?useUnicode=true&characterEncoding=utf8
在某个地方 - 您的 JVM 应该安装了您需要的所有不同的字符集
- 也许代码中发生了一些事情来写入文件/从文件中读取
- 如果您的问题仍然存在,您可以发布一些 Java 代码吗?