SqlException with message "Caught java.io.CharConversionException." and ERRORCODE=-4220

Technote (troubleshooting)

Problem(Abstract)

When an application uses the IBM Data Server Driver for JDBC and SQLJ (also known as the JCC driver) and is connected to a database with code set UTF-8 (code page 1208), it throws an SqlException with message including "Caught java.io.CharConversionException" and ERRORCODE=-4220 if the data in a character column that it queries contains a sequence of bytes that is not a valid UTF-8 string.

Symptom

An exception is thrown similar to this:

com.ibm.db2.jcc.am.SqlException: [jcc][t4][1065][12306][XXX.XXX.XXX] Caught java.io.CharConversionException. See attached Throwable for details. ERRORCODE=-4220, SQLSTATE=null [...] Caused by: java.nio.charset.MalformedInputException: Input length = XXX [...] Caused by: sun.io.MalformedInputException at sun.io.ByteToCharUTF8.convert(ByteToCharUTF8.java:XXX) [...]

Cause

The JCC driver throws the exception when data in a character column that it queries is not a valid string in the database code page.
The invalid data could have been input to the database in the following ways:

  • By running an SQL statement which writes byte values to the database. For example: INSERT INTO tab1 VALUES (X‘C3‘)
  • By running the IMPORT or LOAD commands for files which contain character data that is not in the code page of the client machine, so the appropriate code page conversion was not done. To make sure that the the appropriate code page conversion is done when running IMPORT or LOAD, specify the code page of the input file by including the "codepage=x" file type modifier (specifying the code page in place of x).

Diagnosing the problem

You can use the HEX function to find out what are the byte values in a character column.

For example, to find out the byte values in column COL1 in table TAB1, run: SELECT HEX(col1) FROM tab1

Resolving the problem

Update the invalid data with a valid UTF-8 character string.

Alternatively, for JCC driver versions that support it you can set the JCC configuration property db2.jcc.charsetDecoderEncoder=3 so that instead of throwing an exception the JCC driver returns the Unicode REPLACEMENT CHARACTER (U+FFFD) in place of a sequence of bytes that is not a valid UTF-8 string. The JCC configuration property db2.jcc.charsetDecoderEncoder is supported in versions of the JCC driver that come with DB2 LUW 9.5 Fix Pack 8 and later (APAR IC74896), DB2 LUW 9.7 Fix Pack 5 and later (APAR IC74895), and all DB2 LUW releases from DB2 10.1 onwards.
For example, suppose you have a Java program MyApp.java that executes an SQL query of a database column that contains a sequence of bytes that is not a valid string. When you run: java MyApp it throws an exception. But when you run: java -Ddb2.jcc.charsetDecoderEncoder=3 MyApp it returns the string with any invalid sequence of bytes replaced by the Unicode REPLACEMENT CHARACTER.
The Unicode REPLACEMENT CHARACTER often appears like this (a diamond with a question mark inside it) :

Related information

APAR IC78495 APAR IC74896 Making the setting for IBM Data Studio Making the setting for IBM Content Collector UDF to identify invalid data in a UTF-8 character colum How invalid data can get into a UTF-8 character column LOAD command IMPORT command HEX function

Community questions and discussion

By adding a comment, you accept our Terms of Use. Your comments entered on this IBM Support site do not represent the views or opinions of IBM. IBM, in its sole discretion, reserves the right to remove any comments from this site. IBM is not responsible for, and does not validate or confirm, the correctness or accuracy of any comments you post. IBM does not endorse any of your comments. All IBM comments are provided "AS IS" and are not warranted by IBM in any way.

时间: 2024-08-07 00:12:58

SqlException with message "Caught java.io.CharConversionException." and ERRORCODE=-4220的相关文章

(https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014550004)Topic: Caught java.io.CharConversionException. ERRORCODE=-4220, SQLSTATE=null

270002WDPN 3 Posts 0 people like this Pinned topic                                                              Caught java.io.CharConversionException. ERRORCODE=-4220, SQLSTATE=null Nov 5, 2010                                     |                  

java.io.CharConversionException: Not an ISO 8859-1 character:

java.io.CharConversionException: Not an ISO 8859-1 character: XXX 这个问题可能是因为outputstream输出中文字造成的影响. response.setContentType("text/html;charset=UTF-8");//response.getOutputStream().print("中文字"); //这行会出错response.getWriter().print("中文

关于db2处理特殊字段出现异常java.io.charConversionException

记录一下以前遇到的问题 在使用db2数据库处理数据的时候,碰到特殊字段,出现的问题 java.io.charConversionException 官方解决方法: db2.jcc.charsetDecoderEncoder=3 so that instead of throwing an exception the JCC driver returns the Unicode REPLACEMENT CHARACTER (U+FFFD) in place of a sequence of byt

java.sql.SQLException: null, message from server: “Host ‘xxx’ is not allowed to connect

java.sql.SQLException: null, message from server: “Host ‘xxx’ is not allowed to connect 2014年06月29日 ⁄ 综合 ⁄ 共 637字 ⁄ 字号 小 中 大 ⁄ 评论关闭 java.sql.SQLException: null,  message from server: "Host 'xxx' is not allowed to connect to this MySQL server": j

java.sql.SQLException: null, message from server: "Host 'xxx' is not allowed to connect to this MySQL server"

java.sql.SQLException: null,  message from server: "Host 'xxx' is not allowed to connect to this MySQL server": 表示该对象不是远程对象,不能通过该对象远程访问数据 解决: 方案一:改表: use mysql ;select user,host,password from user; update user set host = '%' where user='root'; 方

Java IO(文件操作工具类)

FileOperate实现的功能: 1. 返回文件夹中所有文件列表 2. 读取文本文件内容 3. 新建目录 4. 新建多级目录 5. 新建文件 6. 有编码方式的创建文件 7. 删除文件 8. 删除指定文件夹下所有文件 9. 复制单个文件 10. 复制整个文件夹的内容 11. 移动文件 12. 移动目录 13. 建立一个可以追加的bufferedwriter 14. 得到一个bufferedreader Java代码    package utils; import java.io.Buffer

远程mysql_java.sql.SQLException: null, message from server: "Host 'xxx' is not allowed to connect

最近在做一个项目,里面要用到远程mysql数据库. 我想把我想要实现的功能说一下吧: 1 /** 2 * 是这样的功能:我的机器是A,我现在先利用我自己写的一个jdbc方法<br> 3 * 调用远程的机器B上安装的数据库<br> 4 * 然后把我想要CRUD操作B机器上的数据库<br> 5 * 或者简单一点,可以这样认为,我现在在机器A上面有一些数据<br> 6 * 我要把这些数据插入到远程的机器B的mysql数据库里面去<br> 7 * 就是这

java io实例具体解释

import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileO

java io之图片存取

一:建表 二:获取数据库连接 1:导入mysql的驱动jar包,mysql-connector-java-5.1.8-bin.jar 2:写代码连接数据库,如下: 1 /** 2 * 3 */ 4 package com.hlcui.file; 5 6 import java.sql.Connection; 7 import java.sql.DriverManager; 8 import java.sql.SQLException; 9 10 /** 11 * @author Administ