【问题标题】:japanese-character-encoding-in-java- from Db2 database来自 Db2 数据库的日语字符编码 in-java-
【发布时间】:2020-04-14 10:15:14
【问题描述】:

我从 db2 获取 clob 数据并存储在一个列表中,然后写入一个文本文件,但是日语字符没有被编码。

从 db2 获取数据

if (rs != null) {
                int slNo = 0;
                BufferedReader reader = null;
                int counter = 0;

                // Read the list of content next
                while (rs.next() && !rs.isClosed()) {
                    // Reserve the first entry in the
                    // result List for header column
                    if (counter == 0) {
                        resultList.add(null);
                        counter++;
                    }
                    reader = new BufferedReader(new InputStreamReader(rs.getAsciiStream(1)));

                    slNo = rs.getInt(2);

                    if (slNo == 1) {

                        resultList.set(0, reader.readLine());
                    } else {
                        // result data
                        resultList.add(reader.readLine());
                           }}}

写入文件

 FileOutputStream outObjectOutputStream = null;
 try {
        outObjectOutputStream = new FileOutputStream(absoluteFilePath);

        for (String line : resultList) {
            if (line != null) {
                outObjectOutputStream.write(line.getBytes());
                outObjectOutputStream.write("\n".getBytes());
                    }}'

【问题讨论】:

  • 我在数据库中有日语字符,当我写入 txt 文件时,它就像 Y'–*^
  • 记事本和windows操作系统
  • 考虑在 CLOB 列中使用 rs.getClob() 而不是 rs.getAsciiStream()。
  • 让我检查一下
  • 它不适合我

标签: java encoding db2 resultset


【解决方案1】:

您必须指定与 DB 中使用的字符编码相同的字符编码。如果表是 Unicode 表,则 DB2 对 CLOB 使用 UTF-8。

如果您已经集体读取了 CLOB 的内容(在将字符串写入流之前尝试System.out.println(line);),请尝试使用line.getBytes("UTF-8"); 而不是line.getBytes(); 写入内容。请参阅https://stackoverflow.com/a/12659462/3902663 了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 2011-12-03
    • 1970-01-01
    • 2019-08-27
    相关资源
    最近更新 更多