Chinaunix首页 | 论坛 | 博客
  • 博客访问: 327245
  • 博文数量: 96
  • 博客积分: 2041
  • 博客等级: 大尉
  • 技术积分: 1080
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-20 14:08
文章分类

全部博文(96)

文章存档

2015年(2)

2013年(1)

2012年(93)

分类: 系统运维

2012-02-05 00:54:43

 
People.hbm.xml

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

 
  
  
  

  
  
  
  
  
  
  
  
  
 
 
 
 
 

 
hibernate.cfg.xml

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

   
     jdbc:mysql://localhost:3306/hibernate
    root
    yanqunqq
    com.mysql.jdbc.Driver
   
    org.hibernate.dialect.MySQL5Dialect
   
 true   
   
   
   
  
   
 
HibernateTest.java
package com.shengsiyuan.hibernate;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Timestamp;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Transaction;
public class HibernateTest
{
 private static SessionFactory sessionFactory;
 static
 {
  try
  {
   sessionFactory = new Configuration().configure()
     .buildSessionFactory();
  }
  catch (Exception ex)
  {
   ex.printStackTrace();
  }
 }
 public static void main(String[] args) throws Exception
 {
  People people = new People();
  people.setUsername("zhangsan");
  people.setPassword("123456");
  people.setGender('F');
  people.setBirthday(new java.sql.Date(new java.util.Date().getTime()));
  people.setGraduation(true);
  people.setTelphone(23342);
  people.setMarryTime(new Timestamp(new java.util.Date().getTime()));
  InputStream is = new FileInputStream("D:/a.java");
  int length = is.available();
  byte[] buffer = new byte[length];
  is.read(buffer);
  people.setFile(buffer);
  Session session = sessionFactory.openSession();
  Transaction tx = null;
  try
  {
   tx = session.beginTransaction();
   session.save(people);
   tx.commit();
  }
  catch (Exception ex)
  {
   if (null != tx)
   {
    tx.rollback();
   }
   ex.printStackTrace() ;
  }
  finally
  {
   session.close();
  }
 }
}
People.java
package com.shengsiyuan.hibernate;
import java.sql.Date;
import java.sql.Timestamp;
public class People
{
 private Long id ; //对应数据库类型是bigint
 private String username ;
 private String password ;
 private int telphone ;
 private char gender ; //'M' 'F'男女
 private boolean graduation ;
 private Date birthday ;
 private Timestamp marryTime ;
 private byte[] file  ;
 public Long getId()
 {
  return id;
 }
 public void setId(Long id)
 {
  this.id = id;
 }
 public String getUsername()
 {
  return username;
 }
 public void setUsername(String username)
 {
  this.username = username;
 }
 public String getPassword()
 {
  return password;
 }
 public void setPassword(String password)
 {
  this.password = password;
 }
 public int getTelphone()
 {
  return telphone;
 }
 public void setTelphone(int telphone)
 {
  this.telphone = telphone;
 }
 public char getGender()
 {
  return gender;
 }
 public void setGender(char gender)
 {
  this.gender = gender;
 }
 public boolean isGraduation()
 {
  return graduation;
 }
 public void setGraduation(boolean graduation)
 {
  this.graduation = graduation;
 }
 public Date getBirthday()
 {
  return birthday;
 }
 public void setBirthday(Date birthday)
 {
  this.birthday = birthday;
 }
 public Timestamp getMarryTime()
 {
  return marryTime;
 }
 public void setMarryTime(Timestamp marryTime)
 {
  this.marryTime = marryTime;
 }
 public byte[] getFile()
 {
  return file;
 }
 public void setFile(byte[] file)
 {
  this.file = file;
 }
 
 
}
阅读(2614) | 评论(0) | 转发(0) |
0

上一篇:Struts2与OGNL

下一篇:Packet for query is too large

给主人留下些什么吧!~~