天天看點

org.springframework.jdbc.UncategorizedSQLException

解決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代碼  

org.springframework.jdbc.UncategorizedSQLException
  1. package com.xxxx.component.ibatis.typehandler;  
  2. import java.io.StringReader;  
  3. import java.sql.PreparedStatement;  
  4. import java.sql.SQLException;  
  5. import com.ibatis.sqlmap.engine.type.StringTypeHandler;  
  6. public class LargeStringTypeHandler extends StringTypeHandler {  
  7.     public void setParameter(PreparedStatement ps, int i, Object parameter, String jdbcType)  
  8.     throws SQLException {  
  9.         String s = (String)parameter;  
  10.         if (s.length() < 667) {  
  11.             //assume that all characters are chinese characters.  
  12.             super.setParameter(ps, i, parameter, jdbcType);  
  13.         }else{  
  14.             //use setCharacterStream can insert more characters.  
  15.             ps.setCharacterStream(i, new StringReader(s), s.length());  
  16.         }  
  17.     }  
  18. }  

2、在SqlMapConfig.xml中添加一行

<typeHandler javaType="java.lang.String" callback="com.sinosoft.component.ibatis.typehandler.LargeStringTypeHandler"/>

問題解決。