zoukankan      html  css  js  c++  java
  • 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
    );

    创建序列号用于给表Student的Student_ID赋值

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

     

    4新建一个名为Hiber的工程

     

    5添加包

    添加hibernatejar中的所有包

     

     

     

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

     

     

     

    添加oracle的jdbc驱动程序ojdbc6.jar

     

     

    添加完成后

     

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

     

    1)hibernate.cfg.xml

     

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <!DOCTYPE hibernate-configuration PUBLIC  
    2.  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
    3.  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
    4. <hibernate-configuration>  
    5. <session-factory>  
    6.     <!--程序执行的时候是否显示真正的sql语句-->  
    7.     <property name="show_sql">true</property>  
    8.     <!--使用的SQL对应的“方言”,此处是Oracle11的“方言”-->  
    9.     <property name="dialect">org.hibernate.dialect.OracleDialect  
    10.     </property>  
    11.     <!--连接数据库的Driver-->  
    12.     <property name="connection.driver_class">  
    13.         oracle.jdbc.driver.OracleDriver  
    14.     </property>  
    15.     <!--数据库连接url-->  
    16.     <property name="connection.url">  
    17.         jdbc:oracle:thin:@localhost:1521:orcl  
    18.     </property>  
    19.     <!--用户名-->  
    20.     <property name="connection.username">system</property>  
    21.     <!--密码-->  
    22.     <property name="connection.password">oracle</property>  
    23.     <mapping resource="Student.hbm.xml"/>  
    24. </session-factory>  
    25. </hibernate-configuration>  

    2)Student.Java

     

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. public class Student  
    2. {  
    3.     private int student_id;  
    4.     private String student_name;  
    5.     private int student_age;  
    6.   
    7.     public int getStudent_id()  
    8.     {  
    9.         return student_id;  
    10.     }  
    11.     public String getStudent_name()  
    12.     {  
    13.         return student_name;  
    14.     }  
    15.     public int getStudent_age()  
    16.     {  
    17.         return student_age;  
    18.     }  
    19.     public void setStudent_id(int id)  
    20.     {  
    21.         this.student_id = id;  
    22.     }  
    23.     public void setStudent_name(String name)  
    24.     {  
    25.         this.student_name = name;  
    26.     }  
    27.     public void setStudent_age(int age)  
    28.     {  
    29.         this.student_age = age;  
    30.     }  
    31. }  

    3)Student.hbm.xml

     

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <?xml version="1.0"encoding="utf-8"?>  
    2. <!DOCTYPEhibernate-mapping  
    3.         PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
    4.        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
    5. <hibernate-mapping>  
    6.     <classname="Student"table="Student">  
    7.         <idname="student_id"column="student_id"type="java.lang.Integer">  
    8.             <generatorclass="native">  
    9.                 <paramname="sequence">student_sequence</param>  
    10.             </generator>  
    11.         </id>  
    12.         <propertyname="student_name"column="Student_Name"  
    13.                   type="java.lang.String"/>  
    14.         <propertyname="student_age"column="Student_Age"  
    15.                   type="java.lang.Integer"/>  
    16.     </class>  
    17. </hibernate-mapping>  

     

    4)Test.java

     

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. importorg.hibernate.*;  
    2. import org.hibernate.cfg.*;  
    3.   
    4. public class Test  
    5. {  
    6.     public static voidmain(String[]args)  
    7.     {  
    8.         try  
    9.         {  
    10.             //通过Configuration获得一个SessionFactory对象  
    11.             SessionFactory sf = new Configuration().configure().buildSessionFactory();  
    12.             //打开一个Session  
    13.             Session session= sf.openSession();  
    14.             //开始一个事务  
    15.             Transaction tx =session.beginTransaction();  
    16.             //创建一个Student对象  
    17.             Student stu =new Student();  
    18.             //通过Student的setter方法改变它的属性  
    19.             //注意student_id不用我们设置  
    20.             stu.setStudent_name("zhangsan");  
    21.             stu.setStudent_age(18);  
    22.             //通过session的save()方法将Student对象保存到数据库中  
    23.             session.save(stu);  
    24.             //提交事务  
    25.             tx.commit();  
    26.             //关闭会话  
    27.             session.close();  
    28.         }  
    29.         catch(Exception e)  
    30.         {  
    31.             e.printStackTrace();  
    32.         }  
    33.     }  
    34. }  

     

    7 验证

    1)运行Test.java,结果为

    Hibernate: select student_sequence.nextvalfrom dual

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

     

    2)从Oracle数据库中查询

  • 相关阅读:
    创建ROS工程結構
    ubuntu下boot分区空间不足问题的解决方案
    Ubuntu下查看自己的GPU型号
    win+Ubuntu双系统安装和卸载、Ubuntu上OpenCV+ROS环境配置
    Opencv——摄像头设置
    error:Assertion failed ((unsigned)i0 < (unsigned)size.p[0]) in cv::Mat::at
    error: OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in cv::Mat::Mat
    常用的传感器和运动机构
    步进电机与伺服电机
    Opencv——级联分类器(AdaBoost)
  • 原文地址:https://www.cnblogs.com/grimm/p/6732418.html
Copyright © 2011-2022 走看看