开发者

How to query a UTF-8 database using JDBC

开发者 https://www.devze.com 2023-03-17 05:41 出处:网络
My DB2 database is using UTF-8 encoding. The source table has one colum with UTF-8 text like these lines below

My DB2 database is using UTF-8 encoding. The source table has one colum with UTF-8 text like these lines below

Greek: Μπορώ να φάω σπασμένα γυαλιά χωρίς να πάθω τίποτα

Czech: Mohu jíst sklo, neublíží mi

Serbian: Ја могу да једем стакло

My java code is doing this (.. snipped out irrelevant parts.. )

ResultSet rs = stmt.executeQuery("SELECT SAMPLE_TEXT FROM UNICODE_TEST");
File file = new File("Unicode.txt");
BufferedWriter outFile = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"UTF8"));
while (rs.next()) {
    outFile.write(rs.getString("SAMPLE_TEXT"));
    outFile.write( "\n" );
}

The result in my file don't look lik开发者_如何学JAVAe those in the source table at all. I get this instead

Greek: ΜποÏÏŽ να φάω σπασμένα γυαλιά χωÏίς να πάθω τίποτα

Czech : Mohu jíst sklo, neublíží mi

Serbian: Ја могу да једем Ñтакло

What did I miss?


Your Java code is ok. Are you sure you your file content is wrong? What you see can appear wrong if you have your terminal wrong, for example. Can you see ok other text files in that computer encoded as UTF-8? (If you are unsure, read it in hexa, get a dump with od, etc). If you are sure this is not your problem, then I'd point to some charset encoding setting in the JDBC connection (no experience with DB2).


I don't know what database you're using, but if it's MySQL, try adding characterEncoding=UTF-8 to your connection string.

0

精彩评论

暂无评论...
验证码 换一张
取 消