Oracle 增删改查

Posted Onlyだ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle 增删改查相关的知识,希望对你有一定的参考价值。

Oracle入门案例:

1.创建实体类Student 并重写ToString方法

package cn.happy.entity;

public class Student {

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", id=" + id + "]";
    }
    private String name;
    private Integer age;
    private Integer id;
    

}

2.导入jar包

3.创建大配置关联小配置

 

<?xml version=\'1.0\' encoding=\'utf-8\'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">scott</property>
<property name="connection.password">0123</property>

<!-- 输出所有 SQL 语句到控制台。 -->
<property name="hibernate.show_sql">true</property>

<!-- 在 log 和 console 中打印出更漂亮的 SQL。 -->
<property name="hibernate.format_sql">true</property>
<!-- 方言 -->
<property name="hibernate.dialect"> org.hibernate.dialect.Oracle10gDialect</property>

<property name="hbm2ddl.auto">update</property>


<mapping resource="cn/happy/entity/Student.hbm.xml"/>


</session-factory>

</hibernate-configuration>

4.编写小配置coding=\'utf-8\'?>

<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 
 <hibernate-mapping package="cn.happy.entity">
     <class name="Student" table="Student">
         <id name="id" type="int" column="id">
         </id>
         <property name="name" type="string" column="name"/>
         <property name="age" type="int" column="age"/>
     </class>    
 </hibernate-mapping>

5.工具类

 

 

package cn.happy.entity;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private static Configuration cfg=new Configuration().configure();
    private static SessionFactory factory=cfg.buildSessionFactory();
    
     //01.方法返回session
    public static Session getSession(){
        return factory.openSession();
    }
    
    //02.关闭session
    public static void closeSession(){
        getSession().close();
    }
    

}

6.测试类

    //  1.2 修改学生 
     
    @Test
    public void updateTest(){
         Session session = HibernateUtil.getSession();
         
         //不被上下文跟踪对象 
         /*Student stu=new Student();
         stu.setId(3);
         stu.setName("微冷的雨训练营");*/
         
         //方式二:如何用呗上下文跟踪的方式
         //检索出一条记录,一个实体对象
         Student stu= (Student)session.load(Student.class,3);
         stu.setName("金龙加油!!!!");

         Transaction tx = session.beginTransaction();
         session.update(stu);
         tx.commit();
         HibernateUtil.closeSession();
         System.out.println("update ok!");
    }
    

 

以上是关于Oracle 增删改查的主要内容,如果未能解决你的问题,请参考以下文章

Java操作Oracle并实现增删改查

oracle表的crud(增删改查)单表

asp.net mvc oracle 做的增删改查功能,给我份源码。

java:Oracle(table的增删改查,data的增删改查)

用java做界面连接oracle数据库实现增删改查功能,具体步骤怎么做?

Oracle增删改查--之删