Declare, my post refer to http://www.hibernate.org.cn/88.html, I am doing exercises, because I can't download JSQL Driver, modified the database driver to JTDS development IDE: JBuilderx uses the database: MS SQL Server 2000 Database Drive: JTDS
Description:
1. Hibernate is clearly illustrated in the configuration file "Microsoft Driver (Not Recommended!)", So use JTDS Driver first.
2, JTDS can be http://sourceforge.net/projects/jtds specific download link http://prdownloads.SourceForge.net/jtds/jtds-0.9.jar?use_mirror=unc Download.
3, JDBC3.0 can only be used in JDK1.4 and above, JBuilderx default is JDK1.4
Ready to work:
1, download hibernate, the current version is 2.1.6 address as follows: http://prdownloads.SourceForge.Net/Hibernate/Hibernate-2.1.6.zip?Use_mirror=optusnet
2. Create a lib in JBuilder, named Hibernate, put all JAR under hibernate / lib in, and put hibernate / hibernate2.jar
3. Create a lib in JBuilder, named JTDS, put jtds-0.9.jar in
Start an example: 1. Create a Project, named Testhibernate
2, joining hibernate_full and jsql3 in Required Libraries in the properties
3. Select the XML file in the menu Project -> Project Properties -> Build -> Resource, select "Copy" - When compiling the project, copy the XML file in the SRC folder to the CLASSES file. In the corresponding directory in the folder
4. Create a directory in the TestHibernate project, such as SRC (SRC is the code location of the project standard)
5. Copy hibernate.properties and log4j.properties in the Hibernate source file to the SRC directory in the TestHibernate project
6. Modify the configuration of MS SQL Server 2000 driver in hibernate.properties
turn up
## Hypersonicsql
hibernate.dialect net.sf.hibernate.dialect.HSQLDialecthibernate.connection.driver_class org.hsqldb.jdbcDriverhibernate.connection.username sahibernate.connection.passwordhibernate.connection.url jdbc: hsqldb: hsql: //localhosthibernate.connection.url jdbc: hsqldb : TestHibernate.Connection.URL JDBC: HSQLDB :.
This, here is that the default is to use Hypersonicsql, we use MS SQL Server, so comment all over time
## Hypersonicsql
# Hibernate.dialect net.sf.hibernate.dialect.HSQLDialect # hibernate.connection.driver_class org.hsqldb.jdbcDriver # hibernate.connection.username sa # hibernate.connection.password # hibernate.connection.url jdbc: hsqldb: hsql: / /localhost#hibernate.Connection.URL JDBC: HSQLDB: TEST # hibernate.connection.URL JDBC: HSQLDB :. and find two paragraphs below
## MS SQL Server
# hibernate.dialect net.sf.hibernate.diaalect.sqlserverdiaalect # Hibernate.Connection.Username Sa # Hibernate.Connection.password sa
## jTDS # hibernate.connection.driver_class net.sourceforge.jtds.jdbc.Driver # hibernate.connection.url jdbc: jtds: sqlserver: //160.1.111.166/Sale; SelectMethod = cursor # hibernate.jdbc.use_scrollable_resultset false
This, such as the database server machine we use is 160.1.111.166, the database name is Sale, the user name connected to the database is Sale_user, the password is 82920810, then the two paragraphs have become
## MS SQL Server
Hibernate.dialev Net.sf.hibernate.dialect.sqlserverDialectHibernate.Connection.userName Sale_UserHibernate.Connection.Password 82920810
## jTDShibernate.connection.driver_class net.sourceforge.jtds.jdbc.Driverhibernate.connection.url jdbc: jtds: sqlserver: //160.1.111.166/Sale; SelectMethod = cursorhibernate.jdbc.use_scrollable_resultset false
7. Create a class TESTHIBERNATE.PERSON, this is a standard JavaBean, only 3 attributes and corresponding GET / SET methods
Package testhibernate;
Public Class Person {Private String ID; Private String Name; Private String Address
Public void setId (string value) {this.id = value;}
Public string getId () {return id;}
Public void setname (string value) {this.name = value;
Public string getname () {return name;}
Public void setaddress (String value) {this.address = value;
Public string getaddress () {return address;}}
8. Create an object-relational map of XML files in Person.hbm.xml, put it in the same directory as Person.java below XML Version = "1.0" Encoding = "GB2312"?>
9. Creating a client program for calling class Person Client1.java
Package testhibernate;
import net.sf.hibernate.Session; import net.sf.hibernate.Transaction; import net.sf.hibernate.SessionFactory; import net.sf.hibernate.cfg.Configuration; import net.sf.hibernate.tool.hbm2ddl.SchemaExport ;
/ *** This class is only used to create a table, and it is not inserted into any data inside the table, and can only be used once, otherwise it will delete * / public class client1 {private static sessionFactory sessionFactory;
Public static void main (string [] args) throws exception {configuration conf = new configuration (); addclass (person.class);
// The first time running is used to create a table / / in the database and output the SQL statement to the TXT file // will not use the segment code, otherwise, each time you will delete the original table, then create the table SchemaExport dbexport = new schemaexport (conf); dbexport.setoutputfile ("sql.txt"); dbexport.create (true, true);}}
Package testhibernate;
import net.sf.hibernate.Session; import net.sf.hibernate.Transaction; import net.sf.hibernate.SessionFactory; import net.sf.hibernate.cfg.Configuration; import net.sf.hibernate.tool.hbm2ddl.SchemaExport ;
public class Client2 {private static SessionFactory sessionFactory; public static void main (String [] args) throws Exception {Configuration conf = new Configuration () addClass (Person.class);. sessionFactory = conf.buildSessionFactory (); Session s = sessionFactory. OpenSession ();
Transaction t = s.begintransaction ();
Person Yuj = New Person (); YUJ.SetName ("john"); YUJ.SetAddress ("Shanghai");
Person x = new person (); x.setname ("zhaoyh"); X.Setaddress ("Shanghai");
// Persistence S.Save (YUJ); // At this point, YUJ can already be found in the database; // At this time, X can already be found in the database.
T.commit (); s.close ();}} If there is a problem, welcome to ask Liufh75@tom.com
Copyright Notice: 9CBS is this BLOG managed service provider. If this paper involves copyright issues, 9CBS does not assume relevant responsibilities, please contact the copyright owner directly with the article Author.
Posted on November 22, 2004 10:38 AM