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

📄 examdaoimpl.java

📁 出试卷系统(包括前台教师出卷系统
💻 JAVA
字号:
package daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import util.JdbcUtil;

import dao.ExamDAO;
import dao.QuestionDAO;
import dao.ThemeDAO;
import entity.Course;
import entity.Exam;
import entity.Question;
import entity.Teacher;
import entity.Theme;

public class ExamDAOImpl  extends HibernateDaoSupport   implements ExamDAO {
	private ThemeDAO tdao=new ThemeDAOImpl();
	private QuestionDAO qdao=new QuestionDAOImpl();
	public void insert(Exam exam) {
		// TODO Auto-generated method stub
	this.getHibernateTemplate().saveOrUpdate(exam);
	}

	public Exam selectByCourseidTeacheridPaperid(Long courseid,Long teacherid,Long Paperid){
		String hqlName = "from Exam e where e.courseid=:courseid and e.teacherid=:teacherid ";
		String[] argNames = new String[] { "courseid", "teacherid", };
		Long[] argValues = new Long[] { courseid, teacherid };
		Collection<Exam> e =this.getHibernateTemplate()
				.findByNamedParam(hqlName, argNames, argValues);
		
		Exam  ex=new Exam();
		for(Exam exam:e){
			if(exam.getPaperid().equals(Paperid)){
				ex=exam;
			}
		}
		System.out.println("******selectByCourseidTeacheridPaperid**88"+ex.getId());
		return ex;
	}

	public Collection<Exam> selectByTeacherid(Long teacherid) {
		Collection<Exam> instance =getHibernateTemplate()
		.find(
				"from entity.Exam e where e.teacherid =?",
				teacherid);
		return instance;
	}

	public void delete(Exam exam) {
		
		this.getHibernateTemplate().delete(exam);
		
	}
	public Exam selectByid(Long id) {
		Exam instance = (Exam) getHibernateTemplate()
		.find(
				"from entity.Exam e where e.id =?",
				id).get(0);
		return instance;
	}

	public Exam selectExamByTeacherAndCourseAndPaper(Long teacherid,
			Long courseid, Long paperid) {
		Exam exam=new Exam();
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select id from t_exam e where e.teacherid=? and e.courseid=? and e.paperid=?";
		try {
			con = JdbcUtil.getConnection();
			ps = con.prepareStatement(sql);
			ps.setLong(1, teacherid);
			ps.setLong(2, courseid);
			ps.setLong(3, paperid);
			rs = ps.executeQuery();
			if (rs.next()) {
				exam.setId(rs.getLong(1));
			}
			List<Theme> themes=selectThemeByExamId(exam.getId());
			themes=selectThemeByExamIdAndThemeId(exam.getId(),themes);
			exam.setThemes(themes);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JdbcUtil.release(rs, ps, con);
		}
		return exam;
	}
	public List<Theme> selectThemeByExamId(Long examid){
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Theme> themes=new ArrayList<Theme>();
		String sql = "select distinct(themeid) from t_exam_theme_question where examid=? order by themeid";
		try {
			con = JdbcUtil.getConnection();
			ps = con.prepareStatement(sql);
			ps.setLong(1, examid);
			rs = ps.executeQuery();
			while(rs.next()){
				Theme theme=tdao.selectById(rs.getInt(1));
				themes.add(theme);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JdbcUtil.release(rs, ps, con);
		}	
		return themes;		
	}
	public List<Theme> selectThemeByExamIdAndThemeId(Long examid,List<Theme> themes){
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select questionid from t_exam_theme_question where examid=? and themeid=?";
		try {
			con = JdbcUtil.getConnection();
			for(Theme theme:themes){
				ps = con.prepareStatement(sql);
				ps.setLong(1, examid);
				ps.setLong(2, theme.getId());
				rs = ps.executeQuery();
				while(rs.next()){
					Question question=qdao.selectById(rs.getInt(1));
					theme.getQuestion().add(question);
				}
			}	
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JdbcUtil.release(rs, ps, con);
		}
		return themes;		
	}

}

⌨️ 快捷键说明

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