Hibernate additions and deletions to Oracle database

Source: Internet
Author: User
Tags generator log4j

This article 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


First step: Establish a MYECLIPSE connection Oracle9i database connection

In MyEclipse open myeclipse Java Enterprise Data exploer perspective form, left db broswer midpoint mouse right

Key--"New", and do the following in the form:

1. Click the "Add JARs" button to find the Oracle installation directory E:/oracle/ora90/jdbc/lib/classes12.jar

2.Driver Template Select Oracle in the Drop-down list (Thin Driver)

Input test in 3.Driver Name

4.Connect url:jdbc:oracle:thin:@127.0.0.1:1521:test (here test is the database name)

5.username:system (here system for database login username)

6.password:manager (Here is the Manager's password for the database login user)

7. Check "Save Password" and click "Finishe" button.

This setting MyEclipse to Oracle database completion. One more database connection called Test in the left DB broswer.

Click the right mouse button to open the database connection or edit and so on.

Step Two: Complete the project's new and hibernate libraries, log4j library references

1. Open MyEclipse Java Enterprise Development perspective form in MyEclipse, create a new webproject on the left

Known as Hibernatetest,

2. Selection of webproject items after the right mouse button select Myeclipse--add Hibernate capebilites--select Hibnernate3.1, check

Hibernate 3.1 Core Libraries, click the "Next" button, go to the Database setup form, select the first step in DB driver the new

Test connection. Click the "Next" button and enter a Sessionfactory package name like Sessionfolder.

3. Select WebProject Project after the right mouse button, choose "Build Path"--"Add Librarys"--"User Library"--Find your local

log4j the associated jar file.

Step three: Complete the related file code writing:

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.sql Database Script Content

CREATE TABLE Student (
STUDENT_ID Number (6) is not NULL PRIMARY KEY,
Student_name VARCHAR2 (Ten) is 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 Contents

<?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" >
<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>

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
{
Get a Sessionfactory object by 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 ();
Changing its properties by student's Setter method
Note student_id don't need us to set up
Stu.setstudent_name ("test");
Stu.setstudent_age (20);
Save the student object to the database through the session's Save () method
Session.save (Stu);
Commit a transaction
Tx.commit ();
Close 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. -->
<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 User name-->
<property name= "Hibernate.connection.username" >test</property>
<!--Oracle Password-->
<property name= "Hibernate.connection.password" >test</property>
<!--Oracle Adapter (anti-translation)-->
<property name= "Hibernate.dialect" >
Org.hibernate.dialect.Oracle9Dialect
</property>
<!--can be--> to prevent the table from being built
<property name= "Hibernate.hbm2ddl.auto" >update</property>
<!--easy to follow SQL execution and add--> to Hibernate.cfg.xml
<property name= "Hibernate.show_sql" >true</property>
<mapping resource= "Mappings/student.hbm.xml"/>

</session-factory>

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

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.