oracle 谭岚_Hibernate实现Oracle BLOB的数据读写(1)

开发中文件上传到服务器,一般将文件保存在Web服务器的某个目录下,除非有特殊要求将文件存到数据库中保存。

本文主要基于学习的目的而作。

测试环境:Hibernate3.6.7,Oracle 10g Express,JDK7,Win7

1,数据库脚本

create table TUser (

ID char(32) not null,

name varchar(10char) not null,

photo blob, --头像

constraint PK_TUser primary key (ID)

);

2,Hibernate配置文件,本文基于传统的hibernate.cfg.xml

/p>

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

oracle.jdbc.driver.OracleDriver

jdbc:oracle:thin:@localhost:1521:XE

tanlan

tanlan

org.hibernate.dialect.Oracle10gDialect

true

3,编写实体类User.java

package com.tanlan.hibernate.entity;

public class User {

private String id;

private String name;

private byte[] photo;

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public byte[] getPhoto() {

return photo;

}

public void setPhoto(byte[] photo) {

this.photo = photo;

}

}

4,编写对应的映射文件,User.hbm.xml

/p>

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

5,测试代码

package com.tanlan.hibernate.test;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import org.hibernate.Session;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import com.tanlan.hibernate.entity.User;

public class TestUser {

public static void main(String[] args) {

addUser();

//getUserById();

//update();

}

private static void addUser() {

User user = new User();

user.setName("谭岚");

File photo = new File("I:\\1.jpg");

try {

FileInputStream is = new FileInputStream(photo);

ByteArrayOutputStream os = new ByteArrayOutputStream();

byte[] temp = new byte[512];

int i = 0;

while ((i = is.read(temp, 0, temp.length)) != -1) {

os.write(temp, 0, temp.length);

}

os.close();

is.close();

user.setPhoto(os.toByteArray());

} catch (Exception e) {

e.printStackTrace();

}

Session session = openSession();

Transaction transaction = session.beginTransaction();

session.save(user);

transaction.commit();

}

private static void getUserById() {

Session session = openSession();

User user = (User) session.get(User.class,

"402881e432993eae0132993eb7d20000");

byte[] photo = user.getPhoto();

try {

FileOutputStream os = new FileOutputStream("E:\\111.jpg");

os.write(photo);

os.close();

} catch (Exception e) {

e.printStackTrace();

}

}

private static void update() {

User user = new User();

user.setId("402881e432993eae0132993eb7d20000");

user.setName("新名");

File photo = new File("I:\\2.jpg");

try {

FileInputStream is = new FileInputStream(photo);

ByteArrayOutputStream os = new ByteArrayOutputStream();

byte[] temp = new byte[512];

int i = 0;

while ((i = is.read(temp, 0, temp.length)) != -1) {

os.write(temp, 0, temp.length);

}

os.close();

is.close();

user.setPhoto(os.toByteArray());

} catch (Exception e) {

e.printStackTrace();

}

Session session = openSession();

Transaction transaction = session.beginTransaction();

session.update(user);

transaction.commit();

}

private static Session openSession() {

Configuration cfg = new Configuration().configure();

return cfg.buildSessionFactory().openSession();

}

}

2

2

分享到:

2011-09-24 10:42

浏览 1507

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值