MySQL table
CREATE TABLE ' t_user ' ( ' id ' int (one) not null auto_increment, ' name ' varchar "NOT NULL, ' pwd ' varchar") D Efault NULL, PRIMARY KEY (' id ')) engine=innodb auto_increment=10113 DEFAULT Charset=utf8;
Hibernate.cfg.xml
<?xml version= ' 1.0 ' encoding= ' UTF-8 '? ><! DOCTYPE hibernate-configuration public "-//hibernate/hibernate configuration DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-configuration-3.0.dtd "><!--Generated by MyEclipse hibernate Tools. -->
User.java
Package Com.domain;public class User {Integer id; String name; String pwd;public Integer getId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String getpwd () {return pwd;} public void SetPwd (String pwd) {this.pwd = pwd;}}
Test.java
Package Com.test;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;import com.domain.*;p ublic class Test {public static void main (string[] args) { Configuration cfg = new configuration (). Configure (); Sessionfactory factory = Cfg.buildsessionfactory (); Session session = Null;try{session = Factory.opensession (); Session.begintransaction (); User user = new user (), User.setname ("Big Wolf"), User.setpwd ("123345"), Session.save (user), Session.gettransaction (). Commit ();} catch (Exception e) {e.printstacktrace (); Session.gettransaction (). rollback (); finally{//Close sessionif (session! = NULL && session.isopen ()) Session.close ();}}
Hibernate Simple Demo