Hibernate operation CLOB Type data

Source: Internet
Author: User

In Pojo, a large object in a string can be declared as a java.lang.String or Java.sql.Clob type.

When the program loads the CLOB type data from the database, only a logical pointer to the CLOB type of data is loaded. We need to get the data input stream of the CLOB type by using the Clob.getcaracterstream () method before we can get the large object data.

Look at the specific code below

Package Dao;import Java.io.bufferedreader;import Java.io.ioexception;import java.io.reader;import Java.math.bigdecimal;import Java.sql.clob;import Java.sql.sqlexception;import Org.hibernate.LobHelper;import Org.hibernate.query;import Org.hibernate.session;import org.hibernate.transaction;import entity. Clobtable;import Factory.hibernatesessionfactory;public class Clobdao {private session session = Null;private Transaction tran = Null;public Clobdao () {session = Hibernatesessionfactory.getsession ();} public void Saveclob (BigDecimal id,string content) {clobtable ct = new clobtable (); Ct.setid (ID); Lobhelper lh = Session.getlobhelper (); ct.setcontent (Lh.createclob content); tran = Session.begintransaction (); try{ Session.save (CT); Tran.commit (); System.out.println ("Insert succeeded! ");} catch (Exception e) {tran.rollback (); System.out.println ("Insert Failed");}} public void Getclob (BigDecimal id) {String hql = ' from clobtable where id =? '; Query query = session.createquery (HQL); Query.setbigdecimal (0, id); clobtable ct = (ClobTable) Query.uniqueresult (); Clob Clob = ct.getcontent (); try {Reader reader = Clob.getcharacterstream (); BufferedReader br = new BufferedReader (reader); String content = Br.readline (); SYSTEM.OUT.PRINTLN (content);} catch (SQLException e) {e.printstacktrace (); System.out.println ("Read failed! ");} catch (IOException e) {System.out.println ("Read failed! ");}}}


Hibernate operation CLOB Type data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.