Hibernate增删改查Oracle数据库

本文开发环境:Win2003+Tomcat 5.5+MyEclipse 5.5.1 GA+jdk1.5.0_15+Oracle9i

软件安装路径:

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


第一步:建立一个Myeclipse连接Oracle9i数据库连接

在MyEclipse打开MyEclipse Java Enterprise Data Exploer Perspective窗体,左边DB Broswer中点鼠标右

键--“New”,在窗体中进行如下操作:

1.点击"Add JARs"按钮,找到Oracle安装目录下的E:/oracle/ora90/jdbc/lib/classes12.jar

2.Driver Template下拉列表中选择Oracle (Thin driver)

3.Driver Name 中输入test

4.Connect URL: jdbc:oracle:thin:@127.0.0.1:1521:test(此处test为数据库名)

5.UserName: system(此处system为数据库登录用户名)

6.Password: Manager(此处Manager为数据库登录用户对应的密码)

7.勾选中"Save Password",点击"Finishe"按钮。

至此设置MyEclipse到Oracle数据库完成。在左边DB Broswer中多出一名称为test的数据库连接。

点鼠标右键可打开数据库连接或者是进行编辑等操作。

 

第二步:完成项目的新建和Hibernate库、log4j库引用

1.在MyEclipse打开MyEclipse Java Enterprise Development Perspective窗体,左边新建立一WebProject命

名为HibernateTest,

2.选种WebProject项目后点鼠标右键选择MyEclipse--Add Hibernate Capebilites--选择Hibnernate3.1,勾选

Hibernate 3.1 core Libraries,点击"Next"按钮,进入数据库设置窗体,在DB Driver中选中第一步新建的

test连接即可。点击"Next"按钮,随便输入一个SessionFactory包名字如SessionFolder.

3.选种WebProject项目后点鼠标右键, 选择"Build Path"--"Add Librarys"--"User Library"--找到你本地的

log4j相关的jar文件。

 

第三步:完成相关文件代码编写:

文件目录结构如下:

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数据库脚本内容

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数据库脚本内容

CREATE SEQUENCE student_sequence
INCREMENT BY 1
START WITH 1000
NOMAXVALUE
NOCYCLE
CACHE 10;


3.src/mappings/Student.hbm.xml文件内容

<?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内容

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内容

package test;
import model.Student;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class TestStudent {
 public static void main(String[] args)
    {
        try
        {
            //通过Configuration获得一个SessionFactory对象
         SessionFactory sf = new Configuration().configure().buildSessionFactory();
            //打开一个Session
            Session session = sf.openSession();
            //开始一个事务
            Transaction tx = session.beginTransaction();
            //创建一个Student对象
            Student stu = new Student();
            //通过Student的setter方法改变它的属性
            //注意student_id不用我们设置
            stu.setStudent_name("test");
            stu.setStudent_age(20);
            //通过session的save()方法将Student对象保存到数据库中
            session.save(stu);
            //提交事务
            tx.commit();
            //关闭会话
            session.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }  
}


6.src/hibernate.cfg.xml内容

<?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数据库连接字符串 -->
  <property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:test
  </property>
  <!-- Oracle驱动类 -->
  <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  <!-- Oracle用户名 -->
  <property name="hibernate.connection.username">test</property>
  <!-- Oracle密码 -->
  <property name="hibernate.connection.password">test</property>
  <!-- Oracle适配器(反译) -->
  <property name="hibernate.dialect">
   org.hibernate.dialect.Oracle9Dialect
  </property>
  <!-- 可以防表被重新建立 -->
  <property name="hibernate.hbm2ddl.auto">update</property>
  <!-- 方便跟踪SQL执行,在hibernate.cfg.xml中加入 -->
  <property name="hibernate.show_sql">true</property>
  <mapping resource="mappings/Student.hbm.xml" />

 </session-factory>
</hibernate-configuration>

7.src/hibernate.properties内容

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内容

log4j.rootLogger=DEBUG,CONSOLE,FILE  
log4j.addivity.org.apache=true

你可能感兴趣的:(oracle,数据库,Hibernate,log4j,MyEclipse,session)