本来想用stream流读出clob类型数据到update页面中的,不知道什么原因是乱码
不过还好,clob类型有getSubString方法
model
创建一个用于存贮clob类型转化后的String属性
package com.zk.model.business;
import java.sql.Clob;
import java.sql.Date;
import oracle.sql.CLOB;
/**
* Issueinfo entity. @author MyEclipse Persistence Tools
*/
public class Issueinfo implements java.io.Serializable {
// Fields
private String recid;
private String title;
private Date issuetim;
private String infosort;
private String worker;
private Clob content;
private String strContent;
public String getStrContent() {
return strContent;
}
public void setStrContent(String strContent) {
this.strContent = strContent;
}
/** default constructor */
public Issueinfo() {
}
/** minimal constructor */
public Issueinfo(String recid) {
this.recid = recid;
}
/** full constructor */
public Issueinfo(String recid, String title, Date issuetim,
String infosort, String worker, Clob content) {
this.recid = recid;
this.title = title;
this.issuetim = issuetim;
this.infosort = infosort;
this.worker = worker;
this.content = content;
}
// Property accessors
public String getRecid() {
return this.recid;
}
public void setRecid(String recid) {
this.recid = recid;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getIssuetim() {
return this.issuetim;
}
public void setIssuetim(Date issuetim) {
this.issuetim = issuetim;
}
public String getInfosort() {
return this.infosort;
}
public void setInfosort(String infosort) {
this.infosort = infosort;
}
public String getWorker() {
return this.worker;
}
public void setWorker(String worker) {
this.worker = worker;
}
public Clob getContent() {
return this.content;
}
public void setContent(Clob content) {
this.content = content;
}
}
model.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.zk.model.business.Issueinfo" table="ISSUEINFO" schema="PAWNSYS"> <id name="recid" type="java.lang.String"> <column name="RECID" length="12" /> <generator class="assigned"></generator> </id> <property name="title" type="java.lang.String"> <column name="TITLE" length="100" /> </property> <property name="issuetim" type="java.sql.Date"> <column name="ISSUETIM" length="7" /> </property> <property name="infosort" type="java.lang.String"> <column name="INFOSORT" length="1" /> </property> <property name="worker" type="java.lang.String"> <column name="WORKER" length="30" /> </property> <property name="content" type="java.sql.Clob"> <column name="CONTENT"/> </property> </class> </hibernate-mapping>
Dao
对象写入库
public void addObj(Issueinfo obj) throws LException {
try {
Session session = this.getSession();
Transaction tran = session.beginTransaction();
obj.setContent(Hibernate.createClob(" "));// 注意,这里的参数是个空格,先新增一个空的Clob进去
session.save(obj);
session.flush();// 强制执行
session.refresh(obj, LockMode.UPGRADE);
SerializableClob sc = (SerializableClob) obj.getContent();// kybasicInfo.getInfoContent()是Clob类型的
Clob wrapclob = sc.getWrappedClob();// 这里的Clob是java.sql.Clob
CLOB clob = (CLOB) wrapclob;// 这里的CLOB是oracle.sql.CLOB
Writer writer = clob.getCharacterOutputStream();
writer.write(obj.getStrContent());// kybasicInfo.getInfoContentToString()是String类型的,在action里就是传这个进来,然后再通过文件流形式写成CLOB字段中
writer.close();
session.save(obj);
tran.commit();
} catch (RuntimeException re) {
throw re;
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
对象从库中取出
public Issueinfo getInfo(String recid, String infosort) throws LException {
Session session = this.getSession();
session.beginTransaction();
Connection con = null;
Statement st = null;
ResultSet rs = null;
Issueinfo is = new Issueinfo();
try {
con = session.connection();
st = con.createStatement();
rs = st.executeQuery("select * from issueinfo where recid='"
+ recid + "' and infosort='" + infosort + "'");
if (rs.next()) {
Clob content = rs.getClob("CONTENT");//取出clob字段
// clob转换成String
long longLen = content.length();//长度
System.out.println(longLen);
String rtn = content.getSubString(1L, (int) longLen);//通过此方法可以将clob以String方式输出
is.setRecid(rs.getString("RECID"));
is.setIssuetim(rs.getDate("ISSUETIM"));
is.setInfosort(rs.getString("INFOSORT"));
is.setTitle(rs.getString("TITLE"));
is.setWorker(rs.getString("WORKER"));
is.setStrContent(rtn);//存贮在临时属性中
System.out.println(rtn);
}
} catch (SQLException e) { // TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (rs != null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (st != null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
session.close();
}
return is;
}
view
在取出strContent时注意
<s:property value="" />默认输入是过滤html
加上escape=false,html字符串转化为html输入
<FCK:editor instanceName="content" toolbarSet="My" width="660" height="400"> <jsp:attribute name="value"> <s:property value="isinfo.strContent" escape="false" /> </jsp:attribute> </FCK:editor>