Developed IDE: JBuilderx Used Database: MS SQL Server 2000 Database Drive: JTDS 0.9
Description:
1. Hibernate is clearly illustrated in the configuration file "Microsoft Driver (Not Recommended!)", So use JTDS Driver first. 2, JTDS can go to http://sourceforge.net/projects/jtds current version 0.9
3, JTDS can only be used in JDK1.4 and above, JBuilderx default is JDK1.4
Ready to work:
1, download hibernate, the current highest version is 2.1.2
2. Create a lib in JBuilder, named hibernate_full, put all JAR under Hibernate / LIB to put it in hibernate / hibernate2.jar.
3. Create a lib in JBuilder, named JTDS, put jtds.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 an SRC directory in the TestHibernate project
5. Copy hibernate / src / hibernate.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
## MS SQL Server
# Hibernate.dialect net.sf.hibernate.dialect.SQLServerDialect # 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,, for example, the database server machine name we use is YUJ, the database name is Testhi, the user named from the database is SA, the password is SA, then the modification has become
## MS SQL Server
Hibernate.Dialev Net.sf.hibernate.dialect.sqlserverDialectHibernate.Connection.username Sahibernate.Connection.Password SA
## 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
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 ();}}