解決thin JDBC 對字元串超長限制的問題。
原始錯誤是: org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [17070];
--- The error occurred in com/sinosoft/card/cardKind/db/dao/IC_CARDKINDNOTIFY_SqlMap.xml.
--- The error occurred while applying a parameter map.
--- Check the IC_CARDKINDNOTIFY.abatorgenerated_insert-InlineParameterMap.
--- Check the parameter mapping for the 'value' property.
--- Cause: java.sql.SQLException: 資料大小超出此類型的最大值: 2986; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in com/sinosoft/card/cardKind/db/dao/IC_CARDKINDNOTIFY_SqlMap.xml.
--- The error occurred while applying a parameter map.
--- Check the IC_CARDKINDNOTIFY.abatorgenerated_insert-InlineParameterMap.
--- Check the parameter mapping for the 'value' property.
--- Cause: java.sql.SQLException: 資料大小超出此類型的最大值: 2986
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.translate(SQLStateSQLExceptionTranslator.java:124)
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.translate(SQLErrorCodeSQLExceptionTranslator.java:322)
分析結果見
http://blog.csdn.net/ruanee/archive/2006/03/24/637213.aspx
http://www.tomjamescn.cn/?p=63
後面的文章直接hack了ibatis的StringTypeHandler,完全沒有必要
我的解決方法:1、擴充StringTypeHandler為LargeStringTypeHandler
Java代碼

- package com.xxxx.component.ibatis.typehandler;
- import java.io.StringReader;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import com.ibatis.sqlmap.engine.type.StringTypeHandler;
- public class LargeStringTypeHandler extends StringTypeHandler {
- public void setParameter(PreparedStatement ps, int i, Object parameter, String jdbcType)
- throws SQLException {
- String s = (String)parameter;
- if (s.length() < 667) {
- //assume that all characters are chinese characters.
- super.setParameter(ps, i, parameter, jdbcType);
- }else{
- //use setCharacterStream can insert more characters.
- ps.setCharacterStream(i, new StringReader(s), s.length());
- }
- }
- }
2、在SqlMapConfig.xml中添加一行
<typeHandler javaType="java.lang.String" callback="com.sinosoft.component.ibatis.typehandler.LargeStringTypeHandler"/>
問題解決。