Java笔记8:Hibernate连接Oracle



1下载hibernate-3.6.0 Final.zip到任意目录,解压缩后得到hibernate目录

 

2下载slf4j-1.7.13.zip到任意目录,解压缩后得到slf4j-1.7.13

 

3操作数据库

sqlplus system/oracle

创建表

create table Student

(
 Student_ID  number(6) NOT NULLPRIMARY KEY,
 Student_Name varchar2(10) NOT NULL,
 Student_Age number(2) NOT NULL
);

创建序列号用于给表StudentStudent_ID赋值

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

 

4新建一个名为Hiber的工程

 

5添加包

添加hibernate\jar中的所有包

 


添加slf4j-1.7.13中的slf4j-nop-1.7.13.jar

 


添加oraclejdbc驱动程序ojdbc6.jar

 


添加完成后


6 添加两个配置文件和两个类

1hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <!--程序执行的时候是否显示真正的sql语句-->
    <property name="show_sql">true</property>
    <!--使用的SQL对应的“方言”,此处是Oracle11的“方言”-->
    <property name="dialect">org.hibernate.dialect.OracleDialect
    </property>
    <!--连接数据库的Driver-->
    <property name="connection.driver_class">
        oracle.jdbc.driver.OracleDriver
    </property>
    <!--数据库连接url-->
    <property name="connection.url">
        jdbc:oracle:thin:@localhost:1521:orcl
    </property>
    <!--用户名-->
    <property name="connection.username">system</property>
    <!--密码-->
    <property name="connection.password">oracle</property>
    <mapping resource="Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>

2Student.java

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;
    }
}

3Student.hbm.xml

<?xml version="1.0"encoding="utf-8"?>
<!DOCTYPEhibernate-mapping
        PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
       "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <classname="Student"table="Student">
        <idname="student_id"column="student_id"type="java.lang.Integer">
            <generatorclass="native">
                <paramname="sequence">student_sequence</param>
            </generator>
        </id>
        <propertyname="student_name"column="Student_Name"
                  type="java.lang.String"/>
        <propertyname="student_age"column="Student_Age"
                  type="java.lang.Integer"/>
    </class>
</hibernate-mapping>
 

4Test.java

importorg.hibernate.*;
import org.hibernate.cfg.*;

public class Test
{
    public static voidmain(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("zhangsan");
            stu.setStudent_age(18);
            //通过session的save()方法将Student对象保存到数据库中
            session.save(stu);
            //提交事务
            tx.commit();
            //关闭会话
            session.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}
 

验证

1)运行Test.java,结果为

Hibernate: select student_sequence.nextvalfrom dual

Hibernate: insert into Student(Student_Name, Student_Age, student_id) values (?, ?, ?)

 

2)从Oracle数据库中查询

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值