⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 coursedaoimp.java

📁 学生成绩管理系统
💻 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.CourseDAO;
import com.stuman.dao.hibernate.HibernateUtil;
import com.stuman.domain.Course;
import com.stuman.domain.CourseForStu;
 
import com.stuman.util.BaseDao;
import com.stuman.util.PageBean;

public class CourseDAOImp extends BaseDao implements CourseDAO {

	private static Log log = LogFactory.getLog(CourseDAOImp.class);

	public List getCourse() {
		try {
			Session s = HibernateUtil.currentSession();
			HibernateUtil.beginTransaction();
			List results = s.createQuery("from Course cour").list();
			HibernateUtil.commitTransaction();
			HibernateUtil.closeSession();
			if (results != null && results.size() > 0) {
				return results;
			}
		} catch (HibernateException e) {
			log.fatal(e);
		}
		return null;
	}

	public boolean deleteCourseByID(String id) {
		try {
			Session s = HibernateUtil.currentSession();
			HibernateUtil.beginTransaction();
			Course cour = (Course) s.load(Course.class, id);
			System.out.println(cour.getId());
			s.delete(cour);
			HibernateUtil.commitTransaction();
			s.flush();
			HibernateUtil.closeSession();
			return true;
		} catch (HibernateException e) {
			log.fatal(e);
		}
		return false;
	}

	public Course getCourseByID(String id) {
		try {
			Session s = HibernateUtil.currentSession();
			HibernateUtil.beginTransaction();
			Course cour = (Course) s.load(Course.class, id);
			HibernateUtil.commitTransaction();
		 
			HibernateUtil.closeSession();
			return cour;
		} catch (HibernateException e) {
			log.fatal(e);
		}
		return null;
	}

	public boolean updateCourse(Course cour) {
		try {
			Session s = HibernateUtil.currentSession();
			HibernateUtil.beginTransaction();
			s.update(cour);
			System.out.println("update Course id =" + cour.getId());
			HibernateUtil.commitTransaction();
			 s.flush();
			HibernateUtil.closeSession();
			return true;
		} catch (HibernateException e) {
			log.fatal(e);
		}
		return false;
	}

	public boolean saveCourse(Course cour) {
		try {
			Session s = HibernateUtil.currentSession();
			HibernateUtil.beginTransaction();
			s.saveOrUpdate(cour);
			System.out.println("save Course id =" + cour.getId());
			HibernateUtil.commitTransaction();
			 s.flush();
			HibernateUtil.closeSession();
			return true;
		} catch (HibernateException e) {
			log.fatal(e);
		}
		return false;
	}

	public List getCourseForStu(String stuid) throws SQLException {
		// TODO Auto-generated method stub
	 	ArrayList results = new ArrayList();
		try {
			Session s = HibernateUtil.currentSession();
			System.out.println("getCourseForStu=="+stuid);
	 
			Connection conn = s.connection();
			Statement sql = conn.createStatement();
			
			
			StringBuffer buf=new StringBuffer();
			buf.append("select course.id, course.name, course.prepare,course.dep, classes.id as classid, classes.room_id, cour_time, teacher.name  ");
			buf.append(" from course,classes,teacher ");
			buf.append(" where teacher.id = classes.tea_id and  course.id=classes.cour_id and 	((prepare = '0' or prepare in ( select course.id from enrol,classes,course where enrol.class_id = classes.id  and classes.cour_id = course.id and enrol.stu_id=' ").append(stuid).append("' and score > '60') )");
			buf.append(" and course.id not in (select classes.cour_id from classes,enrol where stu_id='").append(stuid).append("' and class_id = classes.id   ))");//and accept = '1'
//			ResultSet rs = sql.executeQuery("select course.id, course.name, course.prepare, " +
//					"course.dep, classes.id as classid, classes.room_id, cour_time, teacher.name " +
//					"from course,classes,teacher  where teacher.id = classes.tea_id and " +
//					" course.id=classes.cour_id and 	((prepare = '0' or prepare in ( " +
//					" select course.id from enrol,classes,course where enrol.class_id = classes.id " +
//					" and classes.cour_id = course.id and enrol.stu_id='"+stuid+"' and score > '60') ) " +
//					"and course.id not in (select classes.cour_id from classes,enrol where stu_id='"+stuid+" " +
//					" ");
			
			System.out.println(buf.toString());
			ResultSet rs = sql.executeQuery(buf.toString());
			while (rs.next()) {
				CourseForStu courForStu = new CourseForStu();
				courForStu.setId(rs.getString(1));
				courForStu.setName(rs.getString(2));
				courForStu.setPrepare(rs.getString(3));
				courForStu.setDep(rs.getString(4));
				courForStu.setClassid(rs.getString(5));
				courForStu.setRoomid(rs.getString(6));
				courForStu.setTime(rs.getString(7));
				courForStu.setTeacher(rs.getString(8));
				results.add(courForStu);
			}
			rs.close();
			sql.close();
			
			HibernateUtil.closeSession();
			if (results != null && results.size() > 0) {
				return results;
			}
		} catch (HibernateException e) {
			log.fatal(e);
		}
		return null;
	}
	public PageBean getCourseList(PageBean page){
		PageBean page2= this.getObjects(Course.class, page);
		 
		return page2;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -