Java: Too simple hibernate sample with ms SQL Server

Source: Internet
Author: User
Tags mssqlserver jboss jconsole

/*
Test. Java:
Compile and run the command line: (replace the red font with your own path)
C:/j2sdk/bin/javac-classpath "E:/hibernate-2.1.2/hibernate-2.1/hibernate2.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/ant-1.5.3.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/ant-optional-1.5.3.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/c3p0-0.8.3.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/cglib-2.0-rc2.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/commons-collections-2.1.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/commons-dbcp-1.1.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/commons-lang-1.0.1.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/commons-logging-1.0.3.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/commons-pool-1.1.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/concurrent-1.3.2.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/connector. jar; E:/hibernate-2.1.2/hibernate-2.1/lib/dom4j-1.4.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/ehcache-0.6.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/JAAS. jar; E:/hibernate-2.1.2/hibernate-2.1/lib/jboss-cache.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/jboss-common.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/jboss-jmx.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/jboss-system.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/jcs-1.0-dev.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/jdbc2_0-stdext.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/jgroups-2.2.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/JTA. jar; E:/hibernate-2.1.2/hibernate-2.1/lib/junit-3.8.1.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/log4j-1.2.8.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/odmg-3.0.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/oscache-2.0.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/proxool-0.8.3.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/swarmcache-1.0rc2.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/xalan-2.4.0.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/xerces-2.4.0.jar; E: /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. jar; C:/j2sdk/demo/jfc/java2d/java2demo. jar; C:/j2sdk/demo/plugin/jfc/codepointim. jar; C:/j2sdk/demo/plugin/jfc/java2d/java2demo. jar; C:/j2sdk/JRE/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-2.1.2/hibernate-2.1/hibernate2.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/ant-1.5.3.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/ant-optional-1.5.3.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/c3p0-0.8.3.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/cglib-2.0-rc2.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/commons-collections-2.1.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/commons-dbcp-1.1.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/commons-lang-1.0.1.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/commons-logging-1.0.3.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/commons-pool-1.1.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/concurrent-1.3.2.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/connector. jar; E:/hibernate-2.1.2/hibernate-2.1/lib/dom4j-1.4.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/ehcache-0.6.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/JAAS. jar; E:/hibernate-2.1.2/hibernate-2.1/lib/jboss-cache.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/jboss-common.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/jboss-jmx.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/jboss-system.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/jcs-1.0-dev.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/jdbc2_0-stdext.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/jgroups-2.2.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/JTA. jar; E:/hibernate-2.1.2/hibernate-2.1/lib/junit-3.8.1.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/log4j-1.2.8.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/odmg-3.0.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/oscache-2.0.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/proxool-0.8.3.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/swarmcache-1.0rc2.jar; E: /hibernate-2.1.2/hibernate-2.1/lib/xalan-2.4.0.jar; E:/hibernate-2.1.2/hibernate-2.1/lib/xerces-2.4.0.jar; E: /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. jar; C:/j2sdk/demo/jfc/java2d/java2demo. jar; C:/j2sdk/demo/plugin/jfc/codepointim. jar; C:/j2sdk/demo/plugin/jfc/java2d/java2demo. jar; C:/j2sdk/JRE/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); // Test
}
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)

);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.