开发环境:
windowsXP sp3
jdk1.5
tomcat 6.0
myeclipse 6.5
oracle9i 9.2.0.1
使用的框架:struts1.2,spring2.0,hibernate3
问题:如何存取oracle表的clob字段
折腾了2天,问题总算暂时解决,分享经历与大家交流。
例子:
新闻表(id,标题,内容)
tbl_news
(id varchar2(32),
title varchar2(90),
content clob)
[size=small;]办法1:
[/size]
实体类以及hbm文件中content的类型都设置为java.sql.Clob
spring配置hibernate的xml中,添加以下内容
[code="xml"]
在struts的action中使用以下方式保存tbl_news
[code="java"]...
private NewsManager newsManager;
public void setNewsManager(NewsManager newsManager){
this.newsManager = newsManager;
}
public ActionForward save(ActionMapping mapping, ActionForm form,
HttpServletRequest request,HttpServletResponse response){
LazyValidatorForm lvForm = (LazyValidatorForm) form;
...
tblNews.setNews(Hibernate.createClob((String) lvForm.get("content")));
...
newsManager.save(tblNews)
}
使用以下方式读取
[code="java"]...
tblNews.getContent().subString(1,(int)tblNews.getContent().length)
...
这样的方式,增删改查都没有问题,不过在修改或添加数据时,当content列的内容稍大到大约2000长度时会提示错误,看到网络上的解决办法为
将content首先置空,使用session的save()及flush()方法提交至数据库,接着使用refresh()对象,将实际内容再添加至数据库,不过这种方式我始终没有实现,有待继续研究,如果得到解决过同样问题的朋友指点迷津,将不胜感激。
[size=small;]接着看我的第二种办法:
[/size]
[size=small;]
[/size]
使用spring提供的数据类型ClobStringType
具体办法,tbl_news实体中的content属性使用String类型,hbm文件的type设定为"org.springframework.orm.hibernate3.support.ClobStringType"
数据库连接使用oci驱动,第一种办法中数据库的连接驱动为thin。
在jsp页面以及action中使用String来操作content字段没有任何问题,也不存在数据过大时出现错误。