hibernate的图片的存取

package cn.bdqn.test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.Iterator
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import cn.bdqn.entity.User;

public class Test1 {

	private Session session = null;
	private Transaction tran = null;
	private static final ThreadLocal s = new ThreadLocal();

	public Session getSession() {
		return session;
	}

	public void setSession(Session session) {
		this.session = session;
	}

	public Transaction getTran() {
		return tran;
	}

	public void setTran(Transaction tran) {
		this.tran = tran;
	}

	public static ThreadLocal getS() {
		return s;
	}

	public Test1() {
		Configuration config = new Configuration().configure();
		SessionFactory factory = config.buildSessionFactory();
		session = (Session) s.get();
		if (null == session) {
			session = factory.openSession();
			s.set(session);
		}
	}

	public void save() {
		try {
			tran = session.beginTransaction();
			User user = new User("王五", "123456");
			session.save(user);
			tran.commit();
			System.out.println("用户信息保存成功");
		} catch (HibernateException e) {
			if (null != tran) {
				tran.rollback();
			}
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	public static void query() {
		Configuration config = null;
		SessionFactory factory = null;
		Session session = null;
		User user1 = null;
		try {
			config = new Configuration().configure();
			factory = config.buildSessionFactory();
			session = factory.openSession();
			// user1=(User) session.load(User.class, new Long(2));

			user1 = (User) session.get(User.class, new Long(2));
			System.out.println(user1.getName() + "," + user1.getPassword());

			System.out.println("用户信息查询成功");
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
			if (factory != null) {
				factory.close();
			}
		}
	}

	public static void queryall() {
		Configuration config = null;
		SessionFactory factory = null;
		Session session = null;
		User user1 = null;
		try {
			config = new Configuration().configure();
			factory = config.buildSessionFactory();
			session = factory.openSession();
			String hql = "from User";
			Query query = session.createQuery(hql);
			List list = query.list();
			Iterator it = list.iterator();
			while (it.hasNext()) {
				user1 = (User) it.next();
				System.out.println(user1.getName() + "," + user1.getPassword());
			}
			System.out.println("用户信息查询成功");
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
			if (factory != null) {
				factory.close();
			}
		}
	}

	public static void update() {
		Configuration config = null;
		SessionFactory factory = null;
		Session session = null;
		Transaction tran = null;
		User user = null;
		try {
			config = new Configuration().configure();
			factory = config.buildSessionFactory();
			session = factory.openSession();
			tran = session.beginTransaction();
			// user=(User) session.get(User.class, new Long(2));
			// user.setPassword("111111");
			// session.update(user);

			String hql = "update User set password=? where userid=?";
			Query query = session.createQuery(hql);
			query.setString(0, "888888");
			query.setString(1, "3");
			query.executeUpdate();
			tran.commit();
			System.out.println("用户信息修改成功");
		} catch (HibernateException e) {
			if (null != tran) {
				tran.rollback();
			}
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
			if (factory != null) {
				factory.close();
			}
		}
	}

	public static void dele() {
		Configuration config = null;
		SessionFactory factory = null;
		Session session = null;
		Transaction tran = null;
		User user = null;
		try {
			config = new Configuration().configure();
			factory = config.buildSessionFactory();
			session = factory.openSession();
			tran = session.beginTransaction();
			// user=(User) session.get(User.class, new Long(2));
			// session.delete(user);

			String hql = "delete User where userid=?";
			Query query = session.createQuery(hql);
			query.setString(0, "3");
			query.executeUpdate();
			query.executeUpdate();
			tran.commit();
			System.out.println("用户信息删除成功");
		} catch (HibernateException e) {
			if (null != tran) {
				tran.rollback();
			}
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
			if (factory != null) {
				factory.close();
			}
		}
	}

	public void saveimage(User user, String imagepath) {
		InputStream is = this.getClass().getResourceAsStream(imagepath);
		Blob img;
		tran = session.beginTransaction();
		try {
			img = Hibernate.createBlob(is);
			user.setImg(img);
			session.save(user);
			tran.commit();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public void outimage(int id) {
		User user = (User) session.get(User.class, new Long(id));
		Blob img = user.getImg();
		InputStream is = null;
		OutputStream out = null;
		try {
			is = img.getBinaryStream();
			out = new FileOutputStream("D://test2.gif");
			int len=-1;
			while ((len=is.read()) != -1) {
				out.write(len);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (session != null) {
					session.close();
				}
				if (null != is) {
					is.close();
				}
				if (null != out) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	public static void main(String[] args) {
		// save();
		// query();
		// queryall();
		// update();
		// dele();
//		User user = new User("熊猫", "111111");
//		new Test1().saveimage(user, "test1.gif");
		new Test1().outimage(6);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值