---
title: "Hibernate 连接 orcale（11r2版本）"
author: "txf2004"
date: "2012-12-16T17:25:00Z"
tags: []
slug: "hibernate-orcale-11r2"
draft: false
lastmod: "2013-02-02T20:36:13Z"
id: 1783867
url: "https://txf2004.iteye.com/blog/1783867"
---

Student.java

package com.ctl.db;

public class Student {  
private int id;  
private String name;  
private String password;  
public int getId() {  
return id;  
}

public String getName() {  
return name;  
}

public String getPassword() {  
return password;  
}

public void setId(int id) {  
this.id = id;  
}

public void setName(String name) {  
this.name = name;  
}

public void setPassword(String password) {  
this.password = password;  
}  
}

StudentTestDemo.java

package com.ctl.db;

import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.PreparedStatement;  
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.util.Random;

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

public class StudentTestDemo {

public static void main(String\[] args) throws SQLException {  
Student s = new Student();

Connection conn = null;  
PreparedStatement stmt = null;  
ResultSet rs = null;

//s.setId(2);  
for(int a=0 ;a&lt;30;a++){  
s.setName("ctl");  
s.setPassword("518");  
Configuration cfg = new Configuration();  
SessionFactory sf = cfg.configure().buildSessionFactory();  
Session session = sf.openSession();  
session.beginTransaction();  
session.save(s);  
session.getTransaction().commit();  
session.close();  
sf.close();}  
}  
}

Student.hbm.xml

&lt;?xml version="1.0"?&gt;  
&lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
"[http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd](http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd)"&gt;  
&lt;hibernate-mapping package="com.ctl.db"&gt;  
&lt;class name="Student" table="TB\_STUDENT" &gt;  
&lt;id name="id" type="int"&gt;  
&lt;generator class="native"/&gt;  
&lt;!--设置id自增 --&gt;  
&lt;/id&gt;  
&lt;property generated="never" lazy="false" name="name"/&gt;  
&lt;property generated="never" lazy="false" name="password"/&gt;  
&lt;/class&gt;  
&lt;/hibernate-mapping&gt;

hibernate.cfg.xml

&lt;?xml version='1.0' encoding='UTF-8'?&gt;  
&lt;!DOCTYPE hibernate-configuration PUBLIC  
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
"[http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd](http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd)"&gt;

&lt;!-- Generated by MyEclipse Hibernate Tools. --&gt;  
&lt;hibernate-configuration&gt;

&lt;session-factory&gt;  
&lt;property name="hbm2ddl.auto"&gt;update&lt;/property&gt;  
&lt;property name="dialect"&gt;org.hibernate.dialect.OracleDialect&lt;/property&gt;  
&lt;property name="connection.url"&gt;jdbc:oracle:thin:@localhost:1521:orcl&lt;/property&gt;  
&lt;property name="connection.username"&gt;scott&lt;/property&gt;//用户名  
&lt;property name="connection.password"&gt;Admin518&lt;/property&gt;//密码  
&lt;property name="connection.driver\_class"&gt;oracle.jdbc.driver.OracleDriver&lt;/property&gt;  
&lt;property name="myeclipse.connection.profile"&gt;oracle.jdbc.driver.OracleDriver&lt;/property&gt;  
&lt;property name="format\_sql"&gt;true&lt;/property&gt;  
&lt;property name="show\_sql"&gt;true&lt;/property&gt;  
&lt;mapping resource="com/ctl/db/Student.hbm.xml" /&gt;  
&lt;/session-factory&gt;

&lt;/hibernate-configuration&gt;