/*==============================================================*/
/* Table: users */
/*==============================================================*/
create table dbo.users (
userid int identity,
username varchar(100) not null,
password varchar(100) not null,
address varchar(200) null,
constraint PK_USER primary key clustered (userid)
)
go
User.java,这是根据表结构,自动生成的
package com.test;
import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
/** @author Hibernate CodeGenerator */
public class User implements Serializable {
/** identifier field */
private Integer userid;
/** persistent field */
private String username;
/** persistent field */
private String password;
/** nullable persistent field */
private String address;
/** full constructor */
public User(java.lang.Integer userid, java.lang.String username,
java.lang.String password, java.lang.String address) {
this.userid = userid;
this.username = username;
this.password = password;
this.address = address;
}
/** default constructor */
public User() {
}
/** minimal constructor */
public User(java.lang.Integer userid, java.lang.String username, java.lang.String password) {
this.userid = userid;
this.username = username;
this.password = password;
}
public java.lang.Integer getUserid() {
return this.userid;
}
public void setUserid(java.lang.Integer userid) {
this.userid = userid;
}
public java.lang.String getUsername() {
return this.username;
}
public void setUsername(java.lang.String username) {
this.username = username;
}
public java.lang.String getPassword() {
return this.password;
}
public void setPassword(java.lang.String password) {
this.password = password;
}
public java.lang.String getAddress() {
return this.address;
}
public void setAddress(java.lang.String address) {
this.address = address;
}
public String toString() {
return new ToStringBuilder(this)
.append("userid", getUserid())
.toString();
}
public boolean equals(Object other) {
if ( !(other instanceof User) ) return false;
User castOther = (User) other;
return new EqualsBuilder()
.append(this.getUserid(), castOther.getUserid())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getUserid())
.toHashCode();
}
}
trun.java,这是用来测试数据库操作
package com.test;
/**
*Title: 测试JDO
*Description:
*Copyright: Copyright (c) 2003
*Company:
* @author not attributable
* @version 1.0
*/
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
import net.sf.hibernate.Query;
import java.util.*;
public class trun {
private static SessionFactory sf;
private static Configuration conf;
private Session s=null;
public static void main(String args[]) {
trun tt = new trun();
//tt.insertdb("hsh","dfaf","23地方");
//tt.deletedb(Integer.decode("2"));
//System.out.println(tt.getdb(Integer.decode("4")).getUsername());
//tt.updatedb(Integer.decode("3"),"周华");
/*
List mylist=tt.listdb(20,10);
if(mylist.size()>0)
{
User myuser=null;
for(int i=0;i{
myuser=(User)mylist.get(i);
System.out.println(myuser.getUserid()+":"+myuser.getUsername());
}
}
else
{
System.out.println("no find");
}
*/
}
public trun() {
try {
conf = new Configuration()
.addClass(com.test.User.class);
//conf.setProperty("hibernate.xml.output_stylesheet","e://hibernate-default.xslt");
System.out.println("prop: "+conf.getProperty("hibernate.xml.output_stylesheet"));
sf = conf.buildSessionFactory();
}
catch (Exception e) {
e.printStackTrace();
}
}
public List listdb(int start,int pagenum) {
List resultlist = null;
try {
s=sf.openSession();
Transaction t=s.beginTransaction();
Query q=s.createQuery("from User as user order by user.userid");
q.setFirstResult(start);
q.setMaxResults(pagenum);
resultlist=q.list();
t.commit();
}
catch (Exception e) {
e.printStackTrace();
}
return resultlist;
}
public void deletedb(Integer uid) {
try {
s=sf.openSession();
Transaction t=s.beginTransaction();
User my=(User)s.load(User.class,uid);
s.delete(my);
t.commit();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try{s.close();}catch(Exception e){e.printStackTrace();}
}
}
public void updatedb(Integer uid, String username) {
try {
s = sf.openSession();
Transaction t=s.beginTransaction();
User my=(User)s.load(User.class,uid);
my.setUsername(username);
t.commit();
}
catch (Exception e) {
e.printStackTrace();
}
finally{
try{s.close();}catch(Exception e){e.printStackTrace();}
}
}
public User result=null;
public User getdb(Integer uid) {
try {
s = sf.openSession();
Transaction t=s.beginTransaction();
Query q=s.createQuery("from User as user where user.userid=:uid ");
q.setParameter("uid",uid);
result=(User)q.list().get(0);
t.commit();
}
catch (Exception e) {
e.printStackTrace();
}
finally{
try{s.close();}catch(Exception e){}
}
return result;
}
public void insertdb(String uname,String pass,String address) {
try {
s = sf.openSession();
Transaction t=s.beginTransaction();
User u=new User();
u.setUserid(Integer.decode("1"));
u.setPassword(pass);
u.setUsername(uname);
u.setAddress(address);
s.save(u);
t.commit();
}
catch (Exception e) {
e.printStackTrace();
}
finally{
try{s.close();}catch(Exception e){e.printStackTrace();}
}
}
}