package sevice.impl;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import db.MyHibernateSessionFactory;
import entity.Students;
import service.IStudentsDAO;
public class StudentsDAOImpl implements IStudentsDAO{
@Override
public List<Students> queryAllStudents() {
// TODO Auto-generated method stub
Transaction tx = null;
List<Students> list = null;
String hql = "";
try{
Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
hql = "from Students";
Query query = session.createQuery(hql);
list = query.list();
tx.commit();
return list;
}catch(Exception e){
e.printStackTrace();
tx.commit();
return list;
}finally{
if(tx!=null){
tx = null;
}
}
}
@Override
public Students queryStudentsBySid(String sid) {
// TODO Auto-generated method stub
Transaction tx = null;
Students s = null;
try{
Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
s =(Students) session.get(Students.class, sid);
tx.commit();
return s;
}catch(Exception e){
e.printStackTrace();
tx.commit();
return s;
}finally{
if(tx!=null){
tx = null;
}
}
}
@Override
public boolean addStudents(Students s) {
// TODO Auto-generated method stub
s.setSid(getNewSid());//设置学生学号
Transaction tx = null;
try{
Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
session.save(s);
tx.commit();
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}finally{
if(tx!=null){
tx = null;
}
}
}
@Override
public boolean updateStudents(Students s) {
// TODO Auto-generated method stub
Transaction tx = null;
try{
Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
session.update(s);
tx.commit();
return true;
}catch(Exception e){
e.printStackTrace();
tx.commit();
return false;
}finally{
if(tx!=null){
tx = null;
}
}
}
@Override
public boolean deleteStudents(String sid) {
// TODO Auto-generated method stub
Transaction tx = null;
try{
Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
Students s = (Students)session.get(Students.class, sid);
session.delete(s);
tx.commit();
return true;
}catch(Exception e){
e.printStackTrace();
tx.commit();
return false;
}finally{
if(tx!=null){
tx = null;
}
}
}
//生成学生学号
private String getNewSid(){
Transaction tx = null;
String hql = "";
String sid=null;
try{
Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
//获得当前学生的最大编号
hql = "select max(sid) from Students";
Query query = session.createQuery(hql);
sid =(String) query.uniqueResult();
if(sid==null||"".equals(sid)){
//给一个默认的最大编号
sid = "S0000001";
}else{
String temp = sid.substring(1);
int i = Integer.parseInt(temp);
i++;
temp = String.valueOf(i);
int len = temp.length();
//凑够七位
for(int j=0;j<7-len;j++){
temp = "0"+temp;
}
sid = "S"+temp;
}
tx.commit();
return sid;
}catch(Exception e){
e.printStackTrace();
tx.commit();
return sid;
}finally{
if(tx!=null){
tx = null;
}
}
}
//根据姓名查询学生信息
@Override
public List<Students> queryStudentsByName(String sname) {
// TODO Auto-generated method stub
Transaction tx = null;
List<Students> list = null;
String hql;
try{
Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
hql = "from Students where sname=?";
Query query = session.createQuery(hql);
query.setParameter(0, sname);
list = query.list();
tx.commit();
return list;
}catch(Exception e){
e.printStackTrace();
tx.commit();
return list;
}finally{
if(tx!=null){
tx = null;
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Struts2+Hibernate学生信息管理系统.zip (85个子文件)
studentMS-master
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.jdt.core.prefs 357B
.jsdtscope 552B
org.eclipse.wst.common.component 542B
org.eclipse.wst.common.project.facet.core.xml 335B
org.eclipse.wst.jsdt.ui.superType.container 49B
src
struts.xml 1KB
sevice
impl
UsersDAOImpl.java 983B
StudentsDAOImpl.java 4KB
service
IUsersDAO.java 140B
IStudentsDAO.java 528B
hibernate.cfg.xml 947B
entity
Users.hbm.xml 454B
Students.hbm.xml 578B
Users.java 640B
Students.java 1KB
db
MyHibernateSessionFactory.java 867B
action
StudentsAction.java 2KB
SuperAction.java 1KB
UsersAction.java 1KB
WebRoot
tree.jsp 3KB
users
Users_login_main.jsp 2KB
Users_login_success.jsp 2KB
Users_login.jsp 5KB
WEB-INF
lib
asm-commons-3.3.jar 37KB
ognl-3.0.6.jar 223KB
junit-4.10.jar 247KB
hibernate-core-4.2.4.Final.jar 4.41MB
dom4j-1.6.1.jar 307KB
log4j-api-2.2.jar 130KB
xwork-core-2.3.24.jar 661KB
mysql-connector-java-5.1.41-bin.jar 970KB
commons-lang3-3.2.jar 376KB
struts2-core-2.3.24.jar 812KB
javassist-3.15.0-GA.jar 633KB
hibernate-jpa-2.0-api-1.0.1.Final.jar 100KB
asm-tree-3.3.jar 21KB
jboss-logging-3.1.0.GA.jar 59KB
jboss-transaction-api_1.1_spec-1.0.1.Final.jar 25KB
log4j-core-2.2.jar 807KB
freemarker-2.3.22.jar 1.24MB
commons-io-2.2.jar 170KB
asm-3.3.jar 43KB
commons-fileupload-1.3.1.jar 67KB
antlr-2.7.7.jar 435KB
javassist-3.11.0.GA.jar 600KB
hibernate-commons-annotations-4.0.2.Final.jar 80KB
web.xml 607B
index.jsp 299B
js
Calendar3.js 15KB
components
dtree
img
folder.gif 372B
join.gif 69B
question.gif 1KB
globe.gif 1KB
nolines_plus.gif 870B
joinbottom.gif 66B
imgfolder.gif 622B
page.gif 582B
plus.gif 89B
musicfolder.gif 633B
cd.gif 239B
folderopen.gif 376B
minusbottom.gif 85B
trash.gif 1KB
empty.gif 62B
base.gif 621B
nolines_minus.gif 861B
minus.gif 86B
plusbottom.gif 88B
line.gif 66B
dtree.js 12KB
dtree.css 701B
css
default.css 7KB
index.css 3KB
students
Students_modify.jsp 4KB
Students_modify_success.jsp 2KB
Students_add.jsp 3KB
Students_query_success.jsp 4KB
Students_add_success.jsp 2KB
Students_query.jsp 3KB
images
icon_default.gif 621B
admin.png 48KB
welcome.jpg 31KB
arror.gif 56B
META-INF
MANIFEST.MF 36B
共 85 条
- 1
资源评论
博士僧小星
- 粉丝: 1918
- 资源: 5876
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功