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;
}
}
阅读(2652) | 评论(0) | 转发(0) |