Dear all
大字段Clob的操作:hibernate没找到解决方案,ibatis可以把大字段转换成流的形式model.setMsgObj(stream.toByteArray())进行保存(对象中msgObj字段必须是byte[]类型)。BM上用的是hibernate,故用JDBC操作:
String sql = "select * from SYS_CONTRACT_MODE where ID = "+bean.getId()+" with ur";
SysContractModeDao dao = new SysContractModeDao();
List list1 = dao.qrySql(sql);
org.hibernate.lob.SerializableClob c = null;
Clob clob = null;
if (list1 != null && list1.size() > 0) {
Object[] ob = (Object[])list1.get(0);
c = (SerializableClob) ob[3];
SysContractMode sysContractMode = new SysContractMode();
java.math.BigInteger id = (BigInteger) ob[0];
//查找出来,对大字段的处理。
clob=(Clob)ob[3];
InputStream is=null;
if(clob !=null){
try {
is = clob.getAsciiStream();
} catch (SQLException e) {
LoggerUtil.error(e);
} catch(Exception e){
LoggerUtil.error(e);
}
}
sysContractMode.setId(id);
sysContractMode.setSigningMode(ob[1]+"".trim());
sysContractMode.setModeName(ob[2]+"".trim());
sysContractMode.setModeTemplateName(c);
sysContractMode.setStatus(ob[4]+"".trim());
sysContractMode.setLastOperator(bean.getCommonDTO().getUserId().trim());
sysContractMode.setLastUpdateDatetime(new Date());
//这里在上面select出来,进行直接保存,没报错,可以通过,可惜数据没变动,不知道为什么。
dao.update(sysContractMode);
}
Session session = DBKernel.getSession();
FormFile file = bean.getFile();
InputStream inputStream = null;
java.io.InputStreamReader isr = null;
BufferedOutputStream stream=null;
FileOutputStream fstream=null;
try {
String sql2 = "UPDATE SYS_CONTRACT_MODE SET MODE_NAME=?,MODE_TEMPLATE_NAME=? WHERE ID="+bean.getId();
java.sql.Connection conn = session.connection();
java.sql.PreparedStatement pstmt = conn.prepareStatement(sql2);
pstmt.setString(1,bean.getModeName());
//保存大字段,用JDBC的PreparedStatement有几种方法 (在这注意的是,lob字段的大小问题,当上传的文件太与你设置的大小就会报-301,22001错误!)
//第一种,文件转换成流的形式
File f1 = new File("c:\\web.xml文件详解.txt");
inputStream = new FileInputStream(f1);
pstmt.setAsciiStream(2,inputStream, (int)f1.length());
// 第二种,字符串转换成流的形式。 PreparedStatement里面还有直接存储pstmt.setClob(2, clob);对象的。
byte[] b = file.getFileData();
String childstr = new String(b,"gb2312");
// pstmt.setCharacterStream(2,new StringReader(childstr),childstr.length());
pstmt.executeUpdate();
conn.commit();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
fstream.flush();
fstream.close();
stream.flush();
stream.close();
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
session.close();
}