java操作資料庫中的Clob型別
首先是POJO類:
import java.sql.Clob;
import java.util.Date;
/**
*
* @author coffee
*
*/
public class KybasicInfo implements java.io.Serializable {
private Long infoId;
private String infoTitle;
private String infoKeyword;
private Clob infoContent; //注意這個地方是java.sql.Clob型別的,生成預設是String型別的,需要手工改一下
private String infoContentToString; //這個是和infoContent做轉換時,手工新增的一個,不和資料庫裡的欄位進行對應的
private Long infoTop;
private String userSno;
private Date infoTime;
private Long icId;
private String infoStandby1;
private String infoStandby2;
private String infoStandby3;
private Long infoStandby4;
private Long infoStandby5;
private Long infoStandby6;
public KybasicInfo() {
}
public KybasicInfo(String infoTitle, String infoKeyword,
Clob infoContent, Long infoTop, String userSno,
Date infoTime, Long icId, String infoStandby1, String infoStandby2,
String infoStandby3, Long infoStandby4, Long infoStandby5,
Long infoStandby6) {
this.infoTitle = infoTitle;
this.infoKeyword = infoKeyword;
this.infoContent = infoContent;
this.infoTop = infoTop;
this.userSno = userSno;
this.infoTime = infoTime;
this.icId=icId;
this.infoStandby1 = infoStandby1;
this.infoStandby2 = infoStandby2;
this.infoStandby3 = infoStandby3;
this.infoStandby4 = infoStandby4;
this.infoStandby5 = infoStandby5;
this.infoStandby6 = infoStandby6;
}
public Long getInfoId() {
return this.infoId;
}
public void setInfoId(Long infoId) {
this.infoId = infoId;
}
public String getInfoTitle() {
return this.infoTitle;
}
public void setInfoTitle(String infoTitle) {
this.infoTitle = infoTitle;
}
public String getInfoKeyword() {
return this.infoKeyword;
}
public void setInfoKeyword(String infoKeyword) {
this.infoKeyword = infoKeyword;
}
public Clob getInfoContent() {
return this.infoContent;
}
public void setInfoContent(Clob infoContent) {
this.infoContent = infoContent;
}
public Long getInfoTop() {
return this.infoTop;
}
public void setInfoTop(Long infoTop) {
this.infoTop = infoTop;
}
public Date getInfoTime() {
return this.infoTime;
}
public void setInfoTime(Date infoTime) {
this.infoTime = infoTime;
}
public String getInfoStandby1() {
return this.infoStandby1;
}
public void setInfoStandby1(String infoStandby1) {
this.infoStandby1 = infoStandby1;
}
public String getInfoStandby2() {
return this.infoStandby2;
}
public void setInfoStandby2(String infoStandby2) {
this.infoStandby2 = infoStandby2;
}
public String getInfoStandby3() {
return this.infoStandby3;
}
public void setInfoStandby3(String infoStandby3) {
this.infoStandby3 = infoStandby3;
}
public Long getInfoStandby4() {
return this.infoStandby4;
}
public void setInfoStandby4(Long infoStandby4) {
this.infoStandby4 = infoStandby4;
}
public Long getInfoStandby5() {
return this.infoStandby5;
}
public void setInfoStandby5(Long infoStandby5) {
this.infoStandby5 = infoStandby5;
}
public Long getInfoStandby6() {
return this.infoStandby6;
}
public void setInfoStandby6(Long infoStandby6) {
this.infoStandby6 = infoStandby6;
}
public String getUserSno() {
return userSno;
}
public void setUserSno(String userSno) {
this.userSno = userSno;
}
public Long getIcId() {
return icId;
}
public void setIcId(Long icId) {
this.icId = icId;
}
public String getInfoContentToString() {
return infoContentToString;
}
public void setInfoContentToString(String infoContentToString) {
this.infoContentToString = infoContentToString;
}
}
hbm.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">
<hibernate-mapping>
<class name="kymanage.kybasic.vo.KybasicInfo" table="KYBASIC_INFO" schema="BL">
<id name="infoId" type="java.lang.Long">
<column name="INFO_ID" precision="22" scale="0" />
<generator class="sequence">
<param name="sequence">INFO_ID</param>
</generator>
</id>
<property name="infoTitle" type="java.lang.String">
<column name="INFO_TITLE" length="100" />
</property>
<property name="infoKeyword" type="java.lang.String">
<column name="INFO_KEYWORD" length="100" />
</property>
<!--注意:這裡要將預設生成的String型別改成java.sql.Clob-->
<property name="infoContent" type="java.sql.Clob">
<column name="INFO_CONTENT" />
</property>
<property name="infoTop" type="java.lang.Long">
<column name="INFO_TOP" precision="38" scale="0" />
</property>
<property name="userSno" type="java.lang.String">
<column name="USER_SNO" length="100" />
</property>
<property name="infoTime" type="java.util.Date">
<column name="INFO_TIME" length="7" />
</property>
<property name="icId" type="java.lang.Long">
<column name="IC_ID" precision="38" scale="0" />
</property>
<property name="infoStandby1" type="java.lang.String">
<column name="INFO_STANDBY1" length="100" />
</property>
<property name="infoStandby2" type="java.lang.String">
<column name="INFO_STANDBY2" length="100" />
</property>
<property name="infoStandby3" type="java.lang.String">
<column name="INFO_STANDBY3" length="100" />
</property>
<property name="infoStandby4" type="java.lang.Long">
<column name="INFO_STANDBY4" precision="38" scale="0" />
</property>
<property name="infoStandby5" type="java.lang.Long">
<column name="INFO_STANDBY5" precision="22" scale="0" />
</property>
<property name="infoStandby6" type="java.lang.Long">
<column name="INFO_STANDBY6" precision="22" scale="0" />
</property>
</class>
</hibernate-mapping>
hibernate DAO類:
import oracle.sql.CLOB;
import java.io.IOException;
import java.io.Writer;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.lob.SerializableClob;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
* kybasicInfo
*
* @author coffee
*
*/
public class KybasicInfoDAOImpl extends HibernateDaoSupport {
protected void initDao() {
}
//新增,在action裡將表單中的這個大文字按正常情況傳過來,也就是傳String型別過來,然後,再在DAO裡做Clob型別轉換,
//這裡面可能會有其它層來傳參,但最終還是將傳過來的Stirng轉換成Clob型的,然後通過檔案流形式寫入到CLOB欄位中去
public void save(KybasicInfo kybasicInfo) {
try {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
Transaction tran=session.beginTransaction();
kybasicInfo.setInfoContent(Hibernate.createClob(" "));//注意,這裡的引數是個空格,先新增一個空的Clob進去
session.save(kybasicInfo);
session.flush();//強制執行
session.refresh(kybasicInfo,LockMode.UPGRADE);
SerializableClob sc=(SerializableClob)kybasicInfo.getInfoContent();//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(kybasicInfo.getInfoContentToString());//kybasicInfo.getInfoContentToString()是String型別的,在action裡就是傳這個進來,然後再通過檔案流形式寫成CLOB欄位中
writer.close();
session.save(kybasicInfo);
tran.commit();
} catch (RuntimeException re) {
throw re;
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//更新,操作的新增是一樣的,都是先接收String型別的引數過來,然後再將String的轉成CLOB型別的
public void update(KybasicInfo persistentInstance) {
try {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
Transaction tran=session.beginTransaction();
persistentInstance.setInfoContent(Hibernate.createClob(" "));
session.update(persistentInstance);
session.flush();
session.refresh(persistentInstance,LockMode.UPGRADE);
SerializableClob sc=(SerializableClob)persistentInstance.getInfoContent();
Clob wrapclob=sc.getWrappedClob();
CLOB clob=(CLOB)wrapclob;
Writer writer=clob.getCharacterOutputStream();
writer.write(persistentInstance.getInfoContentToString());
writer.close();
session.update(persistentInstance);
tran.commit();
} catch (RuntimeException re) {
throw re;
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//讀取Clob欄位值
public KybasicInfo findById(java.lang.Long id) {
try {
KybasicInfo instance = (KybasicInfo) getHibernateTemplate().get(
"kymanage.kybasic.vo.KybasicInfo", id);
Clob clob=instance.getInfoContent();//取得Clob的值
if(clob!=null){
String clobString="";
try {
clobString = clob.getSubString(1, (int)clob.length());//將Clob型別的值轉換成String型別的值
instance.setInfoContentToString(clobString);//通過setter方法,設定String值,然後就可以通過instance.getInfoContentToString()來取值了
} catch (SQLException e) {
e.printStackTrace();
}
}
return instance;
} catch (RuntimeException re) {
throw re;
}
}
public static KybasicInfoDAOImpl getFromApplicationContext(
ApplicationContext ctx) {
return (KybasicInfoDAOImpl) ctx.getBean("KybasicInfoDAO");
}
}