Java中使用Hibernate存储Date类型及Boolean类型到Orcale数据库中的心得
来源:互联网 发布:淘宝网橡胶木沙发 编辑:程序博客网 时间:2024/05/12 22:47
首先在Oracle中创建一个Customers的表:
create table CUSTOMERS
(
ID NUMBER default 0 not null,
NAME VARCHAR2(15) not null,
AGE NUMBER not null,
IS_STUDENT VARCHAR2(1) default 1 not null,
REGTIME DATE
);
/
接着编写对应的类Customers.java存放在src/entity目录下,注意下面的类中使用的Date类必须是在java.util.*包内,否则将会引起错误,另外boolean型的私有成员的命名不可以以is开头,否则将引发错误,比如isStudent:
package entity;
import java.io.Serializable;
import java.util.*;
public class Customers implements Serializable {
private Integer id;
private String name;
private int age;
private boolean student;
private Date regTime;
public Customers() {
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
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 boolean isStudent() {
return student;
}
public void setStudent(boolean student) {
this.student = student;
}
public Date getRegTime() {
return regTime;
}
public void setRegTime(Date regTime) {
this.regTime = regTime;
}
}
为类创建对应的Mapping 文件Customers.hbm.xml存放在src/entity目录下,注意下面的regTime属性的type类型为java.util.Date:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping><!-- Created by the Middlegen Hibernate plugin http://boss.bekk.no/boss/middlegen/ http://hibernate.sourceforge.net/-->
<class name="entity.Customers" table="customers">
<id name="id" type="java.lang.Integer" column="id">
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String" column="name" length="15" />
<property name="age" type="java.lang.Integer" column="age" length="11" />
<property name="student" type="java.lang.Boolean" column="IS_STUDENT" length="1" />
<property name="regTime" type="java.util.Date" column="regTime" />
</class>
</hibernate-mapping>
Hibernate配置文件存放在src目录下:
<?xml version='1.0' encoding='GB2312'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 是否将运行期生成的SQL输出到日志以供调试 -->
<property name="show_sql">true</property>
<!-- Oracle方言,这里设定的是Oracle -->
<property name="dialect">net.sf.hibernate.dialect.Oracle9Dialect</property>
<!-- JDBC驱动程序 -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<!-- JDBC URL-->
<property name="connection.url">jdbc:oracle:thin:@192.168.1.95:1521:XianBin</property>
<!-- 数据库用户名 -->
<property name="connection.username">system</property>
<!-- 数据库密码 -->
<property name="connection.password">pwd</property>
<!-- 数据库重新连接 -->
<property name="connection.autoReconnect">true</property>
<property name="connection.autoReconnectForPools">true</property>
<property name="connection.is-connection-validation-required">false</property>
<!-- 指定User的映射文件 -->
<mapping resource="entity/Customers.hbm.xml" />
</session-factory>
</hibernate-configuration>
创建useHibernate.java文件,存放在src/business目录下:
import entity.Customers;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
public class useHibernate {
public static SessionFactory sessionFactory; // 初始化Hibernate,创建SessionFactory实例
public void saveCustomers(Customers customers) throws Exception {
Configuration config = null;
config = new Configuration().configure(); // 创建一个SessionFactory 实例
sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
/* 开始一个事务 */
tx = session.beginTransaction();
session.save(customers); /* 提交事务 */
tx.commit();
} catch (Exception e) { // TODO Auto-generated catch block
if (tx != null)
tx.rollback();
throw e;
} finally {
session.close();
}
}
}
package business;
- Java中使用Hibernate存储Date类型及Boolean类型到Orcale数据库中的心得
- Java中使用Hibernate存储Date类型及Boolean类型到Orcale数据库中的心得
- java中Date类型存储到mysql
- Java中的boolean类型
- Java中boolean类型
- 使用Navicat Premium将Oracle数据库中的表和数据迁移到MySQL数据库中,遇到的Date类型出现精度问题及解决方法
- Sqlite中存储Boolean和boolean对象,分别使用boolean和text类型
- 数据库定义的类型为int要存储JAVA中的Date类型数据
- hibernate中java.util.Date类型映射
- sql数据库与java中的date类型
- sql中的date数据到java中的date类型转换
- 数据库中date类型
- java Date类型插入orcale数据库是出现时分秒丢失现象
- 关于 hibernate 中的Date 类型
- Hibernate 中boolean类型属性问题
- Hibernate 中boolean类型属性问题
- hibernate+mysql java中boolean类型的映射问题+struts2取boolean值
- 存储日期类型到数据库中问题解决
- EJB3.0学习感触
- PHP分页算法详解
- 前期的工作比后期的努力更重要
- PHP的SQL注入
- 在c里面,输入密码,只显示*。(以示安全?)
- Java中使用Hibernate存储Date类型及Boolean类型到Orcale数据库中的心得
- 用比较的方法认识PHP的面向对象
- 在php中进行网页重定向
- 故事——回忆连载(三)
- 专业SQL和ORACLE数据库恢复和修复成功案例
- ajax与java的结合-direct web remoting
- 地铁
- 设计规范一(数据库)
- VSS使用手册