`
heyaron
  • 浏览: 49634 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Oracle SSH Clob的处理

阅读更多
   前一段,我们的论坛用到了点问题。一次发帖不能超过2000个中文,然后我们用Clob来处理,在网上找了好多例子,最终可是弄好了。
   首先在Form中写两个字段,一个是java.sql.clob类型的,一个是String类型的,前者用来取Clob,后者相当于中介的作用。

*.hbm.xml的配置文件如下
        <property name="content" type="java.sql.Clob" update="true" insert="true">
            <column name="CONTENT" />
        </property>

然后在DAO层中写入类似的代码


		Session session = getHibernateTemplate().getSessionFactory()
				.openSession();
		Transaction tran = session.beginTransaction();
		TmInfo tmInfo = new TmInfo();
		tmInfo.setContent(Hibernate.createClob(" "));//这个一定要是空格
		tmInfo.setSubject(administratorForm.getSubject());
		tmInfo.setPblishTime(new Date());
		session.save(tmInfo);
		session.flush();
		session.refresh(tmInfo, LockMode.UPGRADE);

		// 从页面上拿数据到实体
		tmInfo.setContentString(administratorForm.getContent());

		SerializableClob sc = (SerializableClob) tmInfo.getContent();
		Clob wrapclob = sc.getWrappedClob();
		CLOB clob = (CLOB) wrapclob;

		Writer out;
		try {
			Writer characterOutputStream = clob.getCharacterOutputStream();//强制转换,否则会报空指针
			out = characterOutputStream;
			out.write(tmInfo.getContentString());
			out.close();
			tran.commit();
			session.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println(e.toString());
		}


就这样就能存好多字了。

然后取的时候,就

	Clob clob = tmInfo.getContent();// 取得Clob的值
		if (clob != null) {
			String clobString = "";
			try {
				clobString = clob.getSubString(1, (int) clob.length());// 将Clob类型的值转换成String类型的值
				tmInfo.setContentString(clobString);// 通过setter方法,设置String值,然后就可以通过instance.getInfoContentToString()来取值了
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics