现在的位置: 首页 > 综合 > 正文

hibernate插入图片

2013年04月20日 ⁄ 综合 ⁄ 共 483字 ⁄ 字号 评论关闭

 /**
  * 只是向数据库中插入一张图片
  */
 public static void insertPhoto() {
  User1 u = new User1();
  try {
   session = HibernateSessionFactory.getSession();
   Transaction tran = session.beginTransaction();
   FileInputStream in = new FileInputStream("a.jpg");
   byte[] p = new byte[in.available()];
   in.read(p);
   in.close();
   u.setPhoto1(p);
   u.setName("babe");
   u.setPwd("babe");
   session.save(u);
   tran.commit();
  } catch (Exception e) {
   e.printStackTrace();

  } finally {
   HibernateSessionFactory.closeSession();
  }

 } 

抱歉!评论已关闭.