Hibernate 连接 orcale(11r2版本)

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

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ctl.db">
<class name="Student" table="TB_STUDENT" >
<id name="id" type="int">
<generator class="native"/>
<!--设置id自增 -->
</id>
<property generated="never" lazy="false" name="name"/>
<property generated="never" lazy="false" name="password"/>
</class>
</hibernate-mapping>

hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">scott</property>//用户名
<property name="connection.password">Admin518</property>//密码
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="myeclipse.connection.profile">oracle.jdbc.driver.OracleDriver</property>
<property name="format_sql">true</property>
<property name="show_sql">true</property>
<mapping resource="com/ctl/db/Student.hbm.xml" />
</session-factory>

</hibernate-configuration>

你可能感兴趣的:(Hibernate)