使用Hibernate编写通用数据库操作代码
- ·创建struts1.2 + Hibernate3.0 Web工
·Struts+Spring+Hibernate组装web应用
·Hibernate 的原理与配置快速入门
·我找到一个最简单的hibernate入门教程
·Hibernate 实例学习笔记
·在MyEclipse快速搭建Hibernate应用
·spring+hibernate+jbpm整合成功
·整合 JSF+Spring+Hibernate的小例子
·Hibernate配置文件中映射元素详解
·混合Eclipse、WTP、Struts和Hibernate
insert方法
public void insert(Object o){
Session session = HibernateSessionFactory.currentSession();
Transaction t = session.beginTransaction();
session.save(o);
t.commit();
HibernateSessionFactory.clossSession();
}
delete方法
public void delete(Object o,Serializable id){
Session session = HibernateSessionFactory.currentSession():
Transaction t = session.beginTransaction();
Object o = session.get(o.class,id);
if(o!=null){
session.delete(o);
}
t.commit();
HibernateSessionFactory.clossSession();
}
update方法
public void update(Object o,Serializable id){
Session session = HibernateSessionFactory.currentSession();
Transaction t = session.beginTransaction();
session.update(o,id);
t.commit();
HibernateSessionFactory.clossSession();
}
基于HQL的通用select方法
public ArrayList select(String sql){
Session session = HibernateSessionFactory.currentSession();
Query query = createQuery(sql);
List list = query.list();
HibernateSessionFactory.clossSession();
return (ArrayList)list;
}
基于SQL的通用select方法
public ArrayList select(String sql) throws Exception{
Session session = HibernateSessionFactory.currentSession();
Connection con = session.connection();
PreparedStatement pstmt = con.preparedStatement(sql);
ResultSet rs = pstmt.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
Hashtable ht = null;
ArrayList array = new ArrayList();
while(rs.next()){
ht = new Hashtable();
for(int i=0;i<rsmd.getColumnCount();i++){
ht.put(rsmd.getColumnName(i+1),rs.getObject(i+1));
}
array.add(ht);
}
HibernateSessionFactory.clossSession();
return array;
}
- 最新文章
- 在多种数据库环境下管理业务需求(上)[01-08]
- 重新编译PLSQL中无效对象或指定对象[01-08]
- OpenBSD4.0 PostgreSQL8.1.5数据库的..[01-08]
- PostgreSQL 最新8.2版发布[01-08]
- 用触发器生成数据库表的数据操作日志[01-08]
- 有关数据库概念设计几点见解经验谈[01-08]
- 相关文章
- 在多种数据库环境下管理业务需求(上)[01-08]
- OpenBSD4.0 PostgreSQL8.1.5数据库的..[01-08]
- 用触发器生成数据库表的数据操作日志[01-08]
- 有关数据库概念设计几点见解经验谈[01-08]
- PostgreSQL数据库学习手册之日期/时间支持[01-08]
- VFP中利用Excel制作数据库报表[01-08]
