package cn.yuanqiao.archive.common.typeHandler;
import com.alibaba.druid.proxy.jdbc.NClobProxyImpl;
import org.apache.ibatis.type.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.sql.*;
@MappedTypes(Clob.class)
@MappedJdbcTypes(JdbcType.CLOB)
@Component
public class NClobTypeHandler extends BaseTypeHandler<String> {
@Value("${YQ_DATABASE}")
private Integer databaseId;
@Override
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
StringReader reader=new StringReader(parameter);
ps.setCharacterStream(i,reader,parameter.length());
}
@Override
public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
String clobToString="";
try {
switch (databaseId) {
case 4://达梦数据库
NClobProxyImpl aa=(NClobProxyImpl) rs.getObject(columnName);
Clob rawClob = aa.getRawClob();
clobToString = ClobToString(rawClob);
break;
default:
clobToString="长文本类型,无法显示";
break;
}
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return clobToString;
}
@Override
public String getNullableResult(ResultSet rs, int i) throws SQLException {
String clobToString="";
try {
switch (databaseId) {
case 4:
NClobProxyImpl aa=(NClobProxyImpl) rs.getObject(i);
Clob rawClob = aa.getRawClob();
clobToString = ClobToString(rawClob);
break;
default:
clobToString="长文本类型,无法显示";
break;
}
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return clobToString;
}
@Override
public String getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
String clobToString="";
try {
switch (databaseId) {
case 4:
NClobProxyImpl aa=(NClobProxyImpl) callableStatement.getObject(i);
Clob rawClob = aa.getRawClob();
clobToString = ClobToString(rawClob);
break;
default:
clobToString="长文本类型,无法显示";
break;
}
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return clobToString;
}
private String ClobToString(Clob clob) throws SQLException, IOException {
String reString = "";
Reader is = clob.getCharacterStream();
BufferedReader br = new BufferedReader(is);
String s = br.readLine();
StringBuffer sb = new StringBuffer();
while (s != null) {
sb.append(s);
s = br.readLine();
}
reString = sb.toString();
return reString;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111