Java: Too Simple Hibernate Sample with Ms SQL Server

來源:互聯網
上載者:User

/*
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)

);

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.