數(shù)據(jù)庫(kù)CLOB、BLOB類型字段在SSH項(xiàng)目中的應(yīng)用
1. 數(shù)據(jù)庫(kù)設(shè)計(jì) 建表,設(shè)置字段類型

2.使用myeclipse反向工程 生成hibernate映射文件,需要修改。自動(dòng)生成的屬性名字分別是contentText和contentBinary,我們這里修改在前面加上模塊的縮寫

3.生成的PO類,也需要修改。修改為與hibernate映射文件中對(duì)應(yīng)的名字,這里還需要加兩個(gè)string變量來接收頁(yè)面上的數(shù)據(jù),contentText是插入clob字段數(shù)據(jù)時(shí)需要用到的,contentBinary是從數(shù)據(jù)庫(kù)中取出值處理之后,再設(shè)置到這個(gè)變量,方便界面上拿去展現(xiàn)

4. form里面設(shè)置如下。下面的紅圈圈對(duì)應(yīng)的是jsp頁(yè)面上控件的name

5. DAO中 主要代碼部分,在action中直接調(diào)用

2.使用myeclipse反向工程 生成hibernate映射文件,需要修改。自動(dòng)生成的屬性名字分別是contentText和contentBinary,我們這里修改在前面加上模塊的縮寫

3.生成的PO類,也需要修改。修改為與hibernate映射文件中對(duì)應(yīng)的名字,這里還需要加兩個(gè)string變量來接收頁(yè)面上的數(shù)據(jù),contentText是插入clob字段數(shù)據(jù)時(shí)需要用到的,contentBinary是從數(shù)據(jù)庫(kù)中取出值處理之后,再設(shè)置到這個(gè)變量,方便界面上拿去展現(xiàn)

4. form里面設(shè)置如下。下面的紅圈圈對(duì)應(yīng)的是jsp頁(yè)面上控件的name

5. DAO中 主要代碼部分,在action中直接調(diào)用
public void insertLaws(LawsForm lawsForm) throws Exception {
Laws laws = null ;
try {
laws = new Laws();
BeanUtils.copyProperties(laws, lawsForm);
LawsBclass lawsBclass = new LawsBclass() ;
lawsBclass.setBclassId(lawsForm.getBclassId());
laws.setLawsBclass(lawsBclass);
laws.setLawsContentBinary(Hibernate.createBlob(new byte[1]));
laws.setLawsContentText(Hibernate.createClob(" "));
laws.setIsaudit("0");
this.getHibernateTemplate().save(laws);
this.getHibernateTemplate().flush();
this.getHibernateTemplate().update(laws);
this.getHibernateTemplate().refresh(laws, LockMode.UPGRADE);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
// 向BLOB字段寫入內(nèi)容
SerializableBlob serializableBlob = (SerializableBlob) laws.getLawsContentBinary();
java.sql.Blob javablob = serializableBlob.getWrappedBlob() ;
oracle.sql.BLOB blob = (oracle.sql.BLOB) javablob ;
BufferedInputStream contentBin = new BufferedInputStream(
lawsForm.getContentBinaryFormFile().getInputStream());
BufferedOutputStream contentBout = new BufferedOutputStream(blob
.getBinaryOutputStream());
byte[] buffer = new byte[1024];
int length = 0;
while ((length = contentBin.read(buffer)) > 0) {
contentBout.write(buffer, 0, length);
}
contentBin.close();
contentBout.close();
// 向CLOB字段寫入內(nèi)容
SerializableClob serializableClob = (SerializableClob) laws.getLawsContentText();
java.sql.Clob javaclob = serializableClob.getWrappedClob();
oracle.sql.CLOB clob = (oracle.sql.CLOB)javaclob ;
String contentText = lawsForm.getContentText();
BufferedWriter bw = new BufferedWriter(clob.getCharacterOutputStream());
bw.write(contentText);
bw.close();
}
@SuppressWarnings("deprecation")
public void updateLaws(LawsForm lawsForm) throws Exception {
Laws laws = null ;
try {
laws = new Laws();
BeanUtils.copyProperties(laws, lawsForm);
LawsBclass lawsBclass = new LawsBclass() ;
lawsBclass.setBclassId(lawsForm.getBclassId());
laws.setLawsBclass(lawsBclass);
laws.setLawsContentBinary(Hibernate.createBlob(new byte[1]));
laws.setLawsContentText(Hibernate.createClob(" "));
this.getHibernateTemplate().update(laws);
this.getHibernateTemplate().flush();
this.getHibernateTemplate().refresh(laws, LockMode.UPGRADE);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
// 向BLOB字段寫入內(nèi)容
SerializableBlob serializableBlob = (SerializableBlob) laws.getLawsContentBinary();
java.sql.Blob javablob = serializableBlob.getWrappedBlob() ;
oracle.sql.BLOB blob = (oracle.sql.BLOB) javablob ;
BufferedInputStream contentBin = new BufferedInputStream(
lawsForm.getContentBinaryFormFile().getInputStream());
BufferedOutputStream contentBout = new BufferedOutputStream(blob
.getBinaryOutputStream());
byte[] buffer = new byte[1024];
int length = 0;
while ((length = contentBin.read(buffer)) > 0) {
contentBout.write(buffer, 0, length);
}
contentBin.close();
contentBout.close();
// 向CLOB字段寫入內(nèi)容
SerializableClob serializableClob = (SerializableClob) laws.getLawsContentText();
java.sql.Clob javaclob = serializableClob.getWrappedClob();
oracle.sql.CLOB clob = (oracle.sql.CLOB)javaclob ;
String contentText = lawsForm.getContentText();
BufferedWriter bw = new BufferedWriter(clob.getCharacterOutputStream());
bw.write(contentText);
bw.close();
}
public void updateLaws(Laws laws) throws Exception {
try {
getHibernateTemplate().update(laws);
getHibernateTemplate().flush();
} catch (HibernateOptimisticLockingFailureException ex) {
throw new VersionException(ex);
} catch (DataAccessException ex) {
throw new DAOException(ex);
} catch (Exception ex) {
throw new DAOException(ex);
}
}
Laws laws = null ;
try {
laws = new Laws();
BeanUtils.copyProperties(laws, lawsForm);
LawsBclass lawsBclass = new LawsBclass() ;
lawsBclass.setBclassId(lawsForm.getBclassId());
laws.setLawsBclass(lawsBclass);
laws.setLawsContentBinary(Hibernate.createBlob(new byte[1]));
laws.setLawsContentText(Hibernate.createClob(" "));
laws.setIsaudit("0");
this.getHibernateTemplate().save(laws);
this.getHibernateTemplate().flush();
this.getHibernateTemplate().update(laws);
this.getHibernateTemplate().refresh(laws, LockMode.UPGRADE);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
// 向BLOB字段寫入內(nèi)容
SerializableBlob serializableBlob = (SerializableBlob) laws.getLawsContentBinary();
java.sql.Blob javablob = serializableBlob.getWrappedBlob() ;
oracle.sql.BLOB blob = (oracle.sql.BLOB) javablob ;
BufferedInputStream contentBin = new BufferedInputStream(
lawsForm.getContentBinaryFormFile().getInputStream());
BufferedOutputStream contentBout = new BufferedOutputStream(blob
.getBinaryOutputStream());
byte[] buffer = new byte[1024];
int length = 0;
while ((length = contentBin.read(buffer)) > 0) {
contentBout.write(buffer, 0, length);
}
contentBin.close();
contentBout.close();
// 向CLOB字段寫入內(nèi)容
SerializableClob serializableClob = (SerializableClob) laws.getLawsContentText();
java.sql.Clob javaclob = serializableClob.getWrappedClob();
oracle.sql.CLOB clob = (oracle.sql.CLOB)javaclob ;
String contentText = lawsForm.getContentText();
BufferedWriter bw = new BufferedWriter(clob.getCharacterOutputStream());
bw.write(contentText);
bw.close();
}
@SuppressWarnings("deprecation")
public void updateLaws(LawsForm lawsForm) throws Exception {
Laws laws = null ;
try {
laws = new Laws();
BeanUtils.copyProperties(laws, lawsForm);
LawsBclass lawsBclass = new LawsBclass() ;
lawsBclass.setBclassId(lawsForm.getBclassId());
laws.setLawsBclass(lawsBclass);
laws.setLawsContentBinary(Hibernate.createBlob(new byte[1]));
laws.setLawsContentText(Hibernate.createClob(" "));
this.getHibernateTemplate().update(laws);
this.getHibernateTemplate().flush();
this.getHibernateTemplate().refresh(laws, LockMode.UPGRADE);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
// 向BLOB字段寫入內(nèi)容
SerializableBlob serializableBlob = (SerializableBlob) laws.getLawsContentBinary();
java.sql.Blob javablob = serializableBlob.getWrappedBlob() ;
oracle.sql.BLOB blob = (oracle.sql.BLOB) javablob ;
BufferedInputStream contentBin = new BufferedInputStream(
lawsForm.getContentBinaryFormFile().getInputStream());
BufferedOutputStream contentBout = new BufferedOutputStream(blob
.getBinaryOutputStream());
byte[] buffer = new byte[1024];
int length = 0;
while ((length = contentBin.read(buffer)) > 0) {
contentBout.write(buffer, 0, length);
}
contentBin.close();
contentBout.close();
// 向CLOB字段寫入內(nèi)容
SerializableClob serializableClob = (SerializableClob) laws.getLawsContentText();
java.sql.Clob javaclob = serializableClob.getWrappedClob();
oracle.sql.CLOB clob = (oracle.sql.CLOB)javaclob ;
String contentText = lawsForm.getContentText();
BufferedWriter bw = new BufferedWriter(clob.getCharacterOutputStream());
bw.write(contentText);
bw.close();
}
public void updateLaws(Laws laws) throws Exception {
try {
getHibernateTemplate().update(laws);
getHibernateTemplate().flush();
} catch (HibernateOptimisticLockingFailureException ex) {
throw new VersionException(ex);
} catch (DataAccessException ex) {
throw new DAOException(ex);
} catch (Exception ex) {
throw new DAOException(ex);
}
}
posted on 2008-11-18 16:02 lanjh 閱讀(2771) 評(píng)論(0) 編輯 收藏 所屬分類: Java Web