- package com.Exam.Dao;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- import com.Exam.Entity.*;
- import com.Exam.Utill.*;
- public class FindDao {
- Connection conn=null;
- ResultSet rs=null;
- public ClassifyInfo FindClassifyById(int Id){
- conn=DBUtill.getConnection();
- PreparedStatement pstmt = null;
- ClassifyInfo ci=new ClassifyInfo();
- try {
- pstmt=conn.prepareStatement("select * from ClassifyInfo where ClassifyId=?");
- pstmt.setInt(1, Id);
- rs=pstmt.executeQuery();
- if(rs.next()){
- ci.setClassifyId(rs.getInt("ClassifyId"));
- ci.setClassifyName(rs.getString("classifyName"));
- ci.setClassifyRemark(rs.getString("classifyRemark"));
- return ci;
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finally{
- if(conn!=null)
- {
- DBUtill.close(conn);
- }
- }
- return null;
- }
- /**
- * 查询题目类型通过id
- */
- public WriteTypeInfo FindWriteTypById(int Id){
- Connection conne=DBUtill.getConnection();
- PreparedStatement pstmt = null;
- ResultSet rse=null;
- WriteTypeInfo ci=new WriteTypeInfo();
- try {
- pstmt=conne.prepareStatement("select * from WriteTypeInfo where wTypeId=?");
- pstmt.setInt(1, Id);
- rse=pstmt.executeQuery();
- if(rse.next()){
- ci.setwTypeId(rse.getInt("wTypeId"));
- ci.setwTypeName(rse.getString("wTypeName"));
- ci.setwTypeRemark(rse.getString("wTypeRemark"));
- return ci;
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finally{
- if(conne!=null)
- {
- DBUtill.close(conne);
- }
- }
- return null;
- }
- /**
- * 查找试卷最后插入的数据的试卷id
- */
- public int FindPaperId(){
- conn=DBUtill.getConnection();
- PreparedStatement pstmt = null;
- try {
- pstmt=conn.prepareStatement("select top 1 * from TPaperInfo order by tPaperId desc");
- rs=pstmt.executeQuery();
- if(rs.next()){
- //得到id
- return rs.getInt("tpaperId");
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finally{
- if(conn!=null)
- {
- DBUtill.close(conn);
- }
- }
- return 0;
- }
- /**
- * 查询教师通过id
- */
- public TeacherInfo FindTeacherpById(int Id){
- conn=DBUtill.getConnection();
- PreparedStatement pstmt = null;
- TeacherInfo ci=new TeacherInfo();
- try {
- pstmt=conn.prepareStatement("select * from TeacherInfo where teacherId=?");
- pstmt.setInt(1, Id);
- rs=pstmt.executeQuery();
- if(rs.next()){
- ci.setTeacherId(rs.getInt("teacherId"));
- ci.setTeacherName(rs.getString("teacherName"));
- ci.setTeacherRemark(rs.getString("teacherRemark"));
- ci.setTeacherSex(rs.getString("teacherSex"));
- ci.setTeachertCreateTime(rs.getString("teachertCreateTime"));
- return ci;
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finally{
- if(conn!=null)
- {
- DBUtill.close(conn);
- }
- }
- return null;
- }
- /**
- * 查询试卷类型通过id
- */
- public TPaperTypeInfo FindPaperTypeById(int Id){
- Connection conne=DBUtill.getConnection();
- PreparedStatement pstmt = null;
- ResultSet rse=null;
- TPaperTypeInfo ci=new TPaperTypeInfo();
- try {
- pstmt=conne.prepareStatement("select * from tPaperTypeInfo where tpaperTypeId=?");
- pstmt.setInt(1, Id);
- rse=pstmt.executeQuery();
- if(rse.next()){
- ci.settPaperTypeId(rse.getInt("tPaperTypeId"));
- ci.settPaperTypeName(rse.getString("tPaperTypeName"));
- ci.settPaperTypeRemark(rse.getString("tPaperTypeRemark"));
- return ci;
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finally{
- if(conne!=null)
- {
- DBUtill.close(conne);
- }
- }
- return null;
- }
- /**
- * 查找题目通过id
- */
- public WriteInfo FindWriteById(int WriteId){
- Connection conne=DBUtill.getConnection();
- PreparedStatement pstmt = null;
- ResultSet rse=null;
- try {
- pstmt=conne.prepareStatement("select * from WriteInfo where writeId=?");
- pstmt.setInt(1, WriteId);
- rse=pstmt.executeQuery();
- if(rse.next()){
- WriteInfo ci=new WriteInfo();
- ci.settPaperDiffc(rse.getFloat("tPaperDiffc"));
- ci.setWriteId(rse.getInt("writeId"));
- ci.setWriteAnswer(rse.getString("writeAnswer"));
- ci.setwriteContent(rse.getString("writeContent"));
- ci.setwTypeId(FindWriteTypById(rse.getInt("wTypeId")));
- return ci;
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finally{
- if(conne==null)
- {
- DBUtill.close(conne);
- }
- }
- return null;
- }
- /**
- * 查找答案通过id
- */
- public List<AnswerInfo> FindAnswerById(int Id){
- Connection conne=DBUtill.getConnection();
- PreparedStatement pstmt = null;
- ResultSet rse=null;
- List<AnswerInfo> lst=new ArrayList<AnswerInfo>();
- try {
- pstmt=conne.prepareStatement("select * from AnswerInfo where writerId=?");
- pstmt.setInt(1, Id);
- rse=pstmt.executeQuery();
- while(rse.next()){
- AnswerInfo ci=new AnswerInfo();
- ci.setAnswerContent(rse.getString("answerContent"));
- ci.setAnswerId(rse.getInt("answerId"));
- lst.add(ci);
- }
- return lst;
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finally{
- if(conne!=null)
- {
- DBUtill.close(conne);
- }
- }
- return null;
- }
- /**
- * 通过id查询试卷的题目
- *
- */
- public List<PaperQueInfo> FindPaperQueById(int tPaperId){
- Connection conne=DBUtill.getConnection();
- PreparedStatement pstmt = null;
- ResultSet rse=null;
- List<PaperQueInfo> lst=new ArrayList<PaperQueInfo>();
- try {
- pstmt=conne.prepareStatement("select * from PaperQueInfo where tPaperId=?");
- pstmt.setInt(1, tPaperId);
- rse=pstmt.executeQuery();
- while(rse.next()){
- PaperQueInfo ci=new PaperQueInfo();
- ci.setPaperQueId(rse.getInt("paperQueId"));
- ci.setPaperQueRemark(rse.getString("paperQueRemark"));
- ci.setWriteId(FindWriteById(rse.getInt("writeId")));
- lst.add(ci);
- }
- return lst;
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finally{
- if(conne!=null)
- {
- DBUtill.close(conne);
- }
- }
- return null;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/0304201512099.html
来源: http://www.codesnippet.cn/detail/0304201512099.html