基于mysql纯java的智能题库(后期填写注释)
1 package controller; 2 3 import admin.entity.Admin; 4 import admin.service.AdminService; 5 import admin.service.impl.AdminServiceImpl; 6 import questionBank.entity.*; 7 import questionBank.service.ExamService; 8 import questionBank.service.Impl.ExamServiceImpl; 9 import questionBank.service.Impl.QuestionBankServiceImpl; 10 import questionBank.service.Impl.ScoreSheetServiceImpl; 11 import questionBank.service.QuestionBankService; 12 import questionBank.service.ScoreSheetService; 13 import user.entity.User; 14 import user.entity.WrongQuestions; 15 import user.service.UserService; 16 import user.service.WrongQuestionService; 17 import user.service.impl.UserServiceImpl; 18 import user.service.impl.WrongQuestionServiceImpl; 19 import utils.BaseDao; 20 21 import java.text.SimpleDateFormat; 22 import java.util.ArrayList; 23 import java.util.Date; 24 import java.util.List; 25 import java.util.Scanner; 26 27 /** 28 * @author 阿水 29 * @create 2023-02-04 19:48 30 * 31 * _ooOoo_ 32 * o8888888o 33 * 88" . "88 34 * (| -_- |) 35 * O\ = /O 36 * ____/`---'\____ 37 * .' \\| |// `. 38 * / \\||| : |||// \ 39 * / _||||| -:- |||||- \ 40 * | | \\\ - /// | | 41 * | \_| ''\---/'' | | 42 * \ .-\__ `-` ___/-. / 43 * ___`. .' /--.--\ `. . __ 44 * ."" '< `.___\_<|>_/___.' >'"". 45 * | | : `- \`.;`\ _ /`;.`/ - ` : | | 46 * \ \ `-. \_ __\ /__ _/ .-` / / 47 * ======`-.____`-.___\_____/___.-`____.-'====== 48 * `=---=' 49 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 50 * 佛祖保佑 永无BUG 51 * 52 */ 53 public class Main extends BaseDao { 54 public static void main(String[] args) { 55 UserService userService = new UserServiceImpl(); 56 AdminService adminService = new AdminServiceImpl(); 57 QuestionBankService questionBankService = new QuestionBankServiceImpl(); 58 ScoreSheetService scoreSheetService = new ScoreSheetServiceImpl(); 59 WrongQuestionService wrongQuestionService = new WrongQuestionServiceImpl(); 60 ExamService examService = new ExamServiceImpl(); 61 SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间 62 sdf.applyPattern("yyyy-MM-dd HH:mm:ss");// a为am/pm的标记 63 Scanner sc = new Scanner(System.in); 64 while (true) { 65 System.out.println("1、管理员"); 66 System.out.println("2、用户"); 67 int x = sc.nextInt(); 68 if (x == 1) { 69 System.out.println("请输入管理员用户名"); 70 String loginName = sc.next(); 71 System.out.println("请输入管理员密码"); 72 String password = sc.next(); 73 int loginAdminId = adminService.login(loginName, password); 74 if (loginAdminId > 0) { 75 System.out.println("登陆完成"); 76 while (true) { 77 System.out.println("1、查看所有题目"); 78 System.out.println("2、添加题目"); 79 System.out.println("3、删除题目"); 80 System.out.println("4、新注册管理员"); 81 System.out.println("5、出试卷"); 82 System.out.println("6、查看试卷"); 83 System.out.println("7、查看成绩"); 84 int y = sc.nextInt(); 85 if (y == 1) { 86 //查看所有题目 87 List<QuestionBank> questionBanks = questionBankService.selectAll(); 88 for (QuestionBank questionBank : questionBanks) { 89 System.out.println(questionBank); 90 } 91 92 } else if (y == 2) { 93 //添加题目 94 List<TypeName> typeName = questionBankService.findTypeName(); 95 for (TypeName name : typeName) { 96 System.out.println(name); 97 } 98 System.out.println("请输入题目类型"); 99 String type = sc.next(); 100 System.out.println("请输入题目"); 101 String content = sc.next(); 102 System.out.println("请输入答案"); 103 String answer = sc.next(); 104 questionBankService.insert(new QuestionBank(0, type, content, answer)); 105 106 } else if (y == 3) { 107 //删除题目 108 List<QuestionBank> questionBanks = questionBankService.selectAll(); 109 for (QuestionBank questionBank : questionBanks) { 110 System.out.println(questionBank); 111 } 112 System.out.println("请输入删除题目的id"); 113 int id = sc.nextInt(); 114 questionBankService.delete(id); 115 116 } else if (y == 4) { 117 //新注册管理员 118 Admin admin = new Admin(); 119 System.out.println("请输入管理员名称"); 120 String name = sc.next(); 121 System.out.println("请输入账号"); 122 String loginAdminName = sc.next(); 123 System.out.println("请输入密码"); 124 String pwd = sc.next(); 125 admin.setName(name); 126 admin.setLoginName(loginAdminName); 127 admin.setPassword(pwd); 128 adminService.insert(admin); 129 130 } else if (y == 5) { 131 //出试卷 132 int count = 0; 133 List<QuestionBank> questionBanks = questionBankService.selectAll(); 134 for (QuestionBank questionBank : questionBanks) { 135 System.out.println(questionBank); 136 } 137 System.out.println("请输入本次试卷名称"); 138 String examName = sc.next(); 139 while (true) { 140 System.out.println("请输入加入考试的题目id,按0结束"); 141 int id = sc.nextInt(); 142 for (QuestionBank questionBank : questionBanks) { 143 if (id == questionBank.getId()) { 144 examService.insert(id, examName); 145 count--; 146 } 147 count++; 148 } 149 if (count == questionBanks.size()) { 150 System.out.println("查无此题"); 151 break; 152 } 153 if (id == 0) { 154 break; 155 } 156 } 157 158 } else if (y == 6) { 159 List<Exam> exams = examService.selectAll(); 160 for (Exam exam : exams) { 161 System.out.println(exam); 162 } 163 164 } else if (y == 7) { 165 List<ScoreSheet> scoreSheets = scoreSheetService.selectAll(); 166 for (ScoreSheet scoreSheet : scoreSheets) { 167 System.out.println(scoreSheet); 168 } 169 } else { 170 System.out.println("再见"); 171 break; 172 } 173 174 } 175 176 177 } else { 178 System.out.println("登陆失败"); 179 180 } 181 } else if (x == 2) { 182 183 while (true) { 184 System.out.println("1、登录"); 185 System.out.println("2、注册"); 186 int y = sc.nextInt(); 187 if (y == 1) { 188 while (true) { 189 System.out.println("请输入登录账号"); 190 String loginName = sc.next(); 191 System.out.println("请输入登录密码"); 192 String password = sc.next(); 193 int loginUserId = userService.login(loginName, password); 194 if (loginUserId > 0) { 195 System.out.println("登录完成"); 196 while (true) { 197 198 System.out.println("1、刷题模式"); 199 System.out.println("2、按照学科刷题模式"); 200 System.out.println("3、开始考试"); 201 System.out.println("4、按照学科测验练习"); 202 System.out.println("5、查看错题本"); 203 System.out.println("请输入您的选项"); 204 int z = sc.nextInt(); 205 if (z == 1) { 206 ArrayList<QuestionBank> QuestionBank = new ArrayList<>(); 207 List<QuestionBank> questionBanks = questionBankService.selectAll(); 208 for (questionBank.entity.QuestionBank questionBank : questionBanks) { 209 System.out.println(questionBank); 210 } 211 212 } else if (z == 2) { 213 //按照学科刷题模式 214 //ArrayList<QuestionBank> QuestionBank = new ArrayList<>(); 215 List<TypeName> typeName = questionBankService.findTypeName(); 216 for (TypeName name : typeName) { 217 System.out.println(name); 218 } 219 System.out.println("请您完整输入课程学科"); 220 String inputTypeName = sc.next(); 221 int count = 0; 222 for (TypeName name : typeName) { 223 if (name.getName().equals(inputTypeName)) { 224 count++; 225 } 226 } 227 if (count == 0) { 228 System.out.println("课程不存在 请仔细检查"); 229 break; 230 } else { 231 List<QuestionBank> questionBankByType = questionBankService.selectByType(inputTypeName); 232 for (QuestionBank questionBank : questionBankByType) { 233 System.out.println(questionBank); 234 } 235 } 236 237 } else if (z == 3) { 238 // 输出已经格式化的现在时间(24小时制) 239 List<ExamName> examNames = examService.selectByName(); 240 for (ExamName examName : examNames) { 241 System.out.println(examName); 242 } 243 System.out.println("请您完整输入考试名称"); 244 String inputTypeName = sc.next(); 245 int count = 0; 246 for (ExamName examName : examNames) { 247 if (examName.getName().equals(inputTypeName)) { 248 count++; 249 System.out.println("这是考试模式 记录成绩!!!!!"); 250 } 251 } 252 if (count == 0) { 253 System.out.println("课程不存在 请仔细检查"); 254 } else { 255 256 //select * from exam inner join questionbank on exam.questionid = questionbank.id where examName = '无敌巨难试卷'; 257 List<Exam> exams = examService.selectByType(inputTypeName); 258 double number = 0; 259 double scoreByUser = 0; 260 for (Exam exam : exams) { 261 System.out.println(exam.getTypeName() + " " + exam.getContent()); 262 System.out.println("请输入答案"); 263 String answerByUser = sc.next(); 264 if (answerByUser.equals(exam.getAnswer())) { 265 scoreByUser++; 266 }/*else { 267 //wrongQuestionService.insert(); 268 List<WrongQuestions> wrongQuestions = wrongQuestionService.selectByUserId(loginUserId); 269 int var=0; 270 for (WrongQuestions wrongQuestion : wrongQuestions) { 271 System.out.println(wrongQuestion); 272 273 if (wrongQuestion.getContent().equals(exam.getContent())) { 274 wrongQuestionService.update(new WrongQuestions(loginUserId, wrongQuestion.getContent())); 275 break; 276 } else { 277 var++; 278 } 279 if (var == wrongQuestions.size()) { 280 wrongQuestionService.insert(new WrongQuestions(loginUserId,exam.getQuestionId())); 281 } 282 } 283 284 }*/ 285 number++; 286 } 287 288 double sumScore = (scoreByUser / number) * 100; 289 String str = String.format("%.1f", sumScore); 290 System.out.println("您考试 考了" + str + "分"); 291 double finalScore = Double.parseDouble(str); 292 //(int scoreId, String examName, double score, Timestamp dateTime, int userId, String userName) 293 Date date = new Date();// 获取当前时间 294 System.out.println(sdf.format(date)); 295 ScoreSheet scoreSheet1 = new ScoreSheet(1, inputTypeName, finalScore, sdf.format(date).toString(), loginUserId, loginName); 296 scoreSheetService.insert(scoreSheet1); 297 } 298 299 } else if (z == 4) { 300 //按照学科刷题 301 List<TypeName> typeName = questionBankService.findTypeName(); 302 for (TypeName name : typeName) { 303 System.out.println(name); 304 } 305 System.out.println("请您完整输入课程学科"); 306 String inputTypeName = sc.next(); 307 int count = 0; 308 for (TypeName name : typeName) { 309 if (name.getName().equals(inputTypeName)) { 310 count++; 311 } 312 } 313 if (count == 0) { 314 System.out.println("课程不存在 请仔细检查"); 315 } else { 316 List<QuestionBank> questionBankByType = questionBankService.selectByType(inputTypeName); 317 System.out.println("这是娱乐模式 不记录成绩"); 318 double number = 0; 319 double scoreByUser = 0; 320 for (QuestionBank questionBank : questionBankByType) { 321 System.out.println(questionBank.getType() + " " + questionBank.getContent()); 322 System.out.println("请输入答案"); 323 String answerByUser = sc.next(); 324 if (answerByUser.equals(questionBank.getAnswer())) { 325 scoreByUser++; 326 } else { 327 //wrongQuestionService.insert(); 328 List<WrongQuestions> wrongQuestions = wrongQuestionService.selectByUserId(loginUserId); 329 int var=0; 330 for (WrongQuestions wrongQuestion : wrongQuestions) { 331 //System.out.println(wrongQuestion); 332 333 if (wrongQuestion.getContent().equals(questionBank.getContent())) { 334 wrongQuestionService.update(new WrongQuestions(loginUserId, wrongQuestion.getContent())); 335 break; 336 } else { 337 var++; 338 } 339 if (var == wrongQuestions.size()) { 340 wrongQuestionService.insert(new WrongQuestions(loginUserId, questionBank.getId())); 341 } 342 } 343 344 } 345 number++; 346 } 347 double sumScore = (scoreByUser / number) * 100; 348 String str = String.format("%.1f", sumScore); 349 System.out.println("您本次测验考了" + str + "分"); 350 System.out.println("考试结束,记录已上传 无法再打开"); 351 352 353 } 354 } else if (z == 5) { 355 List<WrongQuestions> wrongQuestions = wrongQuestionService.selectByUserId(loginUserId); 356 for (WrongQuestions wrongQuestion : wrongQuestions) { 357 System.out.println("类型:" + wrongQuestion.getTypeName() + " 题目:" + wrongQuestion.getContent() + " 答案:" + wrongQuestion.getAnswer() + " 累计错误次数:" + wrongQuestion.getCount() + "次"); 358 } 359 } else { 360 System.out.println("再见"); 361 break; 362 } 363 } 364 365 } else { 366 System.out.println("登陆失败"); 367 break; 368 } 369 370 371 } 372 } else if (y == 2) { 373 System.out.println("请输入姓名"); 374 String name = sc.next(); 375 System.out.println("请输入登录账号"); 376 String loginName = sc.next(); 377 System.out.println("请输入登录密码"); 378 String password = sc.next(); 379 userService.insert(new User(0, name, loginName, password)); 380 System.out.println("注册完成啦!!!快去登陆吧哈哈嗨"); 381 } else { 382 System.out.println("拜拜了您嘞"); 383 break; 384 } 385 386 387 } 388 389 } 390 391 } 392 393 } 394 }
package admin.dao.impl; import admin.dao.AdminDao; import admin.entity.Admin; import utils.BaseDao; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; /** * @author 刘品水 * @create 2023-02-04 18:31 */ public class AdminDaoImpl extends BaseDao implements AdminDao { @Override public int insert(Admin admin) { String sql = "insert into admin (name,loginName,password) " + "values ('" + admin.getName() + "','" + admin.getLoginName() + "','" + admin.getPassword() + "')"; int result = 0; System.out.println(sql); try { result = this.update(sql); } catch (Exception e) { e.printStackTrace(); } return result; } @Override public int delete(int id) { String sql = "delete from admin where id=" + id; System.out.println(sql); int result = 0; try { result = this.update(sql); } catch (Exception e) { e.printStackTrace(); } return result; } @Override public int update(Admin admin) { String sql = "update admin set name='" + admin.getName() + "', password='" + admin.getPassword() + "' where id=" + admin.getId(); int result = 0; try { result = this.update(sql); } catch (Exception e) { e.printStackTrace(); } System.out.println(sql); return result; } @Override public List<Admin> selectAll() { ArrayList<Admin> admins = new ArrayList<>(); ResultSet resultSet = null; try { resultSet = this.find("select * from admin"); while (resultSet.next()) { Admin a = new Admin(resultSet.getInt("id"), resultSet.getString("name"), resultSet.getString("loginName"), resultSet.getString("password")); admins.add(a); } } catch (Exception e) { throw new RuntimeException(e); } return admins; } @Override public Admin selectById(int id) { ResultSet resultSet = null; Admin a = new Admin(); try { resultSet = this.find("select * from admin where id=" + id); if (resultSet != null) { a = new Admin(resultSet.getInt("id"), resultSet.getString("name"), resultSet.getString("loginName"), resultSet.getString("password")); } } catch (Exception e) { e.printStackTrace(); } return a; } @Override public int login(String loginName, String password) { int id = 0; ResultSet resultSet = null; try { String sql = "select * from admin where loginName=? and password=?"; PreparedStatement pst = this.getPreparedStatement(sql); pst.setString(1, loginName); pst.setString(2, password); resultSet = pst.executeQuery(); /* System.out.println(resultSet); System.out.println(sql);*/ if (resultSet.next()) { id = resultSet.getInt("id"); } } catch (Exception e) { e.printStackTrace(); } //return false; return id; } }
package admin.dao; import admin.entity.Admin; import java.util.List; /** * @author 刘品水 * @create 2023-02-04 18:30 */ public interface AdminDao { int insert(Admin admin); int delete(int id); int update(Admin admin); List<Admin> selectAll(); Admin selectById(int id); int login(String loginName, String password); }
package admin.entity; public class Admin{ private int id; private String name; private String loginName; private String password; public Admin() { } public Admin(int id, String name, String loginName, String password) { this.id = id; this.name = name; this.loginName = loginName; this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "Admin{" + "id=" + id + ", name='" + name + '\'' + ", loginName='" + loginName + '\'' + ", password='" + password + '\'' + '}'; } }
package admin.service.impl; import admin.dao.AdminDao; import admin.dao.impl.AdminDaoImpl; import admin.entity.Admin; import admin.service.AdminService; import java.util.List; /** * @author 阿水 * @create 2023-02-05 16:46 */ public class AdminServiceImpl implements AdminService { private AdminDao adminDao=new AdminDaoImpl(); @Override public int insert(Admin admin) { return adminDao.insert(admin); } @Override public int delete(int id) { return adminDao.delete(id); } @Override public int update(Admin admin) { return adminDao.update(admin); } @Override public List<Admin> selectAll() { return adminDao.selectAll(); } @Override public Admin selectById(int id) { return adminDao.selectById(id); } @Override public int login(String loginName, String password) { return adminDao.login(loginName,password); } }
package admin.service; import admin.entity.Admin; import java.util.List; /** * @author 阿水 * @create 2023-02-05 16:46 */ public interface AdminService { int insert(Admin admin); int delete(int id); int update(Admin admin); List<Admin> selectAll(); Admin selectById(int id); int login(String loginName, String password); }
package questionBank.dao; import questionBank.entity.Exam; import questionBank.entity.ExamName; import java.util.List; /** * @author 阿水 * @create 2023-02-05 19:23 */ public interface ExamDao { int insert(int qid, String eName); int delete(int id); int update(Exam exam); List<Exam> selectAll(); Exam selectById(int id); List<ExamName> selectByName(); List<Exam> selectByType(String inputTypeName); }
package questionBank.dao.impl; import questionBank.dao.QuestionBankDao; import questionBank.entity.QuestionBank; import questionBank.entity.TypeName; import utils.BaseDao; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; /** * @author 阿水 * @create 2023-02-05 18:50 */ public class QuestionBankDaoImpl extends BaseDao implements QuestionBankDao { @Override public int insert(QuestionBank questionBank) { String sql = "insert into questionbank (typeName,content,answer) " + "values ('" + questionBank.getType() + "','" + questionBank.getContent() + "','" + questionBank.getAnswer() + "')"; int result = 0; //System.out.println(sql); try { result = this.update(sql); } catch (Exception e) { e.printStackTrace(); } return result; } @Override public int delete(int id) { String sql = "delete from questionbank where id=" + id; //System.out.println(sql); int result = 0; try { result = this.update(sql); } catch (Exception e) { e.printStackTrace(); } return result; } @Override public int update(QuestionBank questionBank) { String sql = "update questionbank set typeName='" +questionBank.getType() + "', content='" + questionBank.getContent() +"',answer='"+ questionBank.getAnswer()+"' where id=" + questionBank.getId(); int result = 0; try { result = this.update(sql); } catch (Exception e) { e.printStackTrace(); } //System.out.println(sql); return result; } @Override public List<QuestionBank> selectAll() { ArrayList<QuestionBank> list = new ArrayList<>(); ResultSet resultSet = null; try { resultSet = this.find("select * from questionbank"); while (resultSet.next()) { QuestionBank a = new QuestionBank(resultSet.getInt("id"), resultSet.getString("typeName"), resultSet.getString("content"), resultSet.getString("answer")); list.add(a); } } catch (Exception e) { throw new RuntimeException(e); } return list; } @Override public QuestionBank selectById(int id) { ResultSet resultSet = null; QuestionBank a = null; try { resultSet = this.find("select * from questionbank where id=" + id); if (resultSet != null) { a = new QuestionBank(resultSet.getInt("id"), resultSet.getString("typeName"), resultSet.getString("content"), resultSet.getString("answer")); } } catch (Exception e) { e.printStackTrace(); } return a; } @Override public List<TypeName> findTypeName() { ArrayList<TypeName> list = new ArrayList<>(); ResultSet resultSet = null; try { resultSet = this.find("select distinct typeName from questionbank;"); while (resultSet.next()) { TypeName a = new TypeName(resultSet.getString("typeName")); list.add(a); } } catch (Exception e) { throw new RuntimeException(e); } return list; } @Override public List<QuestionBank> selectByType(String inputTypeName) { ArrayList<QuestionBank> list = new ArrayList<>(); ResultSet resultSet = null; try { resultSet = this.find("select * from questionbank where typeName = '"+inputTypeName+"';"); while (resultSet.next()) { QuestionBank a = new QuestionBank(resultSet.getInt("id"), resultSet.getString("typeName"), resultSet.getString("content"), resultSet.getString("answer")); list.add(a); } } catch (Exception e) { throw new RuntimeException(e); } return list; } }
package questionBank.dao.impl; import questionBank.dao.ScoreSheetDao; import questionBank.entity.ScoreSheet; import utils.BaseDao; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; /** * @author 阿水 * @create 2023-02-07 11:04 */ public class ScoreSheetDaoImpl extends BaseDao implements ScoreSheetDao { @Override public int insert(ScoreSheet scoreSheet) { /* String sql = "insert into questionbank (typeName,content,answer) " + "values ('" + questionBank.getType() + "','" + questionBank.getContent() + "','" + questionBank.getAnswer() + "')"; */ String sql="insert into score_sheet (examName, userId, score, datatime) values" + " ('"+scoreSheet.getExamName()+"',"+scoreSheet.getUserId()+","+scoreSheet.getScore()+",'"+scoreSheet.getDateTime()+"');"; int result = 0; System.out.println(sql); try { result = this.update(sql); } catch (Exception e) { e.printStackTrace(); } return result; } @Override public int delete(int id) { return 0; } @Override public int update(ScoreSheet scoreSheet) { return 0; } @Override public List<ScoreSheet> selectAll() { ArrayList<ScoreSheet> list = new ArrayList<>(); ResultSet resultSet = null; try { resultSet = this.find("select score_sheet.id,examName, score, datatime , user.id,name from score_sheet inner join user on score_sheet.userId=user.id;"); while (resultSet.next()) { ScoreSheet a = new ScoreSheet(resultSet.getInt("score_sheet.id"),resultSet.getString("score_sheet.examName"),resultSet.getDouble("score_sheet.score"),resultSet.getString("datatime"),resultSet.getInt("user.id"),resultSet.getString("user.name")); list.add(a); } } catch (Exception e) { throw new RuntimeException(e); } return list; } @Override public ScoreSheet selectById(int id) { return null; } }
package questionBank.dao; import questionBank.entity.Exam; import questionBank.entity.ExamName; import java.util.List; /** * @author 阿水 * @create 2023-02-05 19:23 */ public interface ExamDao { int insert(int qid, String eName); int delete(int id); int update(Exam exam); List<Exam> selectAll(); Exam selectById(int id); List<ExamName> selectByName(); List<Exam> selectByType(String inputTypeName); }
package questionBank.dao; import questionBank.entity.QuestionBank; import questionBank.entity.TypeName; import java.util.List; /** * @author 阿水 * @create 2023-02-05 18:50 */ public interface QuestionBankDao { int insert(QuestionBank questionBank); int delete(int id); int update(QuestionBank questionBank); List<QuestionBank> selectAll(); QuestionBank selectById(int id); List<TypeName> findTypeName(); List<QuestionBank> selectByType(String inputTypeName); }
package questionBank.dao; import questionBank.entity.ScoreSheet; import java.util.List; /** * @author 阿水 * @create 2023-02-07 11:04 */ public interface ScoreSheetDao { int insert(ScoreSheet scoreSheet); int delete(int id); int update(ScoreSheet scoreSheet); List<ScoreSheet> selectAll(); ScoreSheet selectById(int id); }
package questionBank.entity; import java.io.Serializable; public class Exam implements Serializable { private int id; private int questionId; private String examName; private String typeName; private String content; private String answer; public Exam(int questionId, String typeName, String content, String answer) { this.questionId = questionId; this.typeName = typeName; this.content = content; this.answer = answer; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getQuestionId() { return questionId; } public void setQuestionId(int questionId) { this.questionId = questionId; } public Exam(int id, int questionId, String examName, String typeName, String content, String answer) { this.id = id; this.questionId = questionId; this.examName = examName; this.typeName = typeName; this.content = content; this.answer = answer; } public Exam(String examName, String typeName, String content, String answer) { this.examName = examName; this.typeName = typeName; this.content = content; this.answer = answer; } public Exam() { } @Override public String toString() { return "Exam{" + "id=" + id + ", examName='" + examName + '\'' + ", typeName='" + typeName + '\'' + ", content='" + content + '\'' + ", answer='" + answer + '\'' + '}'; } public Exam(int id, String examName, String typeName, String content, String answer) { this.id = id; this.examName = examName; this.typeName = typeName; this.content = content; this.answer = answer; } public Exam(String examName, String typeName, String content) { this.examName = examName; this.typeName = typeName; this.content = content; } /* public Exam(String examName, String typeName, String content, String answer) { this.examName = examName; this.typeName = typeName; this.content = content; this.answer = answer; } */ public String getExamName() { return examName; } public void setExamName(String examName) { this.examName = examName; } public String getTypeName() { return typeName; } public void setTypeName(String typeName) { this.typeName = typeName; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getAnswer() { return answer; } public void setAnswer(String answer) { this.answer = answer; } }
package questionBank.entity; /** * @author 阿水 * @create 2023-02-07 14:38 */ public class ExamName { private String name ; public ExamName() { } public ExamName(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "ExamName{" + "name='" + name + '\'' + '}'; } }
package questionBank.entity; public class QuestionBank { private int id; private String type;//题目标题,可省略 private String content; private String answer; public QuestionBank() { } public QuestionBank(int id, String type, String content, String answer) { this.id = id; this.type = type; this.content = content; this.answer = answer; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getAnswer() { return answer; } public void setAnswer(String answer) { this.answer = answer; } @Override public String toString() { return "QuestionBank{" + "id=" + id + ", type='" + type + '\'' + ", content='" + content + '\'' + ", answer='" + answer + '\'' + '}'; } }
package questionBank.entity; /** * @author 阿水 * @create 2023-02-07 11:01 */ public class ScoreSheet { private int scoreId; private String examName; private double score; private String dateTime; private int userId; private String userName; public ScoreSheet() { } @Override public String toString() { return "ScoreSheet{" + "scoreId=" + scoreId + ", examName='" + examName + '\'' + ", score=" + score + ", dateTime='" + dateTime + '\'' + ", userId=" + userId + ", userName='" + userName + '\'' + '}'; } public int getScoreId() { return scoreId; } public void setScoreId(int scoreId) { this.scoreId = scoreId; } public String getExamName() { return examName; } public void setExamName(String examName) { this.examName = examName; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } public String getDateTime() { return dateTime; } public void setDateTime(String dateTime) { this.dateTime = dateTime; } public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public ScoreSheet(int scoreId, String examName, double score, String dateTime, int userId, String userName) { this.scoreId = scoreId; this.examName = examName; this.score = score; this.dateTime = dateTime; this.userId = userId; this.userName = userName; } }
package questionBank.entity; /** * @author 阿水 * @create 2023-02-07 10:03 */ public class TypeName { private String name; public TypeName() { } public TypeName(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "TypeName{" + "name='" + name + '\'' + '}'; } }
package questionBank.service.Impl; import questionBank.dao.ExamDao; import questionBank.dao.impl.ExamDaoImpl; import questionBank.entity.Exam; import questionBank.entity.ExamName; import questionBank.service.ExamService; import java.util.List; /** * @author 阿水 * @create 2023-02-05 20:05 */ public class ExamServiceImpl implements ExamService { ExamDao examDao=new ExamDaoImpl(); @Override public int insert(int qid, String eName) { return examDao.insert(qid,eName); } @Override public int delete(int id) { return 0; } @Override public int update(Exam exam) { return 0; } @Override public List<Exam> selectAll() { return examDao.selectAll(); } @Override public Exam selectById(int id) { return null; } @Override public List<ExamName> selectByName() { return examDao.selectByName(); } @Override public List<Exam> selectByType(String inputTypeName) { return examDao.selectByType(inputTypeName); } }
package questionBank.service.Impl; import questionBank.dao.QuestionBankDao; import questionBank.dao.impl.QuestionBankDaoImpl; import questionBank.entity.QuestionBank; import questionBank.entity.TypeName; import questionBank.service.QuestionBankService; import java.util.List; /** * @author 阿水 * @create 2023-02-05 19:02 */ public class QuestionBankServiceImpl implements QuestionBankService { private QuestionBankDao questionBankDao=new QuestionBankDaoImpl(); @Override public int insert(QuestionBank questionBank) { return questionBankDao.insert(questionBank); } @Override public int delete(int id) { return questionBankDao.delete(id); } @Override public int update(QuestionBank questionBank) { return questionBankDao.update(questionBank); } @Override public List<QuestionBank> selectAll() { return questionBankDao.selectAll(); } @Override public QuestionBank selectById(int id) { return questionBankDao.selectById(id); } @Override public List<TypeName> findTypeName() { return questionBankaDao.findTypeName(); } @Override public List<QuestionBank> selectByType(String inputTypeName) { return questionBankDao.selectByType(inputTypeName); } }
package questionBank.service.Impl; import questionBank.dao.ScoreSheetDao; import questionBank.dao.impl.ScoreSheetDaoImpl; import questionBank.entity.ScoreSheet; import questionBank.service.ScoreSheetService; import java.util.List; /** * @author 阿水 * @create 2023-02-07 11:04 */ public class ScoreSheetServiceImpl implements ScoreSheetService { private ScoreSheetDao scoreSheetDao=new ScoreSheetDaoImpl(); @Override public int insert(ScoreSheet scoreSheet) { return scoreSheetDao.insert(scoreSheet); } @Override public int delete(int id) { return scoreSheetDao.delete(id); } @Override public int update(ScoreSheet scoreSheet) { return scoreSheetDao.update(scoreSheet); } @Override public List<ScoreSheet> selectAll() { return scoreSheetDao.selectAll(); } @Override public ScoreSheet selectById(int id) { return scoreSheetDao.selectById(id); } }
package questionBank.service; import questionBank.entity.Exam; import questionBank.entity.ExamName; import java.util.List; /** * @author 阿水 * @create 2023-02-05 19:23 */ public interface ExamService { int insert(int qid, String eName); int delete(int id); int update(Exam exam); List<Exam> selectAll(); Exam selectById(int id); List<ExamName> selectByName(); List<Exam> selectByType(String inputTypeName); }
package questionBank.service; import questionBank.entity.QuestionBank; import questionBank.entity.TypeName; import java.util.List; /** * @author 阿水 * @create 2023-02-05 18:50 */ public interface QuestionBankService { int insert(QuestionBank questionBank); int delete(int id); int update(QuestionBank questionBank); List<QuestionBank> selectAll(); QuestionBank selectById(int id); //void selectAlType(); List<TypeName> findTypeName(); List<QuestionBank> selectByType(String inputTypeName); }
package questionBank.service; import questionBank.entity.ScoreSheet; import java.util.List; /** * @author 阿水 * @create 2023-02-07 11:04 */ public interface ScoreSheetService { int insert(ScoreSheet scoreSheet); int delete(int id); int update(ScoreSheet scoreSheet); List<ScoreSheet> selectAll(); ScoreSheet selectById(int id); }
package user.dao.impl; import user.dao.UserDao; import user.entity.User; import utils.BaseDao; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; /** * @author 阿水 * @create 2023-02-05 14:05 */ public class UserDaoImpl extends BaseDao implements UserDao { @Override public int insert(User user) { String sql = "insert into user (name,loginName,password) " + "values ('" + user.getName() + "','" + user.getLoginName() + "','" + user.getPassword() + "')"; int result = 0; System.out.println(sql); try { result = this.update(sql); } catch (Exception e) { e.printStackTrace(); } return result; } @Override public int delete(int id) { String sql = "delete from user where id=" + id; System.out.println(sql); int result = 0; try { result = this.update(sql); } catch (Exception e) { e.printStackTrace(); } return result; } @Override public int update(User user) { String sql = "update user set name='" + user.getName() + "', password='" + user.getPassword() + "' where id=" + user.getId(); int result = 0; try { result = this.update(sql); } catch (Exception e) { e.printStackTrace(); } System.out.println(sql); return result; } @Override public List<User> selectAll() { ArrayList<User> users = new ArrayList<>(); ResultSet resultSet = null; try { resultSet = this.find("select * from user"); while (resultSet.next()) { User u = new User(resultSet.getInt("id"), resultSet.getString("name"), resultSet.getString("loginName"), resultSet.getString("password")); users.add(u); } } catch (Exception e) { throw new RuntimeException(e); } return users; } @Override public User selectById(int id) { ResultSet resultSet = null; User u = new User(); try { resultSet = this.find("select * from user where id=" + id); if (resultSet != null) { u = new User(resultSet.getInt("id"), resultSet.getString("name"), resultSet.getString("loginName"), resultSet.getString("password")); } } catch (Exception e) { e.printStackTrace(); } return u; } @Override public int login(String loginName, String password) { int id = 0; ResultSet resultSet = null; try { String sql = "select * from user where loginName=? and password=?"; PreparedStatement pst = this.getPreparedStatement(sql); pst.setString(1, loginName); pst.setString(2, password); resultSet = pst.executeQuery(); /* System.out.println(resultSet); System.out.println(sql);*/ if (resultSet.next()) { id = resultSet.getInt("id"); } } catch (Exception e) { e.printStackTrace(); } //return false; return id; } }
package user.dao.impl; import user.dao.WrongQuestionDao; import user.entity.WrongQuestions; import utils.BaseDao; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; /** * @author 阿水 * @create 2023-02-07 17:23 */ public class WrongQuestionDaoImpl extends BaseDao implements WrongQuestionDao { @Override public int insert(WrongQuestions wrongQuestions) { String sql = "insert into wrongquestion values (0,"+wrongQuestions.getUserId()+","+wrongQuestions.getQuestionId()+",1);"; int result = 0; System.out.println(sql); try { result = this.update(sql); } catch (Exception e) { e.printStackTrace(); } return result; } @Override public int delete(int id) { return 0; } @Override public int update(WrongQuestions wrongQuestions) { String sql = "update wrongquestion inner join user u on wrongquestion.user_id = u.id inner join questionbank q on wrongquestion.question_id = q.id set count=count + 1 where content = '"+wrongQuestions.getContent()+"' and user_id="+wrongQuestions.getUserId()+";"; int result = 0; try { result = this.update(sql); } catch (Exception e) { e.printStackTrace(); } System.out.println(sql); return result; } @Override public List<WrongQuestions> selectAll() { return null; } @Override public List<WrongQuestions> selectByContent() { ResultSet resultSet = null; ArrayList<WrongQuestions> list = new ArrayList<>(); try { resultSet = this.find("select content\n" + "from wrongquestion\n" + " inner join user u on wrongquestion.user_id = u.id\n" + " inner join questionbank q on wrongquestion.question_id = q.id\n" + "where user_id=1\n" + ";"); while (resultSet != null) { WrongQuestions wrongQuestions1 = new WrongQuestions(resultSet.getInt("wrongquestion.id"), resultSet.getString("typeName"), resultSet.getString("content"), resultSet.getString("answer"), resultSet.getInt("count")); list.add(wrongQuestions1); } } catch (Exception e) { e.printStackTrace(); } return list; } @Override public List<WrongQuestions> selectByUserId(int id) { ArrayList<WrongQuestions> list = new ArrayList<>(); ResultSet resultSet = null; try { resultSet = this.find("select typeName,content,answer,count from wrongquestion inner join user u on wrongquestion.user_id = u.id inner join questionbank q on wrongquestion.question_id = q.id where user_id="+id+";"); while (resultSet.next()) { WrongQuestions a = new WrongQuestions(resultSet.getString("typeName"),resultSet.getString("content"),resultSet.getString("answer"),resultSet.getInt("count")); list.add(a); } } catch (Exception e) { throw new RuntimeException(e); } return list; } }
package user.dao; import user.entity.User; import java.util.List; /** * @author 阿水 * @create 2023-02-05 14:01 */ public interface UserDao { int insert(User user); int delete(int id); int update(User user); List<User> selectAll(); User selectById(int id); int login(String loginName, String password); }
package user.dao; import user.entity.WrongQuestions; import java.util.List; /** * @author 阿水 * @create 2023-02-07 17:22 */ public interface WrongQuestionDao { int insert(WrongQuestions wrongQuestions); int delete(int id); int update(WrongQuestions wrongQuestions); List<WrongQuestions> selectAll(); List<WrongQuestions> selectByContent(); List<WrongQuestions> selectByUserId(int id); }
package user.entity; public class User { private int id; private String name; private String loginName; private String password; public User() { } public User(int id, String name, String loginName, String password) { this.id = id; this.name = name; this.loginName = loginName; this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", loginName='" + loginName + '\'' + ", password='" + password + '\'' + '}'; } }
package user.entity; import java.io.Serializable; //错题集 public class WrongQuestions implements Serializable { private int id; private int userId; private int questionId; private String typeName; public WrongQuestions(int userId, int questionId) { this.userId = userId; this.questionId = questionId; } public WrongQuestions(int userId, String content) { this.userId = userId; this.content = content; } private String content; private String answer; private int count; public WrongQuestions(String typeName, String content, String answer, int count) { this.typeName = typeName; this.content = content; this.answer = answer; this.count = count; } public WrongQuestions(int id, String typeName, String content, String answer, int count) { this.id = id; this.typeName = typeName; this.content = content; this.answer = answer; this.count = count; } public WrongQuestions(int id, int userId, int questionId, String typeName, String content, String answer, int count) { this.id = id; this.userId = userId; this.questionId = questionId; this.typeName = typeName; this.content = content; this.answer = answer; this.count = count; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public WrongQuestions() { } public int getQuestionId() { return questionId; } public void setQuestionId(int questionId) { this.questionId = questionId; } public WrongQuestions(int id, int userId, int questionId, String typeName, String content, String answer) { this.id = id; this.userId = userId; this.questionId = questionId; this.typeName = typeName; this.content = content; this.answer = answer; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getTypeName() { return typeName; } public void setTypeName(String typeName) { this.typeName = typeName; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getAnswer() { return answer; } public void setAnswer(String answer) { this.answer = answer; } @Override public String toString() { return "WrongQuestions{" + "id=" + id + ", userId=" + userId + ", questionId=" + questionId + ", typeName='" + typeName + '\'' + ", content='" + content + '\'' + ", answer='" + answer + '\'' + ", count=" + count + '}'; } }
package user.service.impl; import user.dao.UserDao; import user.dao.impl.UserDaoImpl; import user.entity.User; import user.service.UserService; import java.util.List; /** * @author 阿水 * @create 2023-02-05 15:25 */ public class UserServiceImpl implements UserService { private UserDao userDao=new UserDaoImpl(); @Override public int insert(User user) { return userDao.insert(user); } @Override public int delete(int id) { return userDao.delete(id); } @Override public int update(User user) { return userDao.update(user); } @Override public List<User> selectAll() { return userDao.selectAll(); } @Override public User selectById(int id) { return userDao.selectById(id); } @Override public int login(String loginName, String password) { return userDao.login(loginName,password); } }
package user.service.impl; import user.dao.WrongQuestionDao; import user.dao.impl.WrongQuestionDaoImpl; import user.entity.WrongQuestions; import user.service.WrongQuestionService; import java.util.List; /** * @author 阿水 * @create 2023-02-07 17:23 */ public class WrongQuestionServiceImpl implements WrongQuestionService { private WrongQuestionDao wrongQuestionDao=new WrongQuestionDaoImpl(); @Override public int insert(WrongQuestions wrongQuestions) { return wrongQuestionDao.insert(wrongQuestions); } @Override public int delete(int id) { return wrongQuestionDao.delete(id); } @Override public int update(WrongQuestions wrongQuestions) { return wrongQuestionDao.update(wrongQuestions); } @Override public List<WrongQuestions> selectAll() { return wrongQuestionDao.selectAll(); } @Override public List<WrongQuestions> selectByUserId(int id) { return wrongQuestionDao.selectByUserId(id); } }
package user.service; import user.entity.User; import java.util.List; /** * @author 阿水 * @create 2023-02-05 15:25 */ public interface UserService { int insert(User user); int delete(int id); int update(User user); List<User> selectAll(); User selectById(int id); int login(String loginName, String password); }
package user.service; import user.entity.WrongQuestions; import java.util.List; /** * @author 阿水 * @create 2023-02-07 17:23 */ public interface WrongQuestionService { int insert(WrongQuestions wrongQuestions); int delete(int id); int update(WrongQuestions wrongQuestions); List<WrongQuestions> selectAll(); List<WrongQuestions> selectByUserId(int id); }
package utils; import java.sql.*; /** * @author 阿水 * @create 2023-02-04 19:20 */ public class BaseDao { //!!!!resultSet private String url = "jdbc:mysql://localhost:3306/finalquestionbank?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"; private String root = "root"; private String pwd = "XXXXXXXX"; private Connection connection; private PreparedStatement preparedStatement; private ResultSet resultSet; /** * 增删改数据库 返回被执行操作的行数 * @param sql * @return * @throws Exception */ protected int update(String sql) throws Exception{ int var = 1; PreparedStatement pst=getPreparedStatement(sql); var = pst.executeUpdate(); this.closeAll(); return var; } /** * 查询 select 返回resultSet数据类型数据 * @param sql * @return * @throws Exception */ protected ResultSet find(String sql) throws Exception { PreparedStatement pst1=getPreparedStatement(sql); resultSet = pst1.executeQuery(); return resultSet; } /* protected ResultSet findByLogin(String sql) throws Exception { PreparedStatement pst1=getPreparedStatement(sql); resultSet = pst1.executeQuery(); return resultSet; }*/ public Connection getConnection() throws Exception { if (connection == null) { Class.forName("com.mysql.cj.jdbc.Driver"); this.connection = DriverManager.getConnection(url, root, pwd); } return connection; } public PreparedStatement getPreparedStatement(String sql) throws Exception { this.preparedStatement = getConnection().prepareStatement(sql); return preparedStatement; } /** * 关闭所有的SQL流 */ public void closeAll() { try { if (this.resultSet != null) { this.resultSet.close(); } if (this.preparedStatement!=null){ this.preparedStatement.close(); } if (this.connection!=null){ this.connection.close(); } } catch (Exception e) { e.printStackTrace(); } this.connection=null; } }