免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1242 | 回复: 0
打印 上一主题 下一主题

深入浅出Hibernate之向Mysql插入BLOB,CLOB数据 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-08-27 11:45 |只看该作者 |倒序浏览
mysql version 5.0

database name:sample

数据表DDL:

create table `sample`.`tuser_blob`(
        `id` int not null auto_increment,
       `name` varchar(50),
       `age` int,
       `versio` int,
       `user_type` int,
       `image` blob,
       `resume` mediumblob,
        primary key (`id`)
    );
TuserBlob.hbm.xml文件:

   
        
            
            
        
        
            
        
        
            
        
        
            
        
        
            
        
        
            
        
        
            
        
   

对应的java文件:
package org.hibernatetest.bean;

/**
* TuserBlob generated by MyEclipse - Hibernate Tools
*/
import java.sql.Blob;
import java.sql.Clob;
public class TuserBlob  implements java.io.Serializable {
    // Fields   
     private Integer id;
     private String name;
     private Integer age;
     private Integer versio;
     private Integer userType;
     private Blob image;
     private Clob resume;
    // Constructors
    /** default constructor */
    public TuserBlob() {
    }
   
    /** full constructor */
    public TuserBlob(String name, Integer age, Integer versio, Integer userType, Blob image, Clob resume) {
        this.name = name;
        this.age = age;
        this.versio = versio;
        this.userType = userType;
        this.image = image;
        this.resume = resume;
    }
   
    // Property accessors
    public Integer getId() {
        return this.id;
    }
   
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return this.name;
    }
   
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return this.age;
    }
   
    public void setAge(Integer age) {
        this.age = age;
    }
    public Integer getVersio() {
        return this.versio;
    }
   
    public void setVersio(Integer versio) {
        this.versio = versio;
    }
    public Integer getUserType() {
        return this.userType;
    }
   
    public void setUserType(Integer userType) {
        this.userType = userType;
    }
    public Blob getImage() {
        return this.image;
    }
   
    public void setImage(Blob image) {
        this.image = image;
    }
    public Clob getResume() {
        return this.resume;
    }
   
    public void setResume(Clob resume) {
        this.resume = resume;
    }
}

如何使用:

package org.hibernate.dao;
import org.hibernatetest.base.HibernateSessionFactory;
import org.hibernatetest.bean.TuserBlob;
import java.sql.*;
import net.sf.hibernate.*;
import java.io.*;
public class TuserBlobDAO {
public void addTUserBlob(TuserBlob tuserBlob) {
  try {
   Session session = HibernateSessionFactory.getSession();
   Transaction tx = session.beginTransaction();
   session.save(tuserBlob);
   tx.commit();
  } catch (Exception ex) {
   System.out.println(ex.toString());
  }
}
public void getTUserBlob(int id,String path) {
  try {
   Session session = HibernateSessionFactory.getSession();
   TuserBlob user = (TuserBlob)session.load(TuserBlob.class, new Integer(id));
   
   Clob resume = user.getResume();
   
   //通过Clob.geSubString()方法获取Clob字段
   System.out.println(
   
     "User resume-->"+
     resume.getSubString(1, (int)resume.length())
   );
   
   Blob image = user.getImage();
   
   //通过Blob.getBinaryStream()方法获得2进制流
   InputStream is = image.getBinaryStream();
   
   
   
   FileOutputStream fos = new FileOutputStream(path);
   
   byte[] buf = new byte[102400];
   
   int len;
   while((len = is.read(buf))!= -1){
   
    fos.write(buf,0,len);
   
   }
   
   fos.close();
   is.close();
   
   
   
  } catch (Exception ex) {
   System.out.println(ex.toString());
  }
}
}
servlet调用此文件:

package org.hibernatetest.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.dao.TuserBlobDAO;
import java.io.*;
import org.hibernatetest.bean.TuserBlob;
import net.sf.hibernate.*;
import java.sql.*;
public class TUserBlobServlet extends HttpServlet {
/**
  * Constructor of the object.
  */
public TUserBlobServlet() {
  super();
}
/**
  * Destruction of the servlet.
  */
public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  doPost(request,response);
}
/**
  * The doPost method of the servlet.
  *
  * This method is called when a form has its tag value method equals to post.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  TuserBlob TUserBlob = new TuserBlob();
  
  TUserBlob.setName("lisi001");
  
  TUserBlob.setAge(new Integer(35));
  
  InputStream imageis =this.getServletContext().getResourceAsStream("/images/mm1.jpg");
  
  
  Blob image = Hibernate.createBlob(imageis);
  
  TUserBlob.setImage(image);
  
  Clob resume = Hibernate.createClob("hi,This is a test!");
  
  TUserBlob.setResume(resume);
  
  //new TuserBlobDAO().addTUserBlob(TUserBlob);
  
  String path = "D:\\workspace\\hibernatetest\\WebRoot\\images\\outimage2.jpg";
  
  new TuserBlobDAO().getTUserBlob(2, path);
  
  System.out.println("It's OK!");
  
}
/**
  * Initialization of the servlet.
  *
  * @throws ServletException if an error occure
  */
public void init() throws ServletException {
  // Put your code here
}
}




本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/15642/showart_368622.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP