项目第二阶段
部分代码:
目录结构:
1.dao层接口:
package org.exam.dao; import java.util.List; import org.exam.databean.choicequestion; import org.exam.databean.fillquestion; import org.exam.databean.student; import org.exam.databean.stuscore; import org.exam.databean.teacher; import org.exam.databean.test; import org.exam.databean.testpaper; public interface TestDAO { public int save(test t); public String studentLogin(String account,String password); //学生登陆 public String teacherLogin(String account,String password); //教师登陆 public String adminLogin(String account,String password); //管理员登陆 public int savestudent(student s); //学生注册 public int saveteacher(teacher t); //教师注册 public int updatespwd(String account,String password); //修改学生密码 public int updatetpwd(String account,String password); //修改教师密码 public int updateapwd(String account,String password); //修改管理员面膜 public int savescore(String sid,int score); //保存学生的成绩 public String searchscore(String sid); //教师查询学生的成绩 public int updatestudent(student s); //修改学生信息 public int updateteacher(teacher t); //修改教师信息 public String searchcardnumber(String account, String password); //查询身份证 public String searchStime(); //查询考试开始时间 public String searchTime(); //查询考试时长 public List<choicequestion> findchoiceques(); //查询考卷选择题的信息 public List<fillquestion> findfillques(); //查询考卷填空题的信息 public List<testpaper> findanswer(); //查询考卷试题的答案 public int searchallpage(String type,String keyword); //查询选择/填空总页数 public int savechoiceq(choicequestion c); // 保存选择考试试题 public int savefillq(fillquestion f); //保存填空考试试题 public int searchallfill(String keyword); //搜索总的填空题数 public int searchallchoice(String keyword); //搜索总的选择题数 public int searchnowtest(); //查询当前试卷的题数 public int savefill(List a); //保存填空题 public int savechoice(List a); //保存选择题 public int deletefill(int id); //选择填空题 public int deletechoice(int id); //删除选择题 public int updatefill(fillquestion f); //修改填空题 public int updatechoice(choicequestion c); //修改选择题 public List findtitle(int page,String type,String keyword);//分页查询所有选择/填空试题 public int deletetestpaper(); //删除试卷 public int savetime(String stime,String time); //保存时间 public int deletetime(); //删除时间表 public List<stuscore> findscore(); //查询所有学生的成绩 public int existtime();//判断考试时间表是否存在 public int existscore();//判断学生成绩表是否存在 public int deletescore();//删除成绩表 public List<teacher> searchteacher(String keyword, String select,int page); //查询教师信息 public List<student> searchstudent(String keyword, String select,int page); //查询学生信息 public int findtea(String keyword,String select); //查询教师信息的总页数 public int findstu(String keyword,String select); //查询学生信息的总页数 public int searchteaallpage(String keyword,String select); //查询教师信息的总记录数 public int searchstuallpage(String keyword,String select);//查询学生信息的总记录数 public int deletestudent(String id); //删除学生表 public int deleteteacher(String id); //删除教师表 }
2.dao层接口实现类
package org.exam.dao.impl; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.exam.dao.TestDAO; import org.exam.databean.choicequestion; import org.exam.databean.fillquestion; import org.exam.databean.student; import org.exam.databean.stuscore; import org.exam.databean.teacher; import org.exam.databean.test; import org.exam.databean.testpaper; import org.exam.util.DbUtil; public class testdaoimpl implements TestDAO{ public int save(test t) { String sql="INSERT INTO test(name,password)values(?,?)"; PreparedStatement ps = DbUtil.executePreparedStatement(sql); int result = 0 ; try { ps.setString(1, t.getName()); ps.setString(2, t.getPassword()); result = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } DbUtil.close(); return result; } @Override public String studentLogin(String account, String password) { // TODO Auto-generated method stub String sql="select sname from student where sid='"+account+"' and pwd='"+password+"'"; ResultSet rs = DbUtil.executeQuery(sql); String result= "null"; try { if(rs.next()) { result=rs.getString(1); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DbUtil.close(); return result; } @Override public String teacherLogin(String account, String password) { // TODO Auto-generated method stub String sql="select tname from teacher where tid='"+account+"' and pwd='"+password+"'"; ResultSet rs = DbUtil.executeQuery(sql); String result="null"; try { if(rs.next()) { result=rs.getString(1); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DbUtil.close(); return result; } @Override public String adminLogin(String account, String password) { // TODO Auto-generated method stub String sql="select aname from administrator where adid='"+account+"' and pwd='"+password+"'"; ResultSet rs = DbUtil.executeQuery(sql); String result="null"; try { if(rs.next()) { result=rs.getString(1); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DbUtil.close(); return result; } public int savestudent(student s) { // TODO Auto-generated method stub String sql="INSERT INTO student(sid,sname,sex,cardnumber,pwd,department,phone)values(?,?,?,?,?,?,?)"; PreparedStatement ps = DbUtil.executePreparedStatement(sql); int result = 0 ; try { ps.setString(1, s.getSid() ); ps.setString(2, s.getSname()); ps.setString(3, s.getSex()); ps.setString(4, s.getCardnumber()); ps.setString(5, s.getPwd()); ps.setString(6, s.getDepartment()); ps.setString(7, s.getPhone()); result = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } DbUtil.close(); return result; } public int saveteacher(teacher t) { // TODO Auto-generated method stub String sql="INSERT INTO teacher(tid,tname,sex,cardnumber,pwd,title,phone)values(?,?,?,?,?,?,?)"; PreparedStatement ps = DbUtil.executePreparedStatement(sql); int result = 0 ; try { ps.setString(1, t.getTid()); ps.setString(2, t.getTname()); ps.setString(3, t.getSex()); ps.setString(4, t.getCardnumber()); ps.setString(5, t.getPwd()); ps.setString(6, t.getTitle()); ps.setString(7, t.getPhone()); result = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } DbUtil.close(); return result; } @Override public int updatespwd(String account, String password) { // TODO Auto-generated method stub String sql="update student set pwd='"+password+"' where sid='"+account+"'"; PreparedStatement ps = DbUtil.executePreparedStatement(sql); int result = 0 ; try { result = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } DbUtil.close(); return result; } @Override public int updatetpwd(String account, String password) { // TODO Auto-generated method stub String sql="update teacher set pwd='"+password+"' where tid='"+account+"'"; PreparedStatement ps = DbUtil.executePreparedStatement(sql); int result = 0 ; try { result = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } DbUtil.close(); return result; } @Override public int updateapwd(String account, String password) { // TODO Auto-generated method stub String sql="update administrator set pwd='"+password+"' where adid='"+account+"'"; PreparedStatement ps = DbUtil.executePreparedStatement(sql); int result = 0 ; try { result = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } DbUtil.close(); return result; } @Override public int savescore(String sid, int score) { // TODO Auto-generated method stub String sql="INSERT INTO stuscore(sid,score)values(?,?)"; PreparedStatement ps = DbUtil.executePreparedStatement(sql); int result = 0 ; try { ps.setString(1,sid); ps.setInt(2,score); result = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } DbUtil.close(); return result; } @Override public String searchscore(String sid) { // TODO Auto-generated method stub String sql="select score from stuscore where sid='"+sid+"'"; ResultSet rs = DbUtil.executeQuery(sql); String result="null"; try { if(rs.next()) { result=rs.getString(1); } } catch (SQLException e) { e.printStackTrace(); } DbUtil.close(); return result; } @Override public int updatestudent(student s) { // TODO Auto-generated method stub String sql = "UPDATE student SET sname=?, sex=?," + "cardnumber=?,pwd=?,department=?,phone=?" + " WHERE sid=?"; PreparedStatement ps = DbUtil.executePreparedStatement(sql); int result = 0 ; try { ps.setString(1,s.getSname() ); ps.setString(2, s.getSex()); ps.setString(3, s.getCardnumber()); ps.setString(4, s.getPwd()); ps.setString(5,s.getDepartment()); ps.setString(6, s.getPhone()); ps.setString(7,s.getSid()); result = ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DbUtil.close(); return result; } @Override public int updateteacher(teacher t) { // TODO Auto-generated method stub String sql = "UPDATE teacher SET tname=?, sex=?," + "cardnumber=?,pwd=?,title=?,phone=?" + " WHERE tid=?"; PreparedStatement ps = DbUtil.executePreparedStatement(sql); int result = 0 ; try { ps.setString(1,t.getTname()); ps.setString(2,t.getSex()); ps.setString(3,t.getCardnumber()); ps.setString(4,t.getPwd()); ps.setString(5,t.getTitle()); ps.setString(6,t.getPhone()); ps.setString(7,t.getTid()); result = ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DbUtil.close(); return result; } @Override public String searchcardnumber(String account, String password) { // TODO Auto-generated method stub String sql="select cardnumber from student where sid='"+account+"' and pwd='"+password+"'"; ResultSet rs = DbUtil.executeQuery(sql); String result="null"; try { if(rs.next()) { result=rs.getString(1); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DbUtil.close(); return result; } @Override public String searchStime() { // TODO Auto-generated method stub String sql="select stime from testtime"; ResultSet rs = DbUtil.executeQuery(sql); String result="null"; try { if(rs.next()) { result=rs.getString(1); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DbUtil.close(); return result; } @Override public String searchTime() { // TODO Auto-generated method stub String sql="select time from testtime"; ResultSet rs = DbUtil.executeQuery(sql); String result="null"; try { if(rs.next()) { result=rs.getString(1); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DbUtil.close(); return result; } @Override public List<choicequestion> findchoiceques() { // TODO Auto-generated method stub String type="0"; String sql = "SELECT * FROM testpaper where type='"+type+"'ORDER BY nowid"; List<testpaper> list = new ArrayList<testpaper>(); ResultSet rs = DbUtil.executeQuery(sql); try { while(rs.next()) { testpaper tp=new testpaper(); tp.setPreid(rs.getInt("preid")); list.add(tp); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DbUtil.close(); List<choicequestion> list1=new ArrayList<choicequestion>(); for(int i=0;i<list.size();i++) { int preid=list.get(i).getPreid(); String sql1="select * from choicequestion where c_id='"+preid+"'"; ResultSet rs1 = DbUtil.executeQuery(sql1); try { while(rs1.next()) { choicequestion cq=new choicequestion(); cq.setC_question(rs1.getString("c_question")); cq.setC_choiceA(rs1.getString("c_choiceA")); cq.setC_choiceB(rs1.getString("c_choiceB")); cq.setC_choiceC(rs1.getString("c_choiceC")); cq.setC_choiceD(rs1.getString("c_choiceD")); cq.setC_answer(rs1.getString("c_answer")); list1.add(cq); } }catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DbUtil.close(); } return list1; } @Override public List<fillquestion> findfillques() { // TODO Auto-generated method stub String type="1"; String sql = "SELECT * FROM testpaper where type='"+type+"'ORDER BY nowid"; List list = new ArrayList(); ResultSet rs = DbUtil.executeQuery(sql); try { testpaper tp=new testpaper(); while(rs.next()) { //testpaper tp=new testpaper(); //tp.setPreid(rs.getInt("preid")); list.add(Integer.valueOf(rs.getInt("preid"))); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DbUtil.close(); List<fillquestion> list1=new ArrayList<fillquestion>(); for(int i=0;i<list.size();i++) { int preid=(int) list.get(i); String sql1="select * from fillquestion where f_id='"+preid+"'"; ResultSet rs1 = DbUtil.executeQuery(sql1); try { while(rs1.next()) { fillquestion fq=new fillquestion(); fq.setF_question(rs1.getString("f_question")); fq.setF_answer(rs1.getString("f_answer")); list1.add(fq); } }catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DbUtil.close(); } return list1; } @Override public List<testpaper> findanswer() { // TODO Auto-generated method stub String sql = "SELECT * FROM testpaper ORDER BY nowid "; List<testpaper> list = new ArrayList<testpaper>(); ResultSet rs = DbUtil.executeQuery(sql); try { while(rs.next()) { stuscore sscore=new stuscore(); testpaper tp=new testpaper(); tp.setAnswer(rs.getString("answer")); list.add(tp); } } catch (SQLException e) { e.printStackTrace(); } DbUtil.close(); return list; } @Override public int searchallpage(String type,String keyword) { // TODO Auto-generated method stub int length=0; String sql=null; if(type.equals("0")) { if(keyword.equals("")||keyword==null) sql="select * from choicequestion"; else sql="select * from choicequestion where c_question like '%"+keyword+"%'"; ResultSet rs= DbUtil.executeQuery(sql); List<choicequestion> list = new ArrayList<choicequestion>(); try { while(rs.next()) { choicequestion cq=new choicequestion(); cq.setC_question(rs.getString("c_question")); cq.setC_choiceA(rs.getString("c_choiceA")); cq.setC_choiceB(rs.getString("c_choiceB")); cq.setC_choiceC(rs.getString("c_choiceC")); cq.setC_choiceD(rs.getString("c_choiceD")); list.add(cq); } } catch (SQLException e) { e.printStackTrace(); } length=list.size(); DbUtil.close(); return length/10+1; } else { if(keyword.equals("")||keyword==null) sql="select * from fillquestion"; else sql="select * from fillquestion where f_question like '%"+keyword+"%'"; ResultSet rs= DbUtil.executeQuery(sql); List<fillquestion> list = new ArrayList<fillquestion>(); try { while(rs.next()) { fillquestion fq=new fillquestion(); fq.setF_question(rs.getString("f_question")); list.add(fq); } } catch (SQLException e) { e.printStackTrace(); } length=list.size(); DbUtil.close(); return length/10+1; } } @Override public int savechoiceq(choicequestion c) { // TODO Auto-generated method stub String sql="INSERT INTO choicequestion(c_question,c_choiceA,c_choiceB,c_choiceC,c_choiceD,c_answer)values(?,?,?,?,?,?)"; PreparedStatement ps = DbUtil.executePreparedStatement(sql); int result = 0 ; try { ps.setString(1, c.getC_question() ); ps.setString(2, c.getC_choiceA()); ps.setString(3, c.getC_choiceB()); ps.setString(4, c.getC_choiceC()); ps.setString(5, c.getC_choiceD()); ps.setString(6, c.getC_answer()); result = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } DbUtil.close(); return result; } @Override public int savefillq(fillquestion f) { // TODO Auto-generated method stub String sql="INSERT INTO fillquestion(f_question,f_answer)values(?,?)"; PreparedStatement ps = DbUtil.executePreparedStatement(sql); int result = 0 ; try { ps.setString(1, f.getF_question()); ps.setString(2, f.getF_answer()); result = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } DbUtil.close(); return result; } @Override public int searchallchoice(String keyword) { // TODO Auto-generated method stub String sql=null; if(keyword.equals("")||keyword==null) sql="select count(*)totalCount from choicequestion"; else sql="select count(*)totalCount from choicequestion where c_question like '%"+keyword+"%'"; ResultSet rs= DbUtil.executeQuery(sql); int allchoice=0; try { if(rs.next()) { allchoice=rs.getInt("totalCount"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return allchoice; } @Override public int searchallfill(String keyword) { // TODO Auto-generated method stub String sql=null; if(keyword.equals("")||keyword==null) sql="select count(*)totalCount from fillquestion"; else sql="select count(*)totalCount from fillquestion where f_question like '%"+keyword+"%'"; ResultSet rs= DbUtil.executeQuery(sql); int allfill=0; try { if(rs.next()) { allfill=rs.getInt("totalCount"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return allfill; } @Override public int savechoice(List a) { // TODO Auto-generated method stub int result1 = 0 ; String sql="select * from choicequestion"; ResultSet rs = DbUtil.executeQuery(sql); List<choicequestion> list = new ArrayList<choicequestion>(); String result=null; //答案 int id=0; //原题id try { while(rs.next()) { choicequestion cq=new choicequestion(); cq.setC_id(rs.getInt("c_id")); cq.setC_answer(rs.getString("c_answer")); list.add(cq); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DbUtil.close(); for(int i=0;i<a.size();i++) { int alltitle = searchnowtest();//获取当前试卷的题数 //查找原题库的对应选择题 //System.out.println(list.size()); id=list.get((int) a.get(i)-1).getC_id(); result=list.get((int) a.get(i)-1).getC_answer(); String sql1="insert into testpaper(nowid,preid,type,answer) values(?,?,0,?)"; PreparedStatement ps = DbUtil.executePreparedStatement(sql1); try { ps.setInt(1, alltitle+1); ps.setInt(2, id); ps.setString(3,result); result1 = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } DbUtil.close(); return result1; } @Override public int savefill(List a) { // TODO Auto-generated method stub int result1 = 0 ; String sql="select * from fillquestion"; ResultSet rs = DbUtil.executeQuery(sql); List<fillquestion> list = new ArrayList<fillquestion>(); String result=null; //答案 int id=0; //原题id try { while(rs.next()) { fillquestion fq=new fillquestion(); fq.setF_id(rs.getInt("f_id")); fq.setF_answer(rs.getString("f_answer")); list.add(fq); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DbUtil.close(); for(int i=0;i<a.size();i++) { int alltitle = searchnowtest(); //System.out.println(alltitle); id=list.get((int) a.get(i)-1).getF_id(); result=list.get((int) a.get(i)-1).getF_answer(); String sql1="insert into testpaper(nowid,preid,type,answer) values(?,?,1,?)"; PreparedStatement ps = DbUtil.executePreparedStatement(sql1); try { ps.setInt(1, alltitle+1); ps.setInt(2, id); ps.setString(3,result); result1 = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } DbUtil.close(); } return result1; } @Override public int searchnowtest() { // TODO Auto-generated method stub String sql="select count(*)totalCount from testpaper"; ResultSet rs= DbUtil.executeQuery(sql); int alltitle=0; try { if(rs.next()) { alltitle=rs.getInt("totalCount"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DbUtil.close(); return alltitle; } @Override public int deletechoice(int id) { // TODO Auto-generated method stub String sql="delete from choicequestion where c_id='"+id+"'"; int result = 0 ; result = DbUtil.executeUpdate(sql); DbUtil.close(); return result; } @Override public int deletefill(int id) { // TODO Auto-generated method stub String sql="delete from fillquestion where f_id='"+id+"'"; int result=0; result=DbUtil.executeUpdate(sql); DbUtil.close(); return result; } @Override public int updatechoice(choicequestion c) { // TODO Auto-generated method stub String sql="update choicequestion set c_question=?,c_choiceA=?," +"c_choiceB=?,c_choiceC=?,c_choiceD=?,c_answer=? where c_id=?"; PreparedStatement ps=DbUtil.executePreparedStatement(sql); int result=0; try { ps.setString(1, c.getC_question()); ps.setString(2, c.getC_choiceA()); ps.setString(3, c.getC_choiceB()); ps.setString(4, c.getC_choiceC()); ps.setString(5, c.getC_choiceD()); ps.setString(6, c.getC_answer()); ps.setInt(7, c.getC_id()); result = ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DbUtil.close(); return result; } @Override public int updatefill(fillquestion f) { // TODO Auto-generated method stub String sql="update fillquestion set f_question=?,f_answer=? where f_id=?"; PreparedStatement ps=DbUtil.executePreparedStatement(sql); int result=0; try { ps.setString(1, f.getF_question()); ps.setString(2, f.getF_answer()); ps.setInt(3, f.getF_id()); result = ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DbUtil.close(); return result; } @Override public List findtitle(int page,String type,String keyword) { // TODO Auto-generated method stub int spage=(page-1)*10; //当前页开始的记录数 String sql=null; if(type.equals("0")) { if(keyword.equals("")||keyword==null) sql="select * from choicequestion limit "+spage+",10"; else sql="select * from choicequestion where c_question like '%"+keyword+"%' limit "+spage+",10"; List<choicequestion> list = new ArrayList<choicequestion>(); ResultSet rs = DbUtil.executeQuery(sql); try { while(rs.next()) { choicequestion cq=new choicequestion(); cq.setC_id(rs.getInt("c_id")); cq.setC_question(rs.getString("c_question")); cq.setC_choiceA(rs.getString("c_choiceA")); cq.setC_choiceB(rs.getString("c_choiceB")); cq.setC_choiceC(rs.getString("c_choiceC")); cq.setC_choiceD(rs.getString("c_choiceD")); cq.setC_answer(rs.getString("c_answer")); list.add(cq); } } catch (SQLException e) { e.printStackTrace(); } DbUtil.close(); return list; } else { if(keyword.equals("")||keyword==null) sql="select * from fillquestion limit "+spage+",10"; else sql="select * from fillquestion where f_question like '%"+keyword+"%' limit "+spage+",10"; List<fillquestion> list = new ArrayList<fillquestion>(); ResultSet rs = DbUtil.executeQuery(sql); try { while(rs.next()) { fillquestion fq=new fillquestion(); fq.setF_id(rs.getInt("f_id")); fq.setF_question(rs.getString("f_question")); fq.setF_answer(rs.getString("f_answer")); list.add(fq); } } catch (SQLException e) { e.printStackTrace(); } DbUtil.close(); return list; } } @Override public int deletetestpaper() { // TODO Auto-generated method stub String sql="delete from testpaper"; int result=0; result=DbUtil.executeUpdate(sql); DbUtil.close(); return result; } @Override public int savetime(String stime,String time) { // TODO Auto-generated method stub String sql="insert into testtime(stime,time) values(?,?)"; PreparedStatement ps = DbUtil.executePreparedStatement(sql); int result = 0 ; try { ps.setString(1, stime); ps.setString(2, time); result = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } DbUtil.close(); return result; } @Override public int deletetime() { // TODO Auto-generated method stub String sql="delete from testtime"; int result=0; result=DbUtil.executeUpdate(sql); DbUtil.close(); return result; } @Override public List<stuscore> findscore() { // TODO Auto-generated method stub String sql = "SELECT * FROM stuscore ORDER BY sid "; List<stuscore> list = new ArrayList<stuscore>(); ResultSet rs = DbUtil.executeQuery(sql); try { while(rs.next()) { stuscore sscore=new stuscore(); sscore.setSid(rs.getString("sid")); sscore.setScore(rs.getString("score")); list.add(sscore); } } catch (SQLException e) { e.printStackTrace(); } DbUtil.close(); List<stuscore> list1 =new ArrayList<stuscore>(); for(int i=0;i<list.size();i++) { String sql1="select sname from student where sid='"+list.get(i).getSid()+"'"; ResultSet rs1 = DbUtil.executeQuery(sql1); try { if(rs1.next()) { stuscore score=new stuscore(); score.setSid(list.get(i).getSid()); score.setSname(rs1.getString(1)); score.setScore(list.get(i).getScore()); list1.add(score); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } DbUtil.close(); return list1; } @Override public int existtime() { // TODO Auto-generated method stub String sql="select count(*)totalCount from testtime"; ResultSet rs= DbUtil.executeQuery(sql); int time=0; try { if(rs.next()) { time=rs.getInt("totalCount"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DbUtil.close(); return time; } @Override public int existscore() { // TODO Auto-generated method stub String sql="select count(*)totalCount from stuscore"; ResultSet rs= DbUtil.executeQuery(sql); int score=0; try { if(rs.next()) { score=rs.getInt("totalCount"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DbUtil.close(); return score; } @Override public int deletescore() { // TODO Auto-generated method stub String sql="delete from stuscore"; int result=0; result=DbUtil.executeUpdate(sql); DbUtil.close(); return result; } @Override public List<student> searchstudent(String keyword, String select,int page) { // TODO Auto-generated method stub int spage=(page-1)*10; String sql=null; List<student> list = new ArrayList<student>(); if(keyword==null || keyword == "") { sql="select * from student limit "+spage+",10"; ResultSet rs = DbUtil.executeQuery(sql); try { while(rs.next()) { student s=new student(); s.setSid(rs.getString("sid")); s.setSname(rs.getString("sname")); s.setSex(rs.getString("sex")); s.setCardnumber(rs.getString("cardnumber")); s.setPwd(rs.getString("pwd")); s.setDepartment(rs.getString("department")); s.setPhone(rs.getString("phone")); list.add(s); } } catch (SQLException e) { e.printStackTrace(); } DbUtil.close(); return list; } else { if(select.equals("id")) sql="select * from student where sid='"+keyword+"' limit "+spage+",10"; else sql="select * from student where sname='"+keyword+"' limit "+spage+",10"; ResultSet rs = DbUtil.executeQuery(sql); try { while(rs.next()) { student s=new student(); s.setSid(rs.getString("sid")); s.setSname(rs.getString("sname")); s.setSex(rs.getString("sex")); s.setCardnumber(rs.getString("cardnumber")); s.setPwd(rs.getString("pwd")); s.setDepartment(rs.getString("department")); s.setPhone(rs.getString("phone")); list.add(s); } } catch (SQLException e) { e.printStackTrace(); } DbUtil.close(); return list; } } @Override public List<teacher> searchteacher(String keyword, String select,int page) { // TODO Auto-generated method stub int spage=(page-1)*10; String sql=null; List<teacher> list = new ArrayList<teacher>(); if(keyword==null || keyword == "") { sql="select * from teacher limit "+spage+",10"; ResultSet rs = DbUtil.executeQuery(sql); try { while(rs.next()) { teacher t=new teacher(); t.setTid(rs.getString("tid")); t.setTname(rs.getString("tname")); t.setSex(rs.getString("sex")); t.setCardnumber(rs.getString("cardnumber")); t.setPwd(rs.getString("pwd")); t.setTitle(rs.getString("title")); t.setPhone(rs.getString("phone")); list.add(t); } } catch (SQLException e) { e.printStackTrace(); } DbUtil.close(); return list; } else { if(select.equals("id")) sql="select * from teacher where tid='"+keyword+"' limit "+spage+",10"; else sql="select * from teacher where tname='"+keyword+"' limit "+spage+",10"; ResultSet rs = DbUtil.executeQuery(sql); try { while(rs.next()) { teacher t=new teacher(); t.setTid(rs.getString("tid")); t.setTname(rs.getString("tname")); t.setSex(rs.getString("sex")); t.setCardnumber(rs.getString("cardnumber")); t.setPwd(rs.getString("pwd")); t.setTitle(rs.getString("title")); t.setPhone(rs.getString("phone")); list.add(t); } } catch (SQLException e) { e.printStackTrace(); } DbUtil.close(); return list; } } @Override public int searchstuallpage(String keyword,String select) { // TODO Auto-generated method stub String sql=null; int allstu=0; if(keyword==null || keyword == "") sql="select count(*)totalCount from student"; else { if(select.equals("id")) sql="select count(*)totalCount from student where sid='"+keyword+"'"; else sql="select count(*)totalCount from student where sname='"+keyword+"'"; } ResultSet rs= DbUtil.executeQuery(sql); try { if(rs.next()) { allstu=rs.getInt("totalCount"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DbUtil.close(); return allstu/10+1; } @Override public int searchteaallpage(String keyword,String select) { // TODO Auto-generated method stub String sql=null; int alltea=0; if(keyword==null || keyword == "") sql="select count(*)totalCount from teacher"; else { if(select.equals("id")) sql="select count(*)totalCount from teacher where tid='"+keyword+"'"; else sql="select count(*)totalCount from teacher where tname='"+keyword+"'"; } ResultSet rs= DbUtil.executeQuery(sql); try { if(rs.next()) { alltea=rs.getInt("totalCount"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DbUtil.close(); return alltea/10+1; } @Override public int findstu(String keyword,String select) { // TODO Auto-generated method stub String sql=null; int allstu=0; if(keyword==null || keyword == "") sql="select count(*)totalCount from student"; else { if(select.equals("id")) sql="select count(*)totalCount from student where sid='"+keyword+"'"; else sql="select count(*)totalCount from student where sname='"+keyword+"'"; } ResultSet rs= DbUtil.executeQuery(sql); try { if(rs.next()) { allstu=rs.getInt("totalCount"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DbUtil.close(); return allstu; } @Override public int findtea(String keyword,String select) { // TODO Auto-generated method stub String sql=null; int alltea=0; if(keyword==null || keyword == "") sql="select count(*)totalCount from teacher"; else { if(select.equals("id")) sql="select count(*)totalCount from teacher where tid='"+keyword+"'"; else sql="select count(*)totalCount from teacher where tname='"+keyword+"'"; } ResultSet rs= DbUtil.executeQuery(sql); try { if(rs.next()) { alltea=rs.getInt("totalCount"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DbUtil.close(); return alltea; } @Override public int deletestudent(String id) { // TODO Auto-generated method stub String sql="delete from student where sid='"+id+"'"; int result=0; result=DbUtil.executeUpdate(sql); DbUtil.close(); return result; } @Override public int deleteteacher(String id) { // TODO Auto-generated method stub String sql="delete from teacher where tid='"+id+"'"; int result=0; result=DbUtil.executeUpdate(sql); DbUtil.close(); return result; } }
3.服务于dao的简单工厂
package org.exam.dao.factory; import org.exam.dao.TestDAO; import org.exam.dao.impl.testdaoimpl; public class DaoFactory { public static TestDAO gettestdao() { return new testdaoimpl(); } }
4.数据bean(实体类)
package org.exam.databean; public class adminstrator { private String adid; private String aname; private String sex; private String cardnumber; private String pwd; private String phone; public String getAdid() { return adid; } public void setAdid(String adid) { this.adid = adid; } public String getAname() { return aname; } public void setAname(String aname) { this.aname = aname; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getCardnumber() { return cardnumber; } public void setCardnumber(String cardnumber) { this.cardnumber = cardnumber; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
package org.exam.databean; public class choicequestion { private int c_id; private String c_question; private String c_choiceA; private String c_choiceB; private String c_choiceC; private String c_choiceD; private String c_answer; public int getC_id() { return c_id; } public void setC_id(int c_id) { this.c_id = c_id; } public String getC_question() { return c_question; } public void setC_question(String c_question) { this.c_question = c_question; } public String getC_choiceA() { return c_choiceA; } public void setC_choiceA(String c_choiceA) { this.c_choiceA = c_choiceA; } public String getC_choiceB() { return c_choiceB; } public void setC_choiceB(String c_choiceB) { this.c_choiceB = c_choiceB; } public String getC_choiceC() { return c_choiceC; } public void setC_choiceC(String c_choiceC) { this.c_choiceC = c_choiceC; } public String getC_choiceD() { return c_choiceD; } public void setC_choiceD(String c_choiceD) { this.c_choiceD = c_choiceD; } public String getC_answer() { return c_answer; } public void setC_answer(String c_answer) { this.c_answer = c_answer; } }
package org.exam.databean; public class fillquestion { private int f_id; private String f_question; private String f_answer; public int getF_id() { return f_id; } public void setF_id(int f_id) { this.f_id = f_id; } public String getF_question() { return f_question; } public void setF_question(String f_question) { this.f_question = f_question; } public String getF_answer() { return f_answer; } public void setF_answer(String f_answer) { this.f_answer = f_answer; } }
package org.exam.databean; public class student { private String sid; private String sname; private String sex; private String cardnumber; private String pwd; private String department; private String phone; public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getCardnumber() { return cardnumber; } public void setCardnumber(String cardnumber) { this.cardnumber = cardnumber; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
package org.exam.databean; public class teacher { private String tid; private String tname; private String sex; private String cardnumber; private String pwd; private String title; private String phone; public String getTid() { return tid; } public void setTid(String tid) { this.tid = tid; } public String getTname() { return tname; } public void setTname(String tname) { this.tname = tname; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getCardnumber() { return cardnumber; } public void setCardnumber(String cardnumber) { this.cardnumber = cardnumber; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
package org.exam.databean; public class stuscore { private String sid; private String score; private String sname; public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public String getScore() { return score; } public void setScore(String score) { this.score = score; } }
package org.exam.databean; public class test { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package org.exam.databean; public class testpaper { private int nowid; private int preid; private String type; private String answer; public int getNowid() { return nowid; } public void setNowid(int nowid) { this.nowid = nowid; } public int getPreid() { return preid; } public void setPreid(int preid) { this.preid = preid; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getAnswer() { return answer; } public void setAnswer(String answer) { this.answer = answer; } }
5.
servlet实现类
package org.exam.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.exam.dao.TestDAO; import org.exam.dao.factory.DaoFactory; import org.exam.databean.student; import org.exam.databean.teacher; /** * Servlet implementation class AddUser */ @WebServlet("/AddUser") public class AddUser extends HttpServlet { private static final long serialVersionUID = 1L; public AddUser() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String Result="0"; int result=0; String type = request.getParameter("type"); TestDAO tdao = DaoFactory.gettestdao(); if(type.equals("teacher")) { teacher user = new teacher(); user.setTid(request.getParameter("id")); user.setTname(request.getParameter("name")); user.setSex(request.getParameter("sex")); user.setCardnumber(request.getParameter("IDcard")); user.setPhone(request.getParameter("phone")); user.setTitle(request.getParameter("title")); user.setPwd("12345"); result=tdao.saveteacher(user); }else if(type.equals("student")) { student user = new student(); user.setSid(request.getParameter("id")); user.setSname(request.getParameter("name")); user.setSex(request.getParameter("sex")); user.setCardnumber(request.getParameter("IDcard")); user.setPhone(request.getParameter("phone")); user.setDepartment(request.getParameter("title")); user.setPwd("12345"); result=tdao.savestudent(user); } if(result!=0){ Result="OK"; }else Result="NO"; request.getRequestDispatcher("./jsp/addUser.jsp?Result="+Result).forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package org.exam.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.exam.dao.TestDAO; import org.exam.dao.factory.DaoFactory; @WebServlet("/DeleteServlet") public class DeleteServlet extends HttpServlet { private static final long serialVersionUID = 1L; public DeleteServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); int id = Integer.valueOf(request.getParameter("id")); int page = Integer.valueOf(request.getParameter("page")); String type = request.getParameter("type"); String key = request.getParameter("key"); TestDAO tdao = DaoFactory.gettestdao(); int result = 0; if (type.equals("Choice")) { result = tdao.deletechoice(id); } else result = tdao.deletefill(id); //response.sendRedirect("./TestManage?page=" + page + "&type=" + type + "&key=" + key); request.getRequestDispatcher("./TestManage?page=" + page + "&type=" + type + "&key=" + key).forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package org.exam.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.exam.dao.TestDAO; import org.exam.dao.factory.DaoFactory; @WebServlet("/DeleteUser") public class DeleteUser extends HttpServlet { private static final long serialVersionUID = 1L; public DeleteUser() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String type = request.getParameter("type"); int page = Integer.valueOf(request.getParameter("page")); String key = request.getParameter("key"); String selectStyle = request.getParameter("selectStyle"); String id = request.getParameter("id"); TestDAO tdao = DaoFactory.gettestdao(); if (type.equals("Teacher")) { tdao.deleteteacher(id); } else { tdao.deletestudent(id); } request.getRequestDispatcher("./ShowUserInfo?page=" + page + "&type=" + type + "&key=" + key + "&selectStyle=" + selectStyle).forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package org.exam.servlet; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.exam.dao.TestDAO; import org.exam.dao.factory.DaoFactory; import org.exam.databean.choicequestion; import org.exam.databean.fillquestion; @WebServlet("/EnterTest") public class EnterTest extends HttpServlet { private static final long serialVersionUID = 1L; public EnterTest() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); TestDAO tdao = DaoFactory.gettestdao(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String startTime = tdao.searchStime(); if (startTime.equals("null")) { System.out.println("ERROR.Don't set the test time"); return; } try { Date stime = sdf.parse(startTime); int testMinute = Integer.parseInt(tdao.searchTime()); Date ntime = new Date(); Calendar rightNow = Calendar.getInstance(); rightNow.setTime(stime); rightNow.add(Calendar.MINUTE,testMinute); Date sTime = rightNow.getTime(); //考试结束时间 if (ntime.before(sTime)) { List<choicequestion> list1 = tdao.findchoiceques(); StringBuffer sb = new StringBuffer(); for (int i = 1; i <= list1.size(); i++) { if (i == 1) sb.append("<div class=\"con\" style=\"display: block;\">"); else if (i % 5 == 1) { sb.append("<div class=\"con\" style=\"display: none;\">"); } sb.append("<div class=\"t\"><div class=\"title\"><label>" + i + "."); sb.append(list1.get(i - 1).getC_question()); sb.append("</label></div>"); sb.append("<div class=\"answer\"><label><input name=\"t" + i + "\" type=\"radio\" value=\"A\" />"); sb.append(list1.get(i - 1).getC_choiceA()); sb.append("</label> <label><input name=\"t" + i + "\" type=\"radio\" value=\"B\" />"); sb.append(list1.get(i - 1).getC_choiceB()); sb.append("</label> <label><input name=\"t" + i + "\" type=\"radio\" value=\"C\" />"); sb.append(list1.get(i - 1).getC_choiceC()); sb.append("</label> <label><input name=\"t" + i + "\" type=\"radio\" value=\"D\" />"); sb.append(list1.get(i - 1).getC_choiceD()); sb.append("</label></div></div>"); if (i % 5 == 0) { sb.append("</div>"); } } List<fillquestion> list2 = tdao.findfillques(); for (int i = 51; i <= list2.size() + 50; i++) { if (i % 5 == 1) { sb.append("<div class=\"con\" style=\"display: none;\">"); } sb.append("<div class=\"t\"><div class=\"title\"><label>题目" + i + ":"); sb.append(list2.get(i - 51).getF_question()); sb.append("</label></div>"); sb.append("<div class=\"answer\"><label><input name=\"t" + i + "\" type=\"text\" value=\"\"/> </label></div></div>"); if (i % 5 == 0) { sb.append("</div>"); } } //System.out.println(sb); request.setAttribute("sb", sb); //考试结束时间 SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); String endTime; endTime = df.format(sTime); HttpSession session = request.getSession(); session.setAttribute("endTime", endTime); request.getRequestDispatcher("./jsp/stuTest.jsp").forward(request, response); //response.sendRedirect("./jsp/stuTest.jsp"); } else { request.setAttribute("overTest", "over"); request.getRequestDispatcher("./jsp/stuMenu.jsp").forward(request, response); System.out.println("考试时间已过"); } } catch (ParseException e) { e.printStackTrace(); } //response.sendRedirect("./jsp/stuTest.jsp"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package org.exam.servlet; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Random; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.exam.dao.TestDAO; import org.exam.dao.factory.DaoFactory; import org.exam.databean.choicequestion; import org.exam.databean.fillquestion; @WebServlet("/FormTest") public class FormTest extends HttpServlet { private static final long serialVersionUID = 1L; public FormTest() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String dateTime = request.getParameter("Datetime"); String timeLimit = request.getParameter("Timelimit"); TestDAO tdao = DaoFactory.gettestdao(); //清空试卷表、时间表、成绩表 tdao.deletetestpaper(); tdao.deletetime(); tdao.deletescore(); tdao.savetime(dateTime, timeLimit); List list = new ArrayList(); Random r = new Random(); int nums = 50; int end = tdao.searchallchoice(""); int start = 1; int num = 1; while (list.size() != nums) { num = r.nextInt(end-start) + start; if(!list.contains(num)){ list.add(num); } } //生成选择题 tdao.savechoice(list); list.clear(); end = tdao.searchallfill(""); while (list.size() != nums) { num = r.nextInt(end-start) + start; if(!list.contains(num)){ list.add(num); } } //生成填空题 tdao.savefill(list); request.getRequestDispatcher("./ShowTest").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package org.exam.servlet; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Random; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.exam.dao.TestDAO; import org.exam.dao.factory.DaoFactory; import org.exam.databean.choicequestion; import org.exam.databean.fillquestion; @WebServlet("/FormTest") public class FormTest extends HttpServlet { private static final long serialVersionUID = 1L; public FormTest() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String dateTime = request.getParameter("Datetime"); String timeLimit = request.getParameter("Timelimit"); TestDAO tdao = DaoFactory.gettestdao(); //清空试卷表、时间表、成绩表 tdao.deletetestpaper(); tdao.deletetime(); tdao.deletescore(); tdao.savetime(dateTime, timeLimit); List list = new ArrayList(); Random r = new Random(); int nums = 50; int end = tdao.searchallchoice(""); int start = 1; int num = 1; while (list.size() != nums) { num = r.nextInt(end-start) + start; if(!list.contains(num)){ list.add(num); } } //生成选择题 tdao.savechoice(list); list.clear(); end = tdao.searchallfill(""); while (list.size() != nums) { num = r.nextInt(end-start) + start; if(!list.contains(num)){ list.add(num); } } //生成填空题 tdao.savefill(list); request.getRequestDispatcher("./ShowTest").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package org.exam.servlet; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import org.exam.dao.TestDAO; import org.exam.dao.factory.DaoFactory; import org.exam.databean.choicequestion; import org.exam.databean.fillquestion; public class ReadFile { private String fileName; public ReadFile(String fileName) { this.fileName = fileName; } //打开选择题文件并存入数据库 public int readChoiceFile() throws IOException { int result = 0; //System.out.println(fileName); //File f=new File("C:\\Users\\ZC\\eclipse-workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp1\\wtpwebapps\\examOnline\\upload\\" + fileName); File f = new File("D:\\" + fileName); FileInputStream in; in = new FileInputStream(f); BufferedReader br=new BufferedReader(new InputStreamReader(in,"GBK")); String line=null; br.mark((int) f.length()); while(!(line=br.readLine()).equals("答案")) { ; } line = br.readLine(); String[] answer=line.trim().split(" +"); //for (int i = 0; i < answer.length; i++) // System.out.println(answer[i]); br.reset(); choicequestion cq = new choicequestion(); int flag = 1; int i = 0; while(!(line=br.readLine()).equals("答案")) { //System.out.println(line); if (flag % 2 == 1) { line = line.substring(2, line.length()); cq.setC_question(line.trim()); } else { String[] choice = line.trim().split("\\s\\s+"); cq.setC_choiceA(choice[0].trim()); cq.setC_choiceB(choice[1].trim()); cq.setC_choiceC(choice[2].trim()); cq.setC_choiceD(choice[3].trim()); cq.setC_answer(answer[i++].trim()); /*System.out.println(cq.getC_question()); System.out.println(cq.getC_choiceA()); System.out.println(cq.getC_choiceD()); System.out.println(cq.getC_answer());*/ TestDAO tdao = DaoFactory.gettestdao(); result = tdao.savechoiceq(cq); } flag++; } br.close(); in.close(); return result; } //打开填空题文件并存入数据库 public int readFillFile() throws IOException { int result = 0; //File f=new File("C:\\Users\\ZC\\eclipse-workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp1\\wtpwebapps\\examOnline\\upload\\" + fileName); File f = new File("D:\\" + fileName); FileInputStream in; in = new FileInputStream(f); BufferedReader br=new BufferedReader(new InputStreamReader(in,"GBK")); String line=null; fillquestion fq = new fillquestion(); int flag = 1; while((line=br.readLine()) != null) { //System.out.println(line); if (flag % 2 == 1) { line = line.substring(2, line.length()); fq.setF_question(line.trim()); } else { fq.setF_answer(line.trim()); //System.out.println(fq.getF_question()); //System.out.println(fq.getF_answer()); TestDAO tdao = DaoFactory.gettestdao(); result = tdao.savefillq(fq); } flag++; } br.close(); in.close(); return result; } }
package org.exam.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.exam.dao.TestDAO; import org.exam.dao.factory.DaoFactory; import org.exam.databean.stuscore; @WebServlet("/ShowScore") public class ShowScore extends HttpServlet { private static final long serialVersionUID = 1L; public ShowScore() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); TestDAO tdao = DaoFactory.gettestdao(); if (tdao.existscore() != 0) { List<stuscore> list = tdao.findscore(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < list.size(); i++) { sb.append("<tr><td>"); sb.append(list.get(i).getSid()); sb.append("</td><td>"); sb.append(list.get(i).getSname()); sb.append("</td><td>"); sb.append(list.get(i).getScore()); sb.append("</td></tr>"); } request.setAttribute("score", sb); //System.out.println(sb); } else { request.setAttribute("noScore", "noScore"); } request.getRequestDispatcher("./jsp/showScore.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package org.exam.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.exam.dao.TestDAO; import org.exam.dao.factory.DaoFactory; import org.exam.databean.choicequestion; import org.exam.databean.fillquestion; @WebServlet("/ShowTest") public class ShowTest extends HttpServlet { private static final long serialVersionUID = 1L; public ShowTest() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); TestDAO tdao = DaoFactory.gettestdao(); if (tdao.existtime() != 0) { StringBuffer sb = new StringBuffer(); List<choicequestion> list1 = tdao.findchoiceques(); sb.append("<div class=\"t\"><div class=\"title\"><span>考试开始时间:"); String time = tdao.searchStime(); time = time.substring(0, time.length() - 2); sb.append(time); sb.append(" 考试时长:"); sb.append(tdao.searchTime()); sb.append("分钟</span></div></div>"); for (int i = 0; i < list1.size(); i++) { sb.append("<div class=\"t\"><div class=\"title\"><span>"); sb.append(i + 1 + "."); sb.append(list1.get(i).getC_question()); sb.append("</span></div><div class=\"choice\"><div><span>"); sb.append(list1.get(i).getC_choiceA()); sb.append("</span></div><div><span>"); sb.append(list1.get(i).getC_choiceB()); sb.append("</span></div><div><span>"); sb.append(list1.get(i).getC_choiceC()); sb.append("</span></div><div><span>"); sb.append(list1.get(i).getC_choiceD()); sb.append("</span></div></div><div class=\"answer\"><span>答案:"); sb.append(list1.get(i).getC_answer()); sb.append("</span></div></div>"); } List<fillquestion> list2 = tdao.findfillques(); for (int i = 0; i < list2.size(); i++) { sb.append("<div class=\"t\"><div class=\"title\"><span>"); sb.append(i + 1 + list1.size() + "."); sb.append(list2.get(i).getF_question()); sb.append("</span></div><div class=\"answer\"><span>答案:"); sb.append(list2.get(i).getF_answer()); //System.out.println(list2.get(i).getF_answer()); sb.append("</span></div></div>"); } request.setAttribute("testInfo", sb); } else { request.setAttribute("noTest", "noTest"); } request.getRequestDispatcher("./jsp/testView.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package org.exam.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.exam.dao.TestDAO; import org.exam.dao.factory.DaoFactory; import org.exam.databean.student; import org.exam.databean.teacher; @WebServlet("/ShowUserInfo") public class ShowUserInfo extends HttpServlet { private static final long serialVersionUID = 1L; public ShowUserInfo() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); int page = Integer.valueOf(request.getParameter("page")); String type = request.getParameter("type"); String key = request.getParameter("key"); String selectStyle = request.getParameter("selectStyle"); request.setAttribute("type", type); request.setAttribute("key", key); request.setAttribute("page", page); request.setAttribute("selectStyle", selectStyle); TestDAO tdao = DaoFactory.gettestdao(); StringBuffer sb = new StringBuffer(); if (type.equals("Teacher")) { sb.append("<table class=\"table table-hover table-bordered\">"); sb.append("<caption><span id=\"titleChange\">教师用户信息</span></caption>"); sb.append("<thead><tr><th>教工号</th><th>姓名</th><th>性别</th><th>身份证号</th>"); sb.append("<th>职称</th><th>密码</th><th>电话号码</th><th>操作</th></tr></thead>"); List<teacher> list = tdao.searchteacher(key, selectStyle, page); for (int i = 0; i < list.size(); i++) { sb.append("<tr><td>"); sb.append(list.get(i).getTid()); sb.append("</td><td>"); sb.append(list.get(i).getTname()); sb.append("</td><td>"); sb.append(list.get(i).getSex()); sb.append("</td><td>"); sb.append(list.get(i).getCardnumber()); sb.append("</td><td>"); sb.append(list.get(i).getTitle()); sb.append("</td><td>"); sb.append(list.get(i).getPwd()); sb.append("</td><td>"); sb.append(list.get(i).getPhone()); sb.append("</td><td>"); sb.append("<form action=\"DeleteUser?type=" + type + "&id=" + list.get(i).getTid() + "&page=" + page + "&key=" + key + "&selectStyle=" + selectStyle + "\" method=\"post\" onsubmit=\"return checkDelete()\">"); sb.append("<button class=\"btn btn-primary\" data-toggle=\"modal\" data-target=\"#myModal1\" type=\"button\" onclick=\"editTest(this)\">修改</button>"); sb.append("<button class=\"btn btn-primary\" type=\"submit\">删除</button></form></td></tr>"); } //总页数 int pages = tdao.searchteaallpage(key, selectStyle); //总人数 int counts = tdao.findtea(key, selectStyle); sb.append("</table><div><span>共"); sb.append(pages); sb.append("页"); sb.append(counts); sb.append("个用户</span> "); sb.append("<label><a href=\"./ShowUserInfo?page=1&type=Teacher&key=" + key + "&selectStyle=" + selectStyle + "\" class=\"abtn\">首页</a></label> "); int last = page - 1; if (last <= 0) last = 1; sb.append("<label><a href=\"./ShowUserInfo?page=" + last + "&type=Teacher&key=" + key + "&selectStyle=" + selectStyle + "\" class=\"abtn\">上一页</a></label> "); int next = page + 1; if (next > pages) next = pages; sb.append("<label><a href=\"./ShowUserInfo?page=" + next + "&type=Teacher&key=" + key + "&selectStyle=" + selectStyle + "\" class=\"abtn\">下一页</a></label> "); sb.append("<label><a href=\"./ShowUserInfo?page=" + pages + "&type=Teacher&key=" + key + "&selectStyle=" + selectStyle + "\" class=\"abtn\">尾页</a></label> "); sb.append("<span>当前第"); sb.append(page); sb.append("页</span></div>"); } else { sb.append("<table class=\"table table-hover table-bordered\">"); sb.append("<caption><span id=\"titleChange\">学生用户信息</span></caption>"); sb.append("<thead><tr><th>学号</th><th>姓名</th><th>性别</th><th>身份证号</th>"); sb.append("<th>系别</th><th>密码</th><th>电话号码</th><th>操作</th></tr></thead>"); List<student> list = tdao.searchstudent(key, selectStyle, page); for (int i = 0; i < list.size(); i++) { sb.append("<tr><td>"); sb.append(list.get(i).getSid()); sb.append("</td><td>"); sb.append(list.get(i).getSname()); sb.append("</td><td>"); sb.append(list.get(i).getSex()); sb.append("</td><td>"); sb.append(list.get(i).getCardnumber()); sb.append("</td><td>"); sb.append(list.get(i).getDepartment()); sb.append("</td><td>"); sb.append(list.get(i).getPwd()); sb.append("</td><td>"); sb.append(list.get(i).getPhone()); sb.append("</td><td>"); sb.append("<form action=\"DeleteUser?type=" + type + "&id=" + list.get(i).getSid() + "&page=" + page + "&key=" + key + "&selectStyle=" + selectStyle + "\" method=\"post\" onsubmit=\"return checkDelete()\">"); sb.append("<button class=\"btn btn-primary\" data-toggle=\"modal\" data-target=\"#myModal1\" type=\"button\" onclick=\"editTest(this)\">修改</button>"); sb.append("<button class=\"btn btn-primary\" type=\"submit\">删除</button></form></td></tr>"); } //总页数 int pages = tdao.searchstuallpage(key, selectStyle); //总人数 int counts = tdao.findstu(key, selectStyle); sb.append("</table><div><span>共"); sb.append(pages); sb.append("页"); sb.append(counts); sb.append("个用户</span> "); sb.append("<label><a href=\"./ShowUserInfo?page=1&type=Student&key=" + key + "&selectStyle=" + selectStyle + "\" class=\"abtn\">首页</a></label> "); int last = page - 1; if (last <= 0) last = 1; sb.append("<label><a href=\"./ShowUserInfo?page=" + last + "&type=Student&key=" + key + "&selectStyle=" + selectStyle + "\" class=\"abtn\">上一页</a></label> "); int next = page + 1; if (next > pages) next = pages; sb.append("<label><a href=\"./ShowUserInfo?page=" + next + "&type=Student&key=" + key + "&selectStyle=" + selectStyle + "\" class=\"abtn\">下一页</a></label> "); sb.append("<label><a href=\"./ShowUserInfo?page=" + pages + "&type=Student&key=" + key + "&selectStyle=" + selectStyle + "\" class=\"abtn\">尾页</a></label> "); sb.append("<span>当前第"); sb.append(page); sb.append("页</span></div>"); } request.setAttribute("UserTable", sb); request.getRequestDispatcher("./jsp/personManage.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package org.exam.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.exam.dao.TestDAO; import org.exam.dao.factory.DaoFactory; import org.exam.databean.choicequestion; import org.exam.databean.fillquestion; @WebServlet("/TestManage") public class TestManage extends HttpServlet { private static final long serialVersionUID = 1L; public TestManage() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); int page = Integer.valueOf(request.getParameter("page")); String type = request.getParameter("type"); String keyword = request.getParameter("key"); request.setAttribute("type", type); request.setAttribute("key", keyword); request.setAttribute("page", page); TestDAO tdao = DaoFactory.gettestdao(); StringBuffer sb = new StringBuffer(); if (type.equals("Choice")) { List<choicequestion> list = tdao.findtitle(page, "0", keyword); sb.append("<table style=\"table-layout:fixed\" class=\"table table-hover table-bordered\">"); sb.append("<caption>选择题</caption><thead><tr>"); sb.append("<th>试题ID</th><th>题目</th><th>选项A</th><th>选项B</th><th>选项C</th><th>选项D</th><th>答案</th><th>操作</th></tr></thead>"); for (int i = 0; i < list.size(); i++) { sb.append("<tr><td>"); sb.append(list.get(i).getC_id()); sb.append("</td><td>"); sb.append(list.get(i).getC_question()); sb.append("</td><td>"); sb.append(list.get(i).getC_choiceA()); sb.append("</td><td>"); sb.append(list.get(i).getC_choiceB()); sb.append("</td><td>"); sb.append(list.get(i).getC_choiceC()); sb.append("</td><td>"); sb.append(list.get(i).getC_choiceD()); sb.append("</td><td>"); sb.append(list.get(i).getC_answer()); sb.append("</td>"); sb.append("<td><button class=\"btn btn-primary\" data-toggle=\"modal\" data-target=\"#myModal1\" onclick=\"editTest(this)\">修改</button><br/>"); sb.append("<form action=\"./DeleteServlet?type=Choice&id=" + list.get(i).getC_id() + "&page=" + page + "&key=" + keyword + "\" method=\"post\" onsubmit=\"return checkDelete()\">"); sb.append("<button class=\"btn btn-primary\" type=\"submit\">删除</button></form></td></tr>"); } //总页数 int pages = tdao.searchallpage("0", keyword); //总题数 int counts = tdao.searchallchoice(keyword); sb.append("</table><div><span>共"); sb.append(pages); sb.append("页"); sb.append(counts); sb.append("题</span> <label>"); sb.append("<a href=\"./TestManage?page=1&type=Choice&key=" + keyword + "\" class=\"abtn\">首页</a></label> "); // int last = page - 1; if (last <= 0) last = 1; sb.append("<label><a href=\"./TestManage?page=" + last + "&type=Choice&key=" + keyword + "\" class=\"abtn\">上一页</a></label> "); // int next = page + 1; if (next > pages) next = pages; sb.append("<label><a href=\"./TestManage?page=" + next + "&type=Choice&key=" + keyword + "\" class=\"abtn\">下一页</a></label> "); sb.append("<label><a href=\"./TestManage?page=" + pages + "&type=Choice&key=" + keyword + "\" class=\"abtn\">尾页</a></label> "); sb.append("<span>当前第" + page + "页</span></div>"); } else { List<fillquestion> list = tdao.findtitle(page, "1", keyword); sb.append("<table class=\"table table-hover table-bordered\">"); sb.append("<caption>填空题</caption><thead><tr>"); sb.append("<th>试题ID</th><th>题目</th><th>答案</th><th>操作</th></tr></thead>"); for (int i = 0; i < list.size(); i++) { sb.append("<tr><td>"); sb.append(list.get(i).getF_id()); sb.append("</td><td>"); sb.append(list.get(i).getF_question()); sb.append("</td><td>"); sb.append(list.get(i).getF_answer()); sb.append("</td>"); sb.append("<td><button class=\"btn btn-primary\" data-toggle=\"modal\" data-target=\"#myModal2\" onclick=\"editTest(this)\">修改</button><br/>"); sb.append("<form action=\"./DeleteServlet?type=Completion&id=" + list.get(i).getF_id() + "&page=" + page + "&key=" + keyword + "\" method=\"post\" onsubmit=\"return checkDelete()\">"); sb.append("<button class=\"btn btn-primary\" type=\"submit\">删除</button></form></td></tr>"); } //总页数 int pages = tdao.searchallpage("1", keyword); //总题数 int counts = tdao.searchallfill(keyword); sb.append("</table><div><span>共"); sb.append(pages); sb.append("页"); sb.append(counts); sb.append("题</span> <label>"); sb.append("<a href=\"./TestManage?page=1&type=Completion&key=" + keyword + "\" class=\"abtn\">首页</a></label> "); // int last = page - 1; if (last <= 0) last = 1; sb.append("<label><a href=\"./TestManage?page=" + last + "&type=Completion&key=" + keyword + "\" class=\"abtn\">上一页</a></label> "); // int next = page + 1; if (next > pages) next = pages; sb.append("<label><a href=\"./TestManage?page=" + next + "&type=Completion&key=" + keyword + "\" class=\"abtn\">下一页</a></label> "); sb.append("<label><a href=\"./TestManage?page=" + pages + "&type=Completion&key=" + keyword + "\" class=\"abtn\">尾页</a></label> "); sb.append("<span>当前第" + page + "页</span></div>"); } request.setAttribute("Table", sb); //response.setContentType("text/html; charset=UTF-8"); request.getRequestDispatcher("./jsp/testManage.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package org.exam.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.exam.dao.TestDAO; import org.exam.dao.factory.DaoFactory; import org.exam.databean.testpaper; @WebServlet("/TestSubmit") public class TestSubmit extends HttpServlet { private static final long serialVersionUID = 1L; public TestSubmit() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); TestDAO tdao = DaoFactory.gettestdao(); List<testpaper> list = tdao.findanswer(); int score = 0; String answer = " "; for (int i = 1; i <= list.size(); i++) { answer = request.getParameter("t" + i); //System.out.println("answer" + i + "=" + answer); if (answer != null) { if (answer.equals(list.get(i - 1).getAnswer())) score += 2; } } HttpSession session = request.getSession(); tdao.savescore(session.getAttribute("stuID").toString(), score); request.setAttribute("score", score); //request.setAttribute("aTest", "alearlyTest"); request.getRequestDispatcher("./jsp/stuMenu.jsp").forward(request, response); //System.out.println(score); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package org.exam.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.exam.dao.TestDAO; import org.exam.dao.factory.DaoFactory; @WebServlet("/UpdatePassword") public class UpdatePassword extends HttpServlet { private static final long serialVersionUID = 1L; public UpdatePassword() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String ID = request.getParameter("id"); String type = request.getParameter("type"); String Password = request.getParameter("newPassword"); TestDAO tdao = DaoFactory.gettestdao(); int result = 0; if(type.equals("Student")) { result = tdao.updatespwd(ID, Password); }else if(type.equals("teacher")) { result = tdao.updatetpwd(ID, Password); }else if(type.equals("admin")) { result = tdao.updateapwd(ID, Password); } String Result="0"; HttpSession session = request.getSession(); if(result!=0){ session.setAttribute("PassWord", Password); Result="OK"; }else Result="NO"; if(type.equals("Student")) request.getRequestDispatcher("./jsp/stuMenu.jsp?Result="+Result).forward(request, response); else if(type.equals("teacher")||type.equals("admin")) request.getRequestDispatcher("./jsp/updatePwd.jsp?Result="+Result).forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package org.exam.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.exam.dao.TestDAO; import org.exam.dao.factory.DaoFactory; import org.exam.databean.choicequestion; import org.exam.databean.fillquestion; @WebServlet("/UpdateServlet") public class UpdateServlet extends HttpServlet { private static final long serialVersionUID = 1L; public UpdateServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String type = request.getParameter("type"); int page = Integer.valueOf(request.getParameter("page")); String key = request.getParameter("key"); TestDAO tdao = DaoFactory.gettestdao(); if (type.equals("Choice")) { choicequestion cq = new choicequestion(); System.out.println(request.getParameter("testID")); cq.setC_id(Integer.valueOf(request.getParameter("testID"))); cq.setC_question(request.getParameter("Title")); cq.setC_choiceA(request.getParameter("OptionA")); cq.setC_choiceB(request.getParameter("OptionB")); cq.setC_choiceC(request.getParameter("OptionC")); cq.setC_choiceD(request.getParameter("OptionD")); cq.setC_answer(request.getParameter("answer")); tdao.updatechoice(cq); } else { fillquestion fq = new fillquestion(); fq.setF_id(Integer.valueOf(request.getParameter("testID"))); fq.setF_question(request.getParameter("Title")); fq.setF_answer(request.getParameter("answer")); tdao.updatefill(fq); } request.setAttribute("type", type); request.setAttribute("key", key); request.setAttribute("page", page); response.setContentType("text/html; charset=UTF-8"); //response.sendRedirect("./TestManage?page=" + page + "&type=" + type + "&key=" + key); request.getRequestDispatcher("./TestManage?page=" + page + "&type=" + type + "&key=" + key).forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package org.exam.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.exam.dao.TestDAO; import org.exam.dao.factory.DaoFactory; import org.exam.databean.student; import org.exam.databean.teacher; @WebServlet("/UpdateUser") public class UpdateUser extends HttpServlet { private static final long serialVersionUID = 1L; public UpdateUser() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String type = request.getParameter("type"); int page = Integer.valueOf(request.getParameter("page")); String key = request.getParameter("key"); String selectStyle = request.getParameter("selectStyle"); TestDAO tdao = DaoFactory.gettestdao(); if (type.equals("Teacher")) { teacher t = new teacher(); t.setTid(request.getParameter("ID")); t.setTname(request.getParameter("name")); t.setSex(request.getParameter("sex")); t.setCardnumber(request.getParameter("IDcard")); t.setPwd(request.getParameter("pwd")); t.setTitle(request.getParameter("perType")); t.setPhone(request.getParameter("phone")); tdao.updateteacher(t); } else { student s = new student(); s.setSid(request.getParameter("ID")); s.setSname(request.getParameter("name")); s.setSex(request.getParameter("sex")); s.setCardnumber(request.getParameter("IDcard")); s.setPwd(request.getParameter("pwd")); s.setDepartment(request.getParameter("perType")); s.setPhone(request.getParameter("phone")); tdao.updatestudent(s); } request.setAttribute("type", type); request.setAttribute("key", key); request.setAttribute("page", page); request.setAttribute("selectStyle", selectStyle); request.getRequestDispatcher("./ShowUserInfo?page=" + page + "&type=" + type + "&key=" + key + "&selectStyle=" + selectStyle).forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package org.exam.servlet; import java.io.IOException; import java.sql.SQLException; import java.util.Date; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.exam.dao.TestDAO; import org.exam.dao.factory.DaoFactory; import com.jspsmart.upload.File; import com.jspsmart.upload.Files; import com.jspsmart.upload.Request; import com.jspsmart.upload.SmartUpload; import com.jspsmart.upload.SmartUploadException; @WebServlet("/UploadFileQuestions") public class UploadFileQuestions extends HttpServlet { private static final long serialVersionUID = 1L; public UploadFileQuestions() { super(); } ServletConfig servletconfig; public void init(ServletConfig config) throws ServletException { this.servletconfig = config; } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); SmartUpload smartUpload = new SmartUpload(); //上传初始化 smartUpload.initialize(servletconfig,request,response); try { //设定上传限制 //限制每个上传文件的最大长度;将最大设定为1024*1024*20 smartUpload.setMaxFileSize(1024*1024*10); //限制总上传数据的长度 smartUpload.setTotalMaxFileSize(1024*1024*20); //限制允许上传的文件类型、允许doc、txt文件 smartUpload.setAllowedFilesList("doc,txt,docx"); //限制禁止上传的文件类型,禁止exe、jsp、和没有扩展名的文件 smartUpload.setDeniedFilesList("exe,jsp,,"); //上传文件 smartUpload.upload(); //smartUpload.save(path); } catch (SQLException e) { e.printStackTrace(); } catch (SmartUploadException e) { e.printStackTrace(); } long d = new Date().getTime(); Files files = smartUpload.getFiles(); File file = files.getFile(0); String fileName = String.valueOf(d) + "." + file.getFileExt(); //String path = "/upload/" + fileName; String path = "D:/" + fileName; //将文件保存到指定的目录下 try { file.saveAs(path); } catch (SmartUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } Request r = smartUpload.getRequest(); String type = r.getParameter("type1"); ReadFile rf = new ReadFile(fileName); if (type.equals("Choice")) { if (rf.readChoiceFile() != 0) { request.setAttribute("upload", "upload"); request.getRequestDispatcher("./jsp/upload.jsp").forward(request, response); } else { request.setAttribute("uploadFail", "uploadFail"); request.getRequestDispatcher("./jsp/upload.jsp").forward(request, response); } } else { if (rf.readFillFile() != 0) { request.setAttribute("upload", "upload"); request.getRequestDispatcher("./jsp/upload.jsp").forward(request, response); } else { request.setAttribute("uploadFail", "uploadFail"); request.getRequestDispatcher("./jsp/upload.jsp").forward(request, response); } } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package org.exam.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.exam.dao.TestDAO; import org.exam.dao.factory.DaoFactory; import org.exam.databean.choicequestion; import org.exam.databean.fillquestion; @WebServlet("/UploadQuestions") public class UploadQuestions extends HttpServlet { private static final long serialVersionUID = 1L; public UploadQuestions() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); TestDAO tdao = DaoFactory.gettestdao(); String type = request.getParameter("type"); if (type.equals("Choice")) { choicequestion cq = new choicequestion(); cq.setC_question(request.getParameter("choiceTitle")); cq.setC_choiceA("A." + request.getParameter("OptionA")); cq.setC_choiceB("B." + request.getParameter("OptionB")); cq.setC_choiceC("C." + request.getParameter("OptionC")); cq.setC_choiceD("D." + request.getParameter("OptionD")); cq.setC_answer(request.getParameter("answer")); if (tdao.savechoiceq(cq) != 0) { request.setAttribute("upload", "upload"); request.getRequestDispatcher("./jsp/upload.jsp").forward(request, response); } else { request.setAttribute("uploadFail", "uploadFail"); request.getRequestDispatcher("./jsp/upload.jsp").forward(request, response); } } else { fillquestion fq = new fillquestion(); fq.setF_question(request.getParameter("completionTitle")); fq.setF_answer(request.getParameter("answerText")); if(tdao.savefillq(fq) != 0) { request.setAttribute("upload", "upload"); request.getRequestDispatcher("./jsp/upload.jsp").forward(request, response); } else { request.setAttribute("uploadFail", "uploadFail"); request.getRequestDispatcher("./jsp/upload.jsp").forward(request, response); } } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
6.工具类
package org.exam.util; import java.sql.*; public class DbUtil { private static final String URL = "jdbc:mysql://localhost:3306/examonline?characterEncoding=utf-8"; private static final String USER = "root"; private static final String PASSWORD = "123456"; protected static PreparedStatement s=null; protected static ResultSet rs = null; protected static Connection conn = null; public static synchronized Connection getConnection() { try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(URL, USER, PASSWORD); } catch (Exception e) { e.printStackTrace(); } return conn; } public static int executeUpdate(String sql) { int result = 0; try { s = getConnection().prepareStatement(sql); result = s.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } return result; } public static ResultSet executeQuery(String sql) { try { s = getConnection().prepareStatement(sql); rs = s.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; } public static PreparedStatement executePreparedStatement(String sql) { PreparedStatement ps = null; try { ps = getConnection().prepareStatement(sql); }catch(Exception e) { e.printStackTrace(); } return ps; } public static void rollback() { try { getConnection().rollback(); } catch (SQLException e) { e.printStackTrace(); } } public static void close() { try { if(rs!=null) rs.close(); if(s!= null) s.close(); if(conn!=null) conn.close(); }catch(SQLException e) { e.printStackTrace(); } } }
7.未完成部分:
●缺少访问控制功能(session验证)
●考试试卷不能正常生成
●缺少事务控制
●前端部分页面未实现