Java: Too Simple Hibernate Sample with Ms SQL Server
/*
Test.java:
编译及运行 命令行:(红色字体部分替换成你自己相应的路径)
C:/J2SDK/bin/javac -classpath "E:/Hibernate/hibernate-2.1.2/hibernate-2.1/hibernate2.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/ant-1.5.3.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/ant-optional-1.5.3.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/c3p0-0.8.3.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/cglib-2.0-rc2.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/commons-collections-2.1.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/commons-dbcp-1.1.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/commons-lang-1.0.1.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/commons-logging-1.0.3.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/commons-pool-1.1.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/concurrent-1.3.2.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/connector.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/dom4j-1.4.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/ehcache-0.6.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/jaas.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/jboss-cache.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/jboss-common.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/jboss-jmx.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/jboss-system.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/jcs-1.0-dev.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/jdbc2_0-stdext.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/jgroups-2.2.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/jta.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/junit-3.8.1.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/log4j-1.2.8.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/odmg-3.0.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/oscache-2.0.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/proxool-0.8.3.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/swarmcache-1.0rc2.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/xalan-2.4.0.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/xerces-2.4.0.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/xml-apis.jar;F:/mssql/jdbc/lib/msbase.jar;F:/mssql/jdbc/lib/mssqlserver.jar;F:/mssql/jdbc/lib/msutil.jar;C:/J2SDK/demo/jfc/CodePointIM/CodePointIM.jar;C:/J2SDK/demo/jfc/Java2D/Java2Demo.jar;C:/J2SDK/demo/plugin/jfc/CodePointIM/CodePointIM.jar;C:/J2SDK/demo/plugin/jfc/Java2D/Java2Demo.jar;C:/J2SDK/jre/javaws/javaws.jar;C:/J2SDK/jre/lib/charsets.jar;C:/J2SDK/jre/lib/deploy.jar;C:/J2SDK/jre/lib/ext/dnsns.jar;C:/J2SDK/jre/lib/ext/ldapsec.jar;C:/J2SDK/jre/lib/ext/localedata.jar;C:/J2SDK/jre/lib/ext/sunjce_provider.jar;C:/J2SDK/jre/lib/ext/sunpkcs11.jar;C:/J2SDK/jre/lib/im/indicim.jar;C:/J2SDK/jre/lib/im/thaiim.jar;C:/J2SDK/jre/lib/javaws.jar;C:/J2SDK/jre/lib/jce.jar;C:/J2SDK/jre/lib/jsse.jar;C:/J2SDK/jre/lib/plugin.jar;C:/J2SDK/jre/lib/rt.jar;C:/J2SDK/lib/dt.jar;C:/J2SDK/lib/htmlconverter.jar;C:/J2SDK/lib/jconsole.jar;C:/J2SDK/lib/tools.jar" Test.java
C:/J2SDK/bin/java -classpath ".;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/hibernate2.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/ant-1.5.3.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/ant-optional-1.5.3.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/c3p0-0.8.3.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/cglib-2.0-rc2.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/commons-collections-2.1.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/commons-dbcp-1.1.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/commons-lang-1.0.1.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/commons-logging-1.0.3.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/commons-pool-1.1.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/concurrent-1.3.2.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/connector.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/dom4j-1.4.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/ehcache-0.6.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/jaas.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/jboss-cache.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/jboss-common.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/jboss-jmx.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/jboss-system.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/jcs-1.0-dev.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/jdbc2_0-stdext.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/jgroups-2.2.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/jta.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/junit-3.8.1.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/log4j-1.2.8.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/odmg-3.0.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/oscache-2.0.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/proxool-0.8.3.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/swarmcache-1.0rc2.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/xalan-2.4.0.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/xerces-2.4.0.jar;E:/Hibernate/hibernate-2.1.2/hibernate-2.1/lib/xml-apis.jar;F:/mssql/jdbc/lib/msbase.jar;F:/mssql/jdbc/lib/mssqlserver.jar;F:/mssql/jdbc/lib/msutil.jar;C:/J2SDK/demo/jfc/CodePointIM/CodePointIM.jar;C:/J2SDK/demo/jfc/Java2D/Java2Demo.jar;C:/J2SDK/demo/plugin/jfc/CodePointIM/CodePointIM.jar;C:/J2SDK/demo/plugin/jfc/Java2D/Java2Demo.jar;C:/J2SDK/jre/javaws/javaws.jar;C:/J2SDK/jre/lib/charsets.jar;C:/J2SDK/jre/lib/deploy.jar;C:/J2SDK/jre/lib/ext/dnsns.jar;C:/J2SDK/jre/lib/ext/ldapsec.jar;C:/J2SDK/jre/lib/ext/localedata.jar;C:/J2SDK/jre/lib/ext/sunjce_provider.jar;C:/J2SDK/jre/lib/ext/sunpkcs11.jar;C:/J2SDK/jre/lib/im/indicim.jar;C:/J2SDK/jre/lib/im/thaiim.jar;C:/J2SDK/jre/lib/javaws.jar;C:/J2SDK/jre/lib/jce.jar;C:/J2SDK/jre/lib/jsse.jar;C:/J2SDK/jre/lib/plugin.jar;C:/J2SDK/jre/lib/rt.jar;C:/J2SDK/lib/dt.jar;C:/J2SDK/lib/htmlconverter.jar;C:/J2SDK/lib/jconsole.jar;C:/J2SDK/lib/tools.jar" Test
*/
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class Test
{
public static void main(String[] args) throws Exception
{
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
for (int i = 0; i < 10; i++)
{
Customer customer = new Customer();
customer.setUsername("customer" + i);
customer.setPassword("password");
session.save(customer); //测试
}
tx.commit();
Query query = session.createQuery("select xx from Customer xx");
java.util.List l = query.list();
java.util.Iterator i = l.iterator();
//java.util.Iterator i = query.iterate();
while (i.hasNext())
{
Customer c = (Customer) i.next();
System.out.print(c.getUsername());
System.out.print("/t" + c.getPassword());
System.out.println();
}
session.close();
}
}
class Customer
{
private int _id;
private String _username;
private String _password;
public int getId()
{
return _id;
}
public String getPassword()
{
return _password;
}
public String getUsername()
{
return _username;
}
public void setId(int id)
{
this._id = id;
}
public void setPassword(String password)
{
this._password = password;
}
public void setUsername(String username)
{
this._username = username;
}
}
SQL:
CREATE TABLE CUSTOMER
(
CID INTEGER NOT NULL PRIMARY KEY,
USERNAME VARCHAR(12) NOT NULL,
PASSWORD VARCHAR(12)
);