Hibernate数据库增删改查
来源:互联网 发布:seo金刚 编辑:程序博客网 时间:2024/04/29 22:07
package com.my.dao;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.lovo.util.HibernateUtil;
import com.my.bean.StudentBean;
public class StudentDao {
Session session = HibernateUtil.getSession();
Transaction tx=null;
public List<StudentBean> showAllStudent() throws Exception{
List<StudentBean> list= session.createQuery("from StudentBean").list();
HibernateUtil.close();
return list;
}
public void addStudent(StudentBean student)throws Exception{
try {
tx = session.beginTransaction();
session.save(student);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}finally{
HibernateUtil.close();
}
}
public boolean updateStudent(StudentBean student)throws Exception{
try{
session=HibernateUtil.getSession();
tx=session.beginTransaction();
session.update(student);
tx.commit();
}catch(Exception e){
e.printStackTrace();
}finally{
if(session!=null){
session.close();
}
}
return false;
}
public Boolean delStudent(int id)throws Exception{
try{
StudentBean student = new StudentBean();
session=HibernateUtil.getSession();
tx=session.beginTransaction();
student = (StudentBean) session.load(student.getClass(),id);
session.delete(student);
tx.commit();
}catch(Exception e){
e.printStackTrace();
}finally{
if(session!=null){
session.close();
}
}
return false;
}
}
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.lovo.util.HibernateUtil;
import com.my.bean.StudentBean;
public class StudentDao {
Session session = HibernateUtil.getSession();
Transaction tx=null;
public List<StudentBean> showAllStudent() throws Exception{
List<StudentBean> list= session.createQuery("from StudentBean").list();
HibernateUtil.close();
return list;
}
public void addStudent(StudentBean student)throws Exception{
try {
tx = session.beginTransaction();
session.save(student);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}finally{
HibernateUtil.close();
}
}
public boolean updateStudent(StudentBean student)throws Exception{
try{
session=HibernateUtil.getSession();
tx=session.beginTransaction();
session.update(student);
tx.commit();
}catch(Exception e){
e.printStackTrace();
}finally{
if(session!=null){
session.close();
}
}
return false;
}
public Boolean delStudent(int id)throws Exception{
try{
StudentBean student = new StudentBean();
session=HibernateUtil.getSession();
tx=session.beginTransaction();
student = (StudentBean) session.load(student.getClass(),id);
session.delete(student);
tx.commit();
}catch(Exception e){
e.printStackTrace();
}finally{
if(session!=null){
session.close();
}
}
return false;
}
}
0 0
- Hibernate数据库增删改查
- Hibernate数据库增删改查代码
- Hibernate增删改查Oracle数据库
- hibernate增删查改
- hibernate增删改查
- Hibernate增删改查
- hibernate 增删改查
- hibernate 增删改查
- Hibernate 增删改查
- Hibernate增删改查
- hibernate 增删改查
- Hibernate增删改查
- Hibernate增删改查
- hibernate增删改查
- Hibernate--增删改查
- Hibernate--增删改查
- Hibernate-增删改查
- hibernate增删查改
- 各种编程语言的深度学习库整理
- LightOJ刷题记录
- Karel,那些本应该简单的问题
- 润乾报表关于当输入条件为空取出所有数据的几种用法
- Linux下浮点数转换成字符串gcvt函数
- Hibernate数据库增删改查
- hadoop之浏览器故障(安全证书认证失败)
- 删除数据库中冗余信息
- sdut 2710 1-4 输出菱形
- web预览本地图片(对象URL)
- 51Nod 1014 X^2 Mod P
- JavaScript基础系列1---初探JavaScript
- 排序数据
- 队列——排队买饭(数组模拟法)