Development Environment: win2003 + Tomcat 5.5 + myeclipse 5.5.1 GA + jdk1.5.0 _ 15 + Oracle9i
Software installation path:
Tomcat 5.5: C:/program files/Apache Software Foundation/tomcat 5.5
Myeclipse 5.5.1 GA: C:/program files/myeclipse 5.5.1 ga
Jdk1.5.0 _ 15: C:/program files/Java/jdk1.5.0 _ 15
Jre1.5.0 _ 15: C:/program files/Java/jre1.5.0 _ 15
Oracle9i: e:/Oracle/ora90
Log4j: D:/JAR/log4j-1.2.15/log4j-1.2.15.jar
Step 1: Establish a myeclipse connection to the Oracle9i database connection
Open the myeclipse Java Enterprise Data exploer perspective form in myeclipse, and right-click dB broswer on the left
Key -- "new", perform the following operations in the form:
1. Click "add jars" and find E:/Oracle/ora90/jdbc/lib/classes12.jar in the Oracle installation directory.
2. Select Oracle (thin driver) from the driver template drop-down list)
3. Enter test in driver name
4. Connect URL: JDBC: oracle: thin: @ 127.0.0.1: 1521: Test (here test is the database name)
5. Username: System (here the system is the user name for database logon)
6. Password: Manager (here, the manager is the password of the database logon user)
7. Check "Save Password" and click "finishe.
So far, setting myeclipse to the Oracle database is complete. In the left dB broswer, a database connection named test is added.
Right click to open the database connection or edit the database.
Step 2: Create a project and reference the hibernate library and log4j library.
1. Open the myeclipse Java Enterprise Development Perspective form in myeclipse, and create a webproject on the left
The name is hibernatetest,
2. After selecting a webproject project, right-click and choose myeclipse -- add hibernate capebilites -- select hibnernate3.1, and select
Hibernate 3.1 core libraries, click "Next" to enter the database settings form, select
Connect to test. Click "Next" and enter a sessionfactory package name, such as sessionfolder.
3. Right-click a webproject project and choose "build path" -- "add librarys" -- "User library" -- find your local
Jar files related to log4j.
Step 3: complete the compilation of relevant file code:
The file directory structure is as follows:
1. src/DB/student. SQL
2. src/DB/studentsequence. SQL
3. src/mappings/student. HBM. xml
4. src/model/student. Java
5. src/test/teststudent. SQL
6. src/hibernate. cfg. xml
7. src/hibernate. Properties
8. src/log4j. Properties
1. src/DB/student. sqldatabase script content
Create Table student (
Student_id number (6) Not null primary key,
Student_name varchar2 (10) Not null,
Student_age number (2) not null
);
2. src/DB/studentsequence. SQL database script content
Create sequence student_sequence
Increment by 1
Start with 1000
Nomaxvalue
Nocycle
Cache 10;
3. src/mappings/student. HBM. xml file content
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype hibernate-Mapping
Public "-// hibernate/hibernate mapping DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<Hibernate-mapping>
<Class name = "model. Student" table = "student">
<ID name = "student_id" column = "student_id" type = "Java. Lang. Integer">
<Generator class = "native">
<Param name = "sequence"> student_sequence </param>
</Generator>
</ID>
<Property name = "student_name" column = "student_name" type = "Java. Lang. String"/>
<Property name = "student_age" column = "student_age" type = "Java. Lang. Integer"/>
</Class>
</Hibernate-mapping>
4. src/model/student. Java content
Package Model;
Public class student
{
Private int student_id;
Private string student_name;
Private int student_age;
Public int getstudent_id ()
{
Return student_id;
}
Public String getstudent_name ()
{
Return student_name;
}
Public int getstudent_age ()
{
Return student_age;
}
Public void setstudent_id (int id)
{
This. student_id = ID;
}
Public void setstudent_name (string name)
{
This. student_name = Name;
}
Public void setstudent_age (INT age)
{
This. student_age = age;
}
}
5. src/test/teststudent. SQL content
Package test;
Import model. student;
Import org. hibernate .*;
Import org. hibernate. cfg .*;
Public class teststudent {
Public static void main (string [] ARGs)
{
Try
{
// Obtain a sessionfactory object through Configuration
Sessionfactory Sf = new configuration (). Configure (). buildsessionfactory ();
// Open a session
Session session = SF. opensession ();
// Start a transaction
Transaction Tx = session. begintransaction ();
// Create a student object
Student Stu = new student ();
// Use the setter method of student to change its attributes.
// Note that student_id does not need to be set
Stu. setstudent_name ("test ");
Stu. setstudent_age (20 );
// Save the student object to the database through the SAVE () method of the session
Session. Save (Stu );
// Submit the transaction
TX. Commit ();
// Close the session
Session. Close ();
}
Catch (exception E)
{
E. printstacktrace ();
}
}
}
6. src/hibernate. cfg. XML content
<? XML version = '1. 0' encoding = 'utf-8'?>
<! Doctype hibernate-configuration public
"-// Hibernate/hibernate configuration DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd>
<! -- Generated by myeclipse hibernate tools. -->
<Hibernate-configuration>
<Session-factory>
<! -- Oracle database connection string -->
<Property name = "hibernate. Connection. url"> JDBC: oracle: thin: @ 127.0.0.1: 1521: Test
</Property>
<! -- Oracle Driver Class -->
<Property name = "hibernate. Connection. driver_class"> oracle. JDBC. Driver. oracledriver </property>
<! -- Oracle username -->
<Property name = "hibernate. Connection. username"> test </property>
<! -- Oracle password -->
<Property name = "hibernate. Connection. Password"> test </property>
<! -- Oracle adapter -->
<Property name = "hibernate. dialect">
Org. hibernate. dialect. oracle9dialect
</Property>
<! -- Prevents the table from being created again -->
<Property name = "hibernate. hbm2ddl. Auto"> Update </property>
<! -- Easy to track SQL Execution, add in hibernate. cfg. xml -->
<Property name = "hibernate. show_ SQL"> true </property>
<Mapping Resource = "mappings/student. HBM. xml"/>
</Session-factory>
</Hibernate-configuration>
7. src/hibernate. properties content
Hibernate. dialect = org. hibernate. dialect. oracle9dialect
Hibernate. Connection. driver_class = oracle. JDBC. Driver. oracledriver
Hibernate. Connection. url = JDBC: oracle: thin: @ 127.0.0.1: 1521: Test
Hibernate. Connection. Username = test
Hibernate. Connection. Password = test
Hibernate. show_ SQL = true
8. src/log4j. properties content
Log4j. rootlogger = debug, console, file
Log4j.addivity.org. Apache = true