📄 studentdaoimp.java
字号:
package com.stuman.dao.imp;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import com.stuman.dao.StudentDAO;
import com.stuman.dao.hibernate.HibernateUtil;
import com.stuman.domain.Student;
import com.stuman.util.BaseDao;
import com.stuman.util.PageBean;
public class StudentDAOImp extends BaseDao implements StudentDAO {
private static Log log = LogFactory.getLog(StudentDAOImp.class);
public List getStudent() {
// TODO Auto-generated method stub
try {
Session s = HibernateUtil.currentSession();
HibernateUtil.beginTransaction();
List results = s.createQuery("from Student stu").list();
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
if (results != null && results.size() > 0) {
return results;
}
} catch (HibernateException e) {
log.fatal(e);
}
return null;
}
public boolean deleteStudentByID(String id) {
// TODO Auto-generated method stub
try {
Session s = HibernateUtil.currentSession();
HibernateUtil.beginTransaction();
Student stu = (Student) s.load(Student.class, id);
HibernateUtil.commitTransaction();
System.out.println("stuid===========" + stu.getId());
HibernateUtil.beginTransaction();
s.delete(stu);
HibernateUtil.commitTransaction();
s.flush();
HibernateUtil.closeSession();
return true;
} catch (HibernateException e) {
log.fatal(e);
}
return false;
}
public Student getStudentByID(String id) {
// TODO Auto-generated method stub
try {
Session s = HibernateUtil.currentSession();
HibernateUtil.beginTransaction();
Student stu = (Student) s.load(Student.class, id);
HibernateUtil.commitTransaction();
s.flush();
HibernateUtil.closeSession();
return stu;
} catch (HibernateException e) {
log.fatal(e);
}
return null;
}
public boolean updateStudent(Student student) {
// TODO Auto-generated method stub
try {
Session s = HibernateUtil.currentSession();
HibernateUtil.beginTransaction();
s.update(student);
System.out.println("update student id =" + student.getId());
HibernateUtil.commitTransaction();
s.flush();
HibernateUtil.closeSession();
} catch (HibernateException e) {
log.fatal(e);
return false;
}
Student stu = this.getStudentByID(student.getId());
if (stu == null) {
System.out.println("stu===null");
} else {
System.out.println("&&&&&&&&&&&&&&&===" + stu.getName() + ",'"
+ stu.getJiguan());
}
return true;
}
public boolean saveStudent(Student stu) {
// TODO Auto-generated method stub
try {
Session s = HibernateUtil.currentSession();
HibernateUtil.beginTransaction();
s.save(stu);
System.out.println("save student id =" + stu.getId());
HibernateUtil.commitTransaction();
s.flush();
HibernateUtil.closeSession();
return true;
} catch (HibernateException e) {
log.fatal(e);
}
return false;
}
/*
* (non-Javadoc)
*
* @see com.stuman.dao.StudentDAO#getSutdentList(com.stuman.util.PageBean)
*/
public PageBean getSutdentList(PageBean page) {
PageBean page2 = this.getObjects(Student.class, page);
return page2;
}
public PageBean getCourseForStu(PageBean page) {
PageBean page2 = this.getObjects(Student.class, page);
return page2;
}
public List getStuForTea(String strQuery) {
ArrayList results = new ArrayList();
String str = strQuery;
ResultSet rs = null;
Statement sql = null;
try {
// 获得Session
Session s = HibernateUtil.currentSession();
// 执行操作
Connection conn = s.connection();
try {
sql = conn.createStatement();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// "select name, mark, score from enrol,classes,course where stu_id
// = '"+stuid+"' and class_id = classes.id and course.id =
// classes.cour_id";
System.out.println(str);
try {
rs = sql.executeQuery(str);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while (rs.next()) {
Student stu = new Student();
//
String id = rs.getString(1), name = rs.getString(2), password = rs
.getString(3), jiguan = rs.getString(4);
String department = rs.getString(5), sex = rs.getString(6);
Integer mark = Integer.valueOf(rs.getInt(7));
String tel = rs.getString(8);
String phone = rs.getString(9), email = rs.getString(10);
stu.setId(id);
stu.setDepartment(department);
stu.setEmail(email);
stu.setJiguan(jiguan);
stu.setMark(mark);
stu.setName(name);
stu.setPassword(password);
stu.setPhone(phone);
stu.setSex(sex);
stu.setTel(tel);
// score.setMark(rs.getString(2));
String mm = "";
if (rs.getString(3) != null) {
mm = rs.getString(3);
}
// score.setScore(mm);
results.add(stu);
}
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (sql != null) {
sql.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
s.flush();
// 关闭Session
HibernateUtil.closeSession();
if (results != null && results.size() > 0) {
return results;
}
} catch (HibernateException e) {
log.fatal(e);
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -