Jbuilderx+sql Server Development hibernate_jsp programming

Source: Internet
Author: User
Environment:

Development of the Ide:jbuilderx
  
Database used: MS SQL Server 2000
  
Database driver used: Jsql Driver (JDBC 3.0)

Description

1. Hibernate explicitly "Microsoft Driver (not recommended!)" In the configuration file, so use Jsql Driver first.
  
2, Jsql driver can get to the http://www.jnetdirect.com, need to register a user before downloading to the trial version.

3, JDBC3.0 can only be used in JDK1.4 and above version, Jbuilderx the default is JDK1.4

Preparatory work:

1, download hibernate, the current highest version is 2.1.2

2, in the JBuilder to create a lib, named Hibernate_full, will hibernatelib all the jars under all put in, and will Hibernatehibernate2.jar also put in

3, in the JBuilder to create a lib, named JSQL3, will jsql driver under the Jnetdirectjsqlconnectjdbc_3.0_driverjsqlconnect.jar put in

Start with examples:
  
1, create a project, named Testhibernate

2. Add Hibernate_full and JSQL3 to the required libraries in the attribute

3. Select the XML file in the menu Project--> Project Properties--> build--> Resource, select "Copy"--When compiling the project, Automatically copies the XML files in the SRC folder to the corresponding directories in the Classes folder

4. Create a src directory in the Testhibernate project

5, the Hibernate source files in the hibernatesrchibernate.properties and log4j.properties copy to the Testhibernate project in the SRC directory

6, modify the configuration of MS SQL Server 2000 driver in Hibernate.properties

Found it

# # 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:.

This paragraph, here is the default is to use Hypersonicsql, we are using MS SQL Server, so the whole comment out

# # 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:.

Also, find

# # MS SQL Server

#hibernate. Dialect Net.sf.hibernate.dialect.SQLServerDialect
#hibernate. Connection.username SA
#hibernate. Connection.password SA

# # Jsql Driver
#hibernate. Connection.driver_class Com.jnetdirect.jsql.JSQLDriver
#hibernate. Connection.url jdbc:jsqlconnect://1e1/test

This paragraph, for example we use the database server machine name is Yuj, the database name is Testhi, connects to the database the user name is SA, the password is SA, then after the modification this paragraph becomes

# # MS SQL Server

Hibernate.dialect Net.sf.hibernate.dialect.SQLServerDialect
Hibernate.connection.username SA
Hibernate.connection.password SA

# # Jsql Driver
Hibernate.connection.driver_class Com.jnetdirect.jsql.JSQLDriver
Hibernate.connection.url Jdbc:jsqlconnect://yuj/testhi

7, create a class testhibernate. Person, this is a standard JavaBean, only 3 attributes and the corresponding Getset method

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 mapping of the XML file Person.hbm.xml, placed under the same directory as Person.java

<?xml version= "1.0" encoding= "GB2312"?
! DOCTYPE hibernate-mapping SYSTEM "HTTP://HIBERNATE.SOURCEFORGE.NET/HIBERNATE-MAPPING-2.0.DTD" >
<class name= "Testhibernate. Person ">
!--hibernate generates primary keys for US id-->
<ID name = "id" unsaved-value = "null"
<generator class= "Uuid.hex"/>
</id>

!--default to map the variables of a class to a table column of the same name, of course, we can modify its mapping method-->
<property name= "Name"/>
<property name= "Address"/>
</class>

9. Create a client program that invokes the 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 the table, does not insert any data inside the table, and can only be used once, otherwise the existing table will be deleted
*/
public class Client1
{
private static Sessionfactory sessionfactory;

public static void Main (string[] args) throws Exception
{
Configuration conf = new Configuration (). addclass (Person.class);

The first run time is used to create a table in the database
and output the SQL statement to the TXT file
This code is not available for future runs, otherwise the original table is deleted and the table is created.
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 of
S.save (Yuj); At this point, Yuj can already find in the database
S.save (x); At this point, X can already be found in the database

T.commit ();
S.close ();
}
}

To view the database, add 2 entries, ok! The initial use of success, the rest of the slowly study it ...
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.