package hibernatetest;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;
import derek.hibernate.test.Admin;
import java.util.Iterator;
import java.util.List;
import java.sql.*;
public class TestConTest {
Session session = null;
protected void setUp(){
try {
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
System.out.println(config.getProperty("connection.url"));
session = sessionFactory.openSession();
}
catch (HibernateException ex1) {ex1.printStackTrace();}
}
//数据插入
public void testInsert(){
Admin admin = new Admin();
admin.setName("Hiber");
admin.setPasswd("gen");
System.out.println(admin.getName());
System.out.println(admin.getPasswd());
try {
Transaction ts = session.beginTransaction();
session.save(admin);
session.flush();
ts.commit();
}
catch (HibernateException ex) {ex.printStackTrace(); }
}
//Query方法查询数据
public void testQuery(){
try {
Query q = session.createQuery("from Admin where id=?");
q.setInteger(0,2);//这里的?跟JDBC的PreparedStatement方法的差不多,只不过这里的是以0开始,jdbc的是1开始。
System.out.println("Query方法查询数据");
//q.setFirstResult(2);//查询结果从第几行开始列出数据
//q.setMaxResults(10);//取多少条数据,就跟SQL SERVER的TOP方法和MYSQL的LIMIT方法一样的意思。
// ScrollableResults sc=q.scroll();//得到一个ScrollableResults,可滚动的,如果你的数据库支持游标自由移动的话可以加上,也就是说可以判断查询结果有没有值,或者移动到下一行记录等等.
Iterator it = q.iterate();
while(it.hasNext()){
Admin admin = (Admin)it.next();
System.out.println(admin.getId());
System.out.println(admin.getName());
System.out.println(admin.getPasswd());
}
session.flush();
}
catch (HibernateException ex) {ex.printStackTrace();}
// finally{
// try {
// session.close();
// }
// catch (HibernateException ex1) {ex1.printStackTrace();}
// }
}
/**
* Hibernate3.0中已经废弃了find()和iterate()方法
*/
//find方法查询数据
public void testFind(){
try {
List lt = session.find("from Admin");
System.out.println("find方法查询数据");
Iterator it = lt.iterator();
while(it.hasNext()){
Admin admin = (Admin)it.next();
System.out.println(admin.getId());
System.out.println(admin.getName());
System.out.println(admin.getPasswd());
}
session.flush();
}
catch (HibernateException ex) {ex.printStackTrace();}
// finally{
// try {
// session.close();
// }
// catch (HibernateException ex1) {ex1.printStackTrace();}
// }
}
//iterate方法查询数据
public void testIterator(){
try {
Iterator it = session.iterate("from Admin");
while(it.hasNext()){
Admin admin = (Admin)it.next();
System.out.println(admin.getId());
System.out.println(admin.getName());
System.out.println(admin.getPasswd());
session.flush();
}
}
catch (HibernateException ex) {ex.printStackTrace();}
// finally{
// try {
// session.close();
// }catch (HibernateException ex1) {ex1.printStackTrace();}
// }
}
//修改数据
public void testModify(){
try {
Connection con = session.connection();//得到数据库连接
Query q = session.createQuery("from Admin where id = 2");
Iterator it = q.iterate();
Transaction ts = session.beginTransaction();
if(it.hasNext()){
Admin admin = (Admin)it.next();
admin.setName("dada");
session.update(admin);
session.evict(admin);//调用Session的evict()方法立即释放它的内存
session.flush();
ts.commit();
}
}
catch (HibernateException ex) {ex.printStackTrace();}
// finally{
// try {
// session.close();
// }catch (HibernateException ex1) {ex1.printStackTrace();}
// }
}
/**
* delete()方法执行首先是执行select语句把符合条件的记录载入内存中,
* 然后做逐一删除即有多少条符合条件的记录就执行多少次删除命令
*/
//删除数据
public void testDelete(){
try {
int i = session.delete("from Admin where id = 2");
Transaction ts = session.beginTransaction();
System.out.println("删除数据状态 "+i);
session.flush();
}
catch (HibernateException ex) {ex.printStackTrace();}
// finally{
// try {
// session.close();
// }catch (HibernateException ex1) {ex1.printStackTrace();}
// }
}
public static void main(String arg[]){
TestConTest tt = new TestConTest();
tt.setUp();
tt.testInsert();
//tt.testDelete();
//tt.testModify();
tt.testIterator();
}
}
posted on 2005-09-13 16:55
Derek.Guo 阅读(732)
评论(0) 编辑 收藏 所属分类:
Java