JBuilderX + SQL Server development hibernate

Source: Internet
Author: User

Environment:

Developed IDE: JBuilderX
  
Databases used: MS SQL Server 2000
  
Database Driver used: JSQL Driver (JDBC 3.0)

Note:

1. In the configuration file, hibernate explicitly states "Microsoft Driver (not recommended !)", Therefore, JSQL Driver is used first.
  
2, JSQL Driver can be obtained in the http://www.jnetdirect.com, You need to register a user, in order to download to the trial version.

3. JDBC3.0 can only be used in JDK1.4 and later versions. JBuilderX uses JDK1.4 by default.

Preparations:

1. Download Hibernate. The current maximum version is 2.1.2.

2. Create a lib in JBuilder and name it hibernate_full. All the jars under hibernatelib can be put in and hibernatehibernate2.jar is also put in.

3. Create a lib in JBuilder and name it JSQL3. Put JNetDirectJSQLConnectJDBC_3.0_DriverJSQLConnect.jar in JSQL Driver.

Example:
  
1. Create a project named testhibernate

2. Add hibernate_full and JSQL3 to Required Libraries in the attribute.

3. Select the xml file from the menu Project --> Project Properties --> Build --> Resource and select "Copy" -- when compiling the Project, the xml file in the src folder is automatically copied to the corresponding directory in the classes folder.

4. Create a src directory in the testhibernate Project

5. Copy the hibernatesrchib.pdf. properties and log4j. properties in the hibernate source file to the src directory in the testhibernate project.

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

Find

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

In this section, HypersonicSQL is used by default, and MS SQL Server is used, so comment out the entire segment.

# 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

# JSQL Driver
# Hibernate. connection. driver_class com. jnetdirect. jsql. JSQLDriver
# Hibernate. connection. url jdbc: JSQLConnect: // 1E1/test

For example, the name of the database server we use is yuj, the name of the database is testhi, the user name connecting to the database is sa, and the password is sa.

# 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, which is a standard JavaBean and has only three 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-link ing xml file "Person. hbm. xml" in 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">
<Hibernate-mapping>
<Class name = "testhibernate. Person">
<! -- Hibernate generates a primary key id for us -->
<Id name = "id" unsaved-value = "null">
<Generator class = "uuid. hex"/>
</Id>

<! -- By default, the class variables are mapped to columns with the same name. Of course, you can modify the ing mode. -->
<Property name = "name"/>
<Property name = "address"/>
</Class>
</Hibernate-mapping>

9. Create a client program named "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. It does not insert any data into the table and can only be used once. Otherwise, the existing table's
*/
Public class Client1
{
Private static SessionFactory sessionFactory;

Public static void main (String [] args) throws Exception
{
Configuration conf = new Configuration (). addClass (Person. class );

// Used to create a table in the database during the first running
// Output the SQL statement to the txt file
// This section of code cannot be used for subsequent operations. Otherwise, the original table will be deleted each time before the new 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
S. save (yuj); // at this time, yuj can be found in the database
S. save (x); // at this time, x can be found in the database

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

Two records are added to the database. OK! The preliminary use is successful, and the rest will be studied slowly ......

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.