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);
}
}
hibernate的图片的存取
最新推荐文章于 2018-04-18 15:14:59 发布