`

Hibernate实现Oracle Blob/Clob类型数据读写

阅读更多
环境:Eclipse3.1+Hibernate2+Oracle 10g
说明:本例在使用Eclipse3.1通过一个Java项目来实现对Oracle中Blob/Clob类型数据的读写操作.
1.在oracle数据库中新建一个表,取名:BIGDATA, sql语句如下:
  CREATE TABLE BIGDATA (id number NOT NULL,image blob,resume clob);//新建表
  ALTER TABLE BIGDATA ADD ( CONSTRAINT bigdata_pk PRIMARY KEY (id) USING INDEX );// 修改id为主键:

2.在Eclipse中新建一个java项目,取名:HibernateSample,项目中需要导入的jar文件可根据需要导入即可。
  新建一个源文件夹src,在src下新建一个包,取名bigDataAccess,在该包下新建一个类,取名DataModel.java,内容如下:
package bigDataAccess;

import java.sql.Blob;
import java.sql.Clob;

public class DataModel {
Long id;

Blob image;

Clob resume;

public Long getId() {
  return id;
}

public void setId(Long id) {
  this.id = id;
}

public Blob getImage() {
  return image;
}

public void setImage(Blob image) {
  this.image = image;
}

public Clob getResume() {
  return resume;
}

public void setResume(Clob resume) {
  this.resume = resume;
}
}

2.再在bigDataAccess包下新建一个类取名:HibernateUtil.java用于管理session,源代码如下:
package bigDataAccess;

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;

public class HibernateUtil {
private static SessionFactory sessionFactory;

public static final ThreadLocal session= new ThreadLocal();
static {
  try {
   // 实例化一个SessionFactory对象
   //System.out.println("通过静态模块创建一个SessionFactory");
   sessionFactory = new Configuration().configure()
     .buildSessionFactory();
  } catch (HibernateException ex) {
   throw new RuntimeException("Configuration problem:"
     + ex.getMessage(), ex);
  }
}
public static Session currentSession() throws HibernateException {
  Session s = (Session) session.get();
  // 当原session为空或已关闭时,打一个新的Session
  if (s == null || !s.isOpen()) {
   s = sessionFactory.openSession();
   session.set(s);
  }
  return s;
}

public static void closeSession() throws HibernateException {
  Session s = (Session) session.get(); 
  if (s != null) {
   s.close();
  } 
}
}

2.在包bigDataAccess下新建映射文件bigDataAccess.hbm.xml,内容如下:

    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >



 
  
 
 
 


3.新建Hibernate配置文件于src目录下,hibernate.cfg.xml内容如下

                                         "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">



  oracle.jdbc.driver.OracleDriver
  jdbc:oracle:thin:@localhost:1521:orcl
  work
  caecaodb
  true
  net.sf.hibernate.dialect.OracleDialect
  
  0
   


4.在包bigDataAccess下新建一个带有main()函数的类,取名HibernateTest.java,源代码如下:
package bigDataAccess;
//注意要导入正确有包

public class HibernateTest{

public HibernateTest() throws HibernateException, SQLException, IOException {
  doit();
}

public static void main(String[] args) throws HibernateException,
   SQLException, IOException {
  new HibernateTest();

}

public void doit() throws HibernateException, SQLException, IOException {

  // Blog,Clob类型数据读写---------write

  /*
   * Oracle Blob/Clob字段本身有一个游标(cursor),
   * JDBC通过游标对Blob/Clob字段进行操作,在Blob/Clob字段被创建之前,
   * 无法获得其游标句柄,因此必须首先创建一个空Blob/Clob字段,再从空这个空Blob/Clob字段获取游标,再写入要保存的数据
   */
  System.out.println("写入数据");
  Session session = HibernateUtil.currentSession();
  Transaction tx = session.beginTransaction();

  DataModel info = new DataModel();
  info.setImage(Hibernate.createBlob(new byte[1]));
  info.setResume(Hibernate.createClob(" "));// 这里的参数是一个空格
  session.save(info);
  session.flush();// 强制执行insert
  // 通过refresh方法,强制Hibernate执行select for update
  session.refresh(info, LockMode.UPGRADE);

  // 向Blog写入实际内容
  oracle.sql.BLOB blob = (oracle.sql.BLOB) info.getImage();
  FileInputStream imgis = new FileInputStream("d:\\image.jpg");//在d:\根目录下放一个image.jpg的文件
  OutputStream out = blob.getBinaryOutputStream();
  byte[] buf = new byte[10240];
  int len;
  while ((len = imgis.read(buf)) > 0) {
   out.write(buf, 0, len);
  }
  imgis.close();
  out.close();

  // 向Clob写入实际内容
  oracle.sql.CLOB clob = (oracle.sql.CLOB) info.getResume();
  Writer writer = clob.getCharacterOutputStream();
  writer.write("this is my resume");
  writer.close();

  session.save(info);
  tx.commit();
  //读取刚才写入数据库的数据
  System.out.println("读取数据");
  DataModel info1 = (DataModel) session
    .load(DataModel.class, new Long(1));
  Clob resume = info1.getResume();
  System.out.println("resume="
    + resume.getSubString(1, (int) resume.length()));

  Blob img = info1.getImage();
  InputStream is = img.getBinaryStream();
  FileOutputStream fos = new FileOutputStream("d:\\outimage.jpg");//读取到d:\outimage.jpg
  byte[] buf1 = new byte[10240];
  int len1;
  while ((len1 = is.read(buf1)) != -1) {
   fos.write(buf1, 0, len1);
  }
  fos.close();
  is.close();
  session.close();
}

右击HibernateTest.java,将其运行为java应用程序,查询bigdata表则会新增了一条记录,d:\有一个名为:outimage.jpg的文件,说明成功
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics