├── .gitignore ├── .settings ├── org.eclipse.wst.jsdt.ui.superType.name ├── org.eclipse.wst.jsdt.ui.superType.container ├── org.eclipse.core.resources.prefs ├── org.eclipse.wst.common.project.facet.core.xml ├── org.eclipse.jdt.core.prefs ├── org.eclipse.wst.common.component └── .jsdtscope ├── WebContent ├── META-INF │ └── MANIFEST.MF ├── WEB-INF │ ├── lib │ │ └── mariadb-java-client-2.4.1.jar │ └── web.xml └── hello.html ├── README.md ├── src ├── dataobject │ ├── Answer.java │ ├── Question.java │ ├── User.java │ ├── Selection.java │ └── Paper.java ├── controller │ └── TestServlet.java ├── util │ └── Conn.java ├── example │ └── Ex1.java └── dao │ ├── PaperDao.java │ ├── UserDao.java │ └── QuestionDao.java ├── .project ├── .classpath ├── LICENSE └── DatabaseTableModel ├── JSPModelData.sql └── JSPModel.sql /.gitignore: -------------------------------------------------------------------------------- 1 | /**/*.class 2 | -------------------------------------------------------------------------------- /.settings/org.eclipse.wst.jsdt.ui.superType.name: -------------------------------------------------------------------------------- 1 | Window -------------------------------------------------------------------------------- /WebContent/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Class-Path: 3 | 4 | -------------------------------------------------------------------------------- /.settings/org.eclipse.wst.jsdt.ui.superType.container: -------------------------------------------------------------------------------- 1 | org.eclipse.wst.jsdt.launching.baseBrowserLibrary -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding/=UTF-8 3 | -------------------------------------------------------------------------------- /WebContent/WEB-INF/lib/mariadb-java-client-2.4.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leafee98/Questionnaire/master/WebContent/WEB-INF/lib/mariadb-java-client-2.4.1.jar -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Questionnaire 2 | 3 | ## 文件说明 4 | 5 | * `DatabaseTableModel`文件夹内部保存`sql`建表语句以及部分测试数据 6 | * `.gitignore`保存无需被版本管理的文件类型,目前仅忽略了`.class`文件 7 | 8 | * 其余文件及文件夹均为Eclipse `Java`项目的配置文件或项目需求文件 9 | 10 | ## 项目起因 11 | 12 | 最后一次的Java Web实验工程量出奇的大,并且有多人合作的要求,于是为了更好得完成作业,此项目被上传至此处以方便合作和版本管理。 -------------------------------------------------------------------------------- /WebContent/hello.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Insert title here 6 | 7 | 8 | 9 |
10 | 11 | 12 |
13 | 14 | -------------------------------------------------------------------------------- /.settings/org.eclipse.wst.common.project.facet.core.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=11 4 | org.eclipse.jdt.core.compiler.compliance=11 5 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 6 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 7 | org.eclipse.jdt.core.compiler.release=enabled 8 | org.eclipse.jdt.core.compiler.source=11 9 | -------------------------------------------------------------------------------- /.settings/org.eclipse.wst.common.component: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.settings/.jsdtscope: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /WebContent/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | login.html 8 | 9 | 10 | testServlet 11 | controller.TestServlet 12 | 13 | 14 | testServlet 15 | /servlet/test 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/dataobject/Answer.java: -------------------------------------------------------------------------------- 1 | package dataobject; 2 | 3 | public class Answer { 4 | private long answerid; 5 | private long questionid; 6 | private long respondent; 7 | private String answer; 8 | 9 | public long getAnswerid() { return answerid; } 10 | public void setAnswerid(long answerid) { this.answerid = answerid; } 11 | 12 | public long getQuestionid() { return questionid; } 13 | public void setQuestionid(long questionid) { this.questionid = questionid; } 14 | 15 | public long getRespondent() { return respondent; } 16 | public void setRespondent(long respondent) { this.respondent = respondent; } 17 | 18 | public String getAnswer() { return answer; } 19 | public void setAnswer(String answer) { this.answer = answer; } 20 | } 21 | -------------------------------------------------------------------------------- /src/controller/TestServlet.java: -------------------------------------------------------------------------------- 1 | package controller; 2 | 3 | import javax.servlet.http.HttpServlet; 4 | import javax.servlet.http.HttpServletRequest; 5 | import javax.servlet.http.HttpServletResponse; 6 | import javax.servlet.ServletOutputStream; 7 | import java.io.IOException; 8 | 9 | public class TestServlet extends HttpServlet { 10 | private static final long serialVersionUID = 6718759761234978623L; 11 | 12 | @Override 13 | public void doGet(HttpServletRequest request, HttpServletResponse response) { 14 | ServletOutputStream out = null; 15 | try { 16 | out = response.getOutputStream(); 17 | out.print("

Hello!

"); 18 | out.print(request.getParameter("str_a")); 19 | } catch (IOException e) { 20 | e.printStackTrace(); 21 | } 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/dataobject/Question.java: -------------------------------------------------------------------------------- 1 | package dataobject; 2 | 3 | public class Question { 4 | private long questionid; // question id 5 | private long paperid; 6 | private String type; // radio, check, text; references to question_type 7 | private String question; 8 | 9 | public long getQuestionid() { return questionid; } 10 | public void setQuestionid(long questionid) { this.questionid = questionid; } 11 | 12 | public long getPaperid() { return paperid; } 13 | public void setPaperid(long paperid) { this.paperid = paperid; } 14 | 15 | public String getType() { return type; } 16 | public void setType(String type) { this.type = type; } 17 | 18 | public String getQuestion() { return question; } 19 | public void setQuestion(String question) { this.question = question; } 20 | } 21 | -------------------------------------------------------------------------------- /src/dataobject/User.java: -------------------------------------------------------------------------------- 1 | package dataobject; 2 | 3 | public class User { 4 | private long uid; 5 | private Boolean admin; 6 | private String username; 7 | private String passwd; 8 | private String nickname; 9 | 10 | public long getUid() { return uid; } 11 | public void setUid(long uid) { this.uid = uid; } 12 | 13 | public boolean isAdmin() { return admin; } 14 | public void setAdmin(boolean ad) { this.admin = ad; } 15 | 16 | public String getUsername() { return username; } 17 | public void setUsername(String username) { this.username = username; } 18 | 19 | public String getPasswd() { return passwd; } 20 | public void setPasswd(String passwd) { this.passwd = passwd; } 21 | 22 | public String getNickname() { return nickname; } 23 | public void setNickname(String nickname) { this.nickname = nickname; } 24 | } 25 | -------------------------------------------------------------------------------- /src/dataobject/Selection.java: -------------------------------------------------------------------------------- 1 | package dataobject; 2 | 3 | public class Selection { 4 | private long selectionid; 5 | private long questionid; 6 | private String selection_describe; 7 | 8 | public long getSelectionid() { return selectionid; } 9 | public void setSelectionid(long selectionid) { this.selectionid = selectionid; } 10 | 11 | public long getQuestionid() { return questionid; } 12 | public void setQuestionid(long questionid) { this.questionid = questionid; } 13 | 14 | public String getSelection_describe() { return selection_describe; } 15 | public void setSelection_describe(String selection_describe) { this.selection_describe = selection_describe; } 16 | 17 | public static Selection parseSelection(Long selectionid) { 18 | Selection blankSelection = new Selection(); 19 | blankSelection.setSelectionid(selectionid); 20 | return blankSelection; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/dataobject/Paper.java: -------------------------------------------------------------------------------- 1 | package dataobject; 2 | 3 | import java.sql.Timestamp; 4 | 5 | public class Paper { 6 | private long paperid; 7 | private long ownerid; 8 | private String papername; 9 | private Timestamp publish_time; 10 | private Timestamp cutoff_time; 11 | 12 | public Timestamp getPublish_time() { return publish_time; } 13 | public void setPublish_time(Timestamp publish_time) { this.publish_time = publish_time; } 14 | 15 | public Timestamp getCutoff_time() { return cutoff_time; } 16 | public void setCutoff_time(Timestamp cutoff_time) { this.cutoff_time = cutoff_time; } 17 | 18 | public long getPaperid() { return paperid; } 19 | public void setPaperid(long paperid) { this.paperid = paperid; } 20 | 21 | public long getOwnerid() { return ownerid; } 22 | public void setOwnerid(long ownerid) { this.ownerid = ownerid; } 23 | 24 | public String getPapername() { return papername; } 25 | public void setPapername(String papername) { this.papername = papername; } 26 | } 27 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Questionnaire 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.wst.common.project.facet.core.builder 15 | 16 | 17 | 18 | 19 | org.eclipse.wst.validation.validationbuilder 20 | 21 | 22 | 23 | 24 | 25 | org.eclipse.jem.workbench.JavaEMFNature 26 | org.eclipse.wst.common.modulecore.ModuleCoreNature 27 | org.eclipse.wst.common.project.facet.core.nature 28 | org.eclipse.jdt.core.javanature 29 | org.eclipse.wst.jsdt.core.jsNature 30 | 31 | 32 | -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/util/Conn.java: -------------------------------------------------------------------------------- 1 | package util; 2 | 3 | import java.sql.Connection; 4 | import java.sql.DriverManager; 5 | import java.sql.SQLException; 6 | 7 | public class Conn { 8 | private static String urlAndDatabase = "localhost:3306/questionnaire"; 9 | private static String username = "root"; 10 | private static String passwd = "Leafee98"; 11 | private static Connection connection = null; 12 | 13 | static { 14 | try { 15 | Class.forName("org.mariadb.jdbc.Driver"); 16 | connection = DriverManager.getConnection( 17 | "jdbc:mariadb://" + urlAndDatabase, username, passwd); 18 | } catch (ClassNotFoundException e) { 19 | e.printStackTrace(); 20 | System.out.println("the database's jdbc driver class not found!"); 21 | System.exit(-1); 22 | } catch (SQLException e) { 23 | System.out.println("Unknown SQLException!"); 24 | e.printStackTrace(); 25 | System.exit(-1); 26 | } 27 | } 28 | 29 | public static Connection getConnection() { 30 | /* Connection connection = null; 31 | try { 32 | } catch (SQLException e) { 33 | e.printStackTrace(); 34 | System.out.println("Unknown SQLException!"); 35 | System.exit(-2); 36 | } 37 | return connection;*/ 38 | 39 | return connection; 40 | } 41 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 2-Clause License 2 | 3 | Copyright (c) 2019, Leafee 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | -------------------------------------------------------------------------------- /DatabaseTableModel/JSPModelData.sql: -------------------------------------------------------------------------------- 1 | delete from q_answer; 2 | delete from q_selection; 3 | delete from q_question; 4 | delete from q_allow_answer; 5 | delete from q_paper; 6 | delete from q_user; 7 | 8 | insert into q_user (q_user.uid, q_user.admin, q_user.username, q_user.passwd, q_user.nickname) values 9 | (null, 1, 'leafee', 'feng', 'Leafee'); 10 | set @uid = (select uid from q_user where username = 'leafee'); 11 | 12 | insert into q_paper (paperid, uid, paper_name, publish_time, cutoff_time) values 13 | (null, @uid, 'test_paper', default, default); 14 | set @paperid = (select paperid from q_paper where uid = @uid); 15 | 16 | insert into q_question (questionid, paperid, question_type, question) values 17 | (null, @paperid, "radio", 'test_question, please choose the current one'); 18 | set @questionid = (select questionid from q_question where paperid = @paperid); 19 | 20 | insert into q_selection (selectionid, questionid, selection_describe) values 21 | (null, @questionid, 'the current one'); 22 | insert into q_selection (selectionid, questionid, selection_describe) values 23 | (null, @questionid, 'the wrong one'); 24 | set @selectionid = (select selectionid from q_selection where selection_describe = 'the current one'); 25 | 26 | insert into q_allow_answer (paperid, uid) values (@paperid, @uid); 27 | 28 | insert into q_answer (answerid, questionid, answer, respondent) values 29 | (null, @questionid, @selectionid, @uid); 30 | -------------------------------------------------------------------------------- /src/example/Ex1.java: -------------------------------------------------------------------------------- 1 | package example; 2 | 3 | 4 | import java.sql.Timestamp; 5 | import dao.*; 6 | 7 | public class Ex1 { 8 | public static void main(String[] args) { 9 | UserDao udUserA = new UserDao(); 10 | UserDao udUserB = new UserDao(); 11 | UserDao udUserC = new UserDao(); 12 | UserDao udUserD = new UserDao(); 13 | UserDao udUserE = new UserDao(); 14 | udUserA.addUser("usera", "usera", "USERA", true); 15 | udUserB.addUser("userb", "userb", "USERB", false); 16 | udUserC.addUser("userc", "userc", "USERC", false); 17 | udUserD.addUser("userd", "userd", "USERD", false); 18 | udUserE.addUser("usere", "usere", "USERE", false); 19 | 20 | PaperDao pdPaperPC = new PaperDao(); 21 | // PaperDao pdPaperGame = new PaperDao(); 22 | pdPaperPC.addPaper(udUserA.getUser().getUid(), "组装电脑意向调查", 23 | Timestamp.valueOf("2019-06-01 00:00:00"), 24 | Timestamp.valueOf("2019-06-30 23:59:59")); 25 | QuestionDao qdQuestionPC1 = new QuestionDao(); 26 | QuestionDao qdQuestionPC2 = new QuestionDao(); 27 | QuestionDao qdQuestionPC3 = new QuestionDao(); 28 | QuestionDao qdQuestionPC4 = new QuestionDao(); 29 | QuestionDao qdQuestionPC5 = new QuestionDao(); 30 | qdQuestionPC1.addQuestion(pdPaperPC.getPaper().getPaperid(), 31 | "radio", "内存容量"); 32 | qdQuestionPC1.addSelection("4G"); 33 | qdQuestionPC1.addSelection("8G"); 34 | qdQuestionPC1.addSelection("16G"); 35 | qdQuestionPC1.addSelection("32G"); 36 | qdQuestionPC2.addQuestion(pdPaperPC.getPaper().getPaperid(), 37 | "radio", "显卡品牌"); 38 | qdQuestionPC2.addSelection("AMD"); 39 | qdQuestionPC2.addSelection("英伟达"); 40 | qdQuestionPC3.addQuestion(pdPaperPC.getPaper().getPaperid(), 41 | "radio", "主板品牌"); 42 | qdQuestionPC3.addSelection("技嘉"); 43 | qdQuestionPC3.addSelection("华硕"); 44 | qdQuestionPC3.addSelection("华擎"); 45 | qdQuestionPC3.addSelection("微星"); 46 | qdQuestionPC4.addQuestion(pdPaperPC.getPaper().getPaperid(), 47 | "radio", "CPU品牌"); 48 | qdQuestionPC4.addSelection("AMD"); 49 | qdQuestionPC4.addSelection("英特尔"); 50 | qdQuestionPC5.addQuestion(pdPaperPC.getPaper().getPaperid(), 51 | "check", "看好哪些品牌机"); 52 | qdQuestionPC5.addSelection("戴尔"); 53 | qdQuestionPC5.addSelection("联想"); 54 | qdQuestionPC5.addSelection("神舟"); 55 | qdQuestionPC5.addSelection("惠普"); 56 | qdQuestionPC5.addSelection("机械革命"); 57 | 58 | // pdPaperGame.addPaper(udUserA.getUser().getUid(), "游戏意向调查", 59 | // Timestamp.valueOf("2019-06-15 00:00:00"), 60 | // Timestamp.valueOf("2019-06-25 23:59:59")); 61 | // QuestionDao qdQuestionGame1 = new QuestionDao(); 62 | // QuestionDao qdQuestionGame2 = new QuestionDao(); 63 | // QuestionDao qdQuestionGame3 = new QuestionDao(); 64 | // QuestionDao qdQuestionGame4 = new QuestionDao(); 65 | 66 | System.out.println("DONE"); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /DatabaseTableModel/JSPModel.sql: -------------------------------------------------------------------------------- 1 | DROP DATABASE IF EXISTS questionnaire; 2 | CREATE DATABASE questionnaire; USE questionnaire; 3 | CREATE TABLE q_user ( 4 | uid INT PRIMARY KEY AUTO_INCREMENT, 5 | username CHAR(30) NOT NULL UNIQUE, 6 | passwd CHAR(32) NOT NULL, 7 | nickname nchar(100) NOT NULL, 8 | admin BOOL NOT NULL 9 | ); 10 | CREATE TABLE q_paper ( 11 | paperid INT PRIMARY KEY AUTO_INCREMENT, 12 | uid INT, 13 | paper_name nchar(100) NOT NULL UNIQUE, 14 | publish_time DATETIME NOT NULL DEFAULT NOW(), 15 | cutoff_time DATETIME NOT NULL DEFAULT ADDDATE(NOW(), 3), 16 | 17 | CONSTRAINT reference_of_ownerid FOREIGN KEY(uid) REFERENCES q_user(uid), 18 | CONSTRAINT unique_paper_name_per_user UNIQUE (uid, paper_name) 19 | ); 20 | CREATE TABLE q_question ( 21 | questionid INT PRIMARY KEY AUTO_INCREMENT, 22 | paperid INT, 23 | question_type CHAR(10) NOT NULL, 24 | question nvarchar(1000) NOT NULL, 25 | 26 | CONSTRAINT refenence_of_paperid FOREIGN KEY (paperid) REFERENCES q_paper(paperid), 27 | CONSTRAINT almost_unique_question_per_paper UNIQUE (paperid, question_type, question) 28 | ); 29 | CREATE TABLE q_selection ( 30 | selectionid INT PRIMARY KEY AUTO_INCREMENT, 31 | questionid INT, 32 | selection_describe TEXT NOT NULL, 33 | 34 | CONSTRAINT reference_of_questionid FOREIGN KEY (questionid) REFERENCES q_question(questionid) 35 | ); 36 | CREATE TABLE q_answer ( 37 | answerid INT PRIMARY KEY AUTO_INCREMENT, 38 | questionid INT, 39 | answer TEXT NOT NULL, 40 | respondent INT, 41 | 42 | CONSTRAINT references_of_questionid FOREIGN KEY (questionid) REFERENCES q_question(questionid), 43 | CONSTRAINT references_of_respondent FOREIGN KEY (respondent) REFERENCES q_user(uid) 44 | ); 45 | CREATE TABLE q_done_answer ( 46 | paperid INT NOT NULL, 47 | uid INT NOT NULL, 48 | 49 | CONSTRAINT reference_of_destination_paperid FOREIGN KEY (paperid) REFERENCES q_paper(paperid), 50 | CONSTRAINT reference_of_user_who_allow FOREIGN KEY (uid) REFERENCES q_user(uid), 51 | CONSTRAINT primarykey_q_allow_answer PRIMARY KEY (paperid, uid) 52 | ); 53 | 54 | 55 | 56 | delimiter // 57 | CREATE PROCEDURE check_user_passwd(IN un CHAR(30), IN p CHAR(32)) 58 | BEGIN 59 | SET @pass = (SELECT passwd FROM q_user WHERE username = un); 60 | IF @pass = MD5(p) THEN 61 | SELECT 1 AS result; 62 | ELSE 63 | SELECT 0 AS result; 64 | END IF; 65 | END 66 | // 67 | delimiter ; 68 | 69 | delimiter // 70 | CREATE PROCEDURE delete_question(IN q_id INT) 71 | BEGIN 72 | DELETE FROM q_answer WHERE questionid = q_id; 73 | DELETE FROM q_selection WHERE questionid = q_id; 74 | DELETE FROM q_question WHERE questionid = q_id; 75 | END 76 | // 77 | delimiter ; 78 | 79 | delimiter // 80 | CREATE PROCEDURE delete_paper(IN p_id INT) 81 | BEGIN 82 | DELETE FROM q_answer WHERE questionid IN ( 83 | SELECT questionid FROM q_question WHERE paperid = p_id 84 | ); 85 | DELETE FROM q_selection WHERE questionid IN ( 86 | SELECT questionid FROM q_question WHERE paperid = p_id 87 | ); 88 | DELETE FROM q_question WHERE paperid = p_id; 89 | DELETE FROM q_allow_answer WHERE paperid = p_id; 90 | DELETE FROM q_paper WHERE paperid = p_id; 91 | END 92 | // 93 | delimiter ; 94 | 95 | delimiter // 96 | CREATE PROCEDURE delete_user(IN un CHAR(30)) 97 | BEGIN 98 | SET @uid = (SELECT uid FROM q_user WHERE username = un); 99 | 100 | DELETE FROM q_answer WHERE questionid IN ( 101 | SELECT questionid FROM q_question WHERE paperid IN ( 102 | SELECT paperid FROM q_paper WHERE uid = @uid 103 | ) 104 | ); 105 | DELETE FROM q_selection WHERE questionid IN ( 106 | SELECT questionid FROM q_question WHERE paperid IN ( 107 | SELECT paperid FROM q_paper WHERE uid = @uid 108 | ) 109 | ); 110 | DELETE FROM q_question WHERE paperid IN ( 111 | SELECT paperid FROM q_paper WHERE uid = @uid 112 | ); 113 | DELETE FROM q_allow_answer WHERE paperid IN ( 114 | SELECT paperid FROM q_paper WHERE uid = @uid 115 | ); 116 | DELETE FROM q_paper WHERE paperid IN ( 117 | SELECT paperid FROM q_paper WHERE uid = @uid 118 | ); 119 | DELETE FROM q_user WHERE uid = @uid; 120 | END // 121 | delimiter ; 122 | /* 123 | delimiter // 124 | CREATE PROCEDURE delete_user(IN un CHAR(30)) 125 | BEGIN 126 | DECLARE u_uid INT; 127 | DECLARE conti INT DEFAULT TRUE; 128 | DECLARE current_paperid INT; 129 | DECLARE cur_paperid CURSOR FOR SELECT paperid FROM q_paper WHERE uid = u_uid; 130 | DECLARE CONTINUE handler FOR NOT FOUND SET @conti = FALSE; 131 | SET u_uid = (SELECT uid FROM q_user WHERE username = un); 132 | 133 | OPEN cur_paperid; 134 | WHILE conti = TRUE DO 135 | FETCH cur_paperid INTO current_paperid; 136 | CALL delete_paper(current_paperid); 137 | END WHILE; 138 | CLOSE cur_paperid; 139 | END 140 | // 141 | delimiter ; 142 | */ 143 | 144 | delimiter // 145 | CREATE TRIGGER encry_passwd BEFORE INSERT 146 | ON q_user FOR EACH ROW 147 | BEGIN 148 | SET new.passwd = MD5(new.passwd); 149 | END 150 | // 151 | delimiter ; 152 | 153 | 154 | delimiter // 155 | CREATE TRIGGER encry_passwd_on_update BEFORE UPDATE 156 | ON q_user FOR EACH ROW 157 | BEGIN 158 | SET new.passwd = MD5(new.passwd); 159 | END 160 | // 161 | delimiter ; -------------------------------------------------------------------------------- /src/dao/PaperDao.java: -------------------------------------------------------------------------------- 1 | package dao; 2 | 3 | import dataobject.*; 4 | import util.*; 5 | import java.sql.Connection; 6 | import java.sql.PreparedStatement; 7 | import java.sql.ResultSet; 8 | import java.sql.SQLException; 9 | import java.sql.Timestamp; 10 | import java.util.ArrayList; 11 | 12 | public class PaperDao { 13 | private Paper paper; 14 | private static Connection connection; 15 | 16 | /* 17 | * init the PaperDao, init the paper inside PaperDao with everythin default 18 | */ 19 | public PaperDao() { 20 | if (connection == null) 21 | connection = Conn.getConnection(); 22 | paper = new Paper(); 23 | } 24 | 25 | public PaperDao(Long paperid) { 26 | if (connection == null) 27 | connection = Conn.getConnection(); 28 | paper = new Paper(); 29 | this.setPaper(paperid); 30 | } 31 | 32 | /* 33 | * init the PaperDao, init the paper inside PaperDao with the paper 34 | * passed in as parameter 35 | */ 36 | public PaperDao(Paper p) { 37 | this(); 38 | this.setPaper(p); 39 | } 40 | 41 | /* 42 | * get the paper inside the PaperDao 43 | */ 44 | public Paper getPaper() { 45 | return this.paper; 46 | } 47 | 48 | /* 49 | * set the paper inside the PaperDao to the paper passed in. 50 | */ 51 | public Paper setPaper(Paper paper) { 52 | return this.paper = paper; 53 | } 54 | 55 | /* 56 | * set the paper inside the PaperDao, using parameter paperid to get paper from database 57 | */ 58 | public Paper setPaper(long paperid) { 59 | PreparedStatement state = null; 60 | ResultSet rs = null; 61 | try { 62 | state = connection.prepareStatement("select paperid, uid, paper_name from q_paper where paperid = ?"); 63 | state.setLong(1, paperid); 64 | rs = state.executeQuery(); 65 | if (rs.next()) { 66 | paper.setPaperid(rs.getLong("paperid")); 67 | paper.setOwnerid(rs.getLong("uid")); 68 | paper.setPapername(rs.getString("paper_name")); 69 | } else { 70 | paper = null; 71 | } 72 | } catch (SQLException e) { 73 | System.out.println("Unknown SQLException!"); 74 | e.printStackTrace(); 75 | System.exit(-5); 76 | } 77 | return this.paper; 78 | } 79 | 80 | // /* 81 | // * add a new paper to database, after adding, the paper inside the PaperDao 82 | // * will be set to the paper passed in as parameter 83 | // */ 84 | // public boolean addPaper(Paper paper) { 85 | // PreparedStatement state = null; 86 | // ResultSet rs = null; 87 | // int result = 0; 88 | // try { 89 | // state = connection.prepareStatement( 90 | // "insert into q_paper (paperid, uid, paper_name, publish_time, cutoff_time) values (default, ?, ?, ?, ?);"); 91 | // state.setLong(1, paper.getOwnerid()); 92 | // state.setString(2, paper.getPapername()); 93 | // state.setTimestamp(3, paper.getPublish_time()); 94 | // state.setTimestamp(4, paper.getCutoff_time()); 95 | // result = state.executeUpdate(); 96 | // if (result == 0) { 97 | // return false; 98 | // } 99 | // 100 | // state = connection.prepareStatement( 101 | // "select paperid from q_paper where uid = ? and paper_name = ? and publish_time = ? and cutoff_time = ?"); 102 | // state.setLong(1, paper.getOwnerid()); 103 | // state.setString(2, paper.getPapername()); 104 | // state.setTimestamp(3, paper.getPublish_time()); 105 | // state.setTimestamp(4, paper.getCutoff_time()); 106 | // rs = state.executeQuery(); 107 | // 108 | // rs.next(); 109 | // paper.setPaperid(rs.getLong("paperid")); 110 | // } catch (SQLException e) { 111 | // System.out.println("Unknown SQLException!"); 112 | // e.printStackTrace(); 113 | // } 114 | // return true; 115 | // } 116 | 117 | /* 118 | * add paper to database, using the paper inside the PaperDao 119 | */ 120 | public boolean addPaper() { 121 | return this.addPaper(paper.getOwnerid(), paper.getPapername(), 122 | paper.getPublish_time(), paper.getCutoff_time()); 123 | } 124 | 125 | /* 126 | * you can just pass the parameters required to add a paper 127 | */ 128 | public boolean addPaper(Long ownerid, String papername, Timestamp publish_time, Timestamp cutoff_time) { 129 | PreparedStatement state = null; 130 | ResultSet rs = null; 131 | int result = 0; 132 | try { 133 | state = connection.prepareStatement( 134 | "insert into q_paper (paperid, uid, paper_name, publish_time, cutoff_time) values (default, ?, ?, ?, ?);"); 135 | state.setLong(1, ownerid); 136 | state.setString(2, papername); 137 | state.setTimestamp(3, publish_time); 138 | state.setTimestamp(4, cutoff_time); 139 | result = state.executeUpdate(); 140 | if (result == 0) { 141 | return false; 142 | } 143 | 144 | state = connection.prepareStatement( 145 | "select paperid from q_paper where uid = ? and paper_name = ? and publish_time = ? and cutoff_time = ?"); 146 | state.setLong(1, ownerid); 147 | state.setString(2, papername); 148 | state.setTimestamp(3, publish_time); 149 | state.setTimestamp(4, cutoff_time); 150 | rs = state.executeQuery(); 151 | 152 | rs.next(); 153 | paper.setPaperid(rs.getLong("paperid")); 154 | paper.setOwnerid(ownerid); 155 | paper.setPapername(papername); 156 | paper.setPublish_time(publish_time); 157 | paper.setCutoff_time(cutoff_time); 158 | } catch (SQLException e) { 159 | System.out.println("Unknown SQLException!"); 160 | e.printStackTrace(); 161 | } 162 | return true; 163 | } 164 | 165 | /* 166 | * you can update new uid, which mean give the owner ship to another user, 167 | * paper name, publish_time and cutoff_time of the Paper 168 | */ 169 | public boolean updatePaper() { 170 | PreparedStatement state = null; 171 | int result = 0; 172 | try { 173 | state = connection.prepareStatement( 174 | "update q_paper set uid = ?, paper_name = ?, publish_time = ?, cutoff_time = ? where paperid = ?;"); 175 | state.setLong(1, paper.getOwnerid()); 176 | state.setString(2, paper.getPapername()); 177 | state.setTimestamp(3, paper.getPublish_time()); 178 | state.setTimestamp(4, paper.getCutoff_time()); 179 | state.setLong(5, paper.getPaperid()); 180 | result = state.executeUpdate(); 181 | } catch (SQLException e) { 182 | result = 0; 183 | e.printStackTrace(); 184 | } 185 | if (result == 0) { 186 | return false; 187 | } else { 188 | return true; 189 | } 190 | } 191 | 192 | /* 193 | * delete paper specific by paperid 194 | */ 195 | public static boolean deletePaper(Long paperid) { 196 | PreparedStatement state = null; 197 | int result = 0; 198 | try { 199 | state = connection.prepareStatement( 200 | "call delete_paper(?);"); 201 | state.setLong(1, paperid); 202 | result = state.executeUpdate(); 203 | } catch (SQLException e) { 204 | result = 0; 205 | e.printStackTrace(); 206 | } 207 | return result != 0; 208 | } 209 | 210 | /* 211 | * get all queston belong to the paper inside the PaperDao 212 | */ 213 | public ArrayList getQuestion() { 214 | ArrayList questions = new ArrayList(); 215 | ResultSet rs = null; 216 | PreparedStatement state = null; 217 | try { 218 | state = connection.prepareStatement( 219 | "select questionid, paperid, question_type, question from q_question where paperid = ?"); 220 | state.setLong(1, paper.getPaperid()); 221 | rs = state.executeQuery(); 222 | while (rs.next()) { 223 | Question que = new Question(); 224 | que.setQuestionid(rs.getLong("questionid")); 225 | que.setPaperid(rs.getLong("paperid")); 226 | que.setQuestion(rs.getString("question")); 227 | que.setType(rs.getString("question_type")); 228 | questions.add(que); 229 | } 230 | } catch (SQLException e) { 231 | System.out.println("Unknown SQLException!"); 232 | e.printStackTrace(); 233 | System.exit(-5); 234 | } 235 | return questions; 236 | } 237 | 238 | /* 239 | * delete done flag in q_done_answer of the user presented by uid to this paper; 240 | */ 241 | public boolean addAllowUser(Long uid) { 242 | PreparedStatement state = null; 243 | boolean addFlag = true; 244 | try { 245 | state = connection.prepareStatement( 246 | "delete from q_done_answer where paperid = ? and uid = ?"); 247 | // "insert into q_allow_answer (paperid, uid) values (?, ?);") 248 | state.setLong(1, paper.getPaperid()); 249 | state.setLong(2, uid); 250 | state.executeQuery(); 251 | } catch (SQLException e) { 252 | addFlag = false; 253 | 254 | System.out.println("UnKnown SQLException!"); 255 | e.printStackTrace(); 256 | System.exit(-12); 257 | } 258 | return addFlag; 259 | } 260 | 261 | /* 262 | * get all user did not do the paper inside the PaperDao 263 | */ 264 | public ArrayList getAllowedUser() { 265 | ArrayList allowedUser = new ArrayList(); 266 | PreparedStatement state = null; 267 | ResultSet rs = null; 268 | try { 269 | state = connection.prepareStatement( 270 | "select username, nickname, uid from q_user " + 271 | "where uid not in (select uid from q_done_answer where paperid = ?) "); 272 | // "select q_user.uid as uid, q_user.username as username, q_user.nickname as nickname " + 273 | // "from q_allow_answer join q_user on q_allow_answer.uid = q_user.uid " + 274 | // "where paperid = ?;"); 275 | state.setLong(1, paper.getPaperid()); 276 | rs = state.executeQuery(); 277 | while (rs.next()) { 278 | User u = new User(); 279 | u.setUsername(rs.getString("username")); 280 | u.setNickname(rs.getString("nickname")); 281 | u.setUid(rs.getLong("uid")); 282 | allowedUser.add(u); 283 | } 284 | } catch (SQLException e) { 285 | System.out.println("UnKnown SQLException!"); 286 | e.printStackTrace(); 287 | System.exit(-13); 288 | } 289 | return allowedUser; 290 | } 291 | } 292 | -------------------------------------------------------------------------------- /src/dao/UserDao.java: -------------------------------------------------------------------------------- 1 | package dao; 2 | 3 | import dataobject.*; 4 | import util.*; 5 | import java.sql.Connection; 6 | import java.sql.PreparedStatement; 7 | import java.sql.ResultSet; 8 | import java.sql.SQLException; 9 | import java.util.ArrayList; 10 | import java.util.Scanner; 11 | 12 | public class UserDao { 13 | private User user; 14 | private static Connection connection; 15 | 16 | /* 17 | * init the UserDao class, and init a blank user inside it. 18 | */ 19 | public UserDao() { 20 | if (connection == null) 21 | connection = Conn.getConnection(); 22 | this.user = new User(); 23 | } 24 | 25 | /* 26 | * init the UserDao class, and init a user with User passed in; 27 | */ 28 | public UserDao(User u) { 29 | if (connection == null) 30 | connection = Conn.getConnection(); 31 | this.setUser(u); 32 | } 33 | 34 | /* 35 | * init the UserDao, using uid to init the user inside; 36 | */ 37 | public UserDao(Long uid) { 38 | if (connection == null) 39 | connection = Conn.getConnection(); 40 | this.user = new User(); 41 | this.setUser(uid); 42 | } 43 | 44 | /* 45 | * init the UserDao, using username to init the user inside 46 | */ 47 | public UserDao(String username) { 48 | if (connection == null) 49 | connection = Conn.getConnection(); 50 | this.user = new User(); 51 | this.setUser(username); 52 | } 53 | 54 | /* 55 | * get the user inside UserDao 56 | */ 57 | public User getUser() { 58 | return this.user; 59 | } 60 | 61 | /* 62 | * set a new user to UserDao 63 | */ 64 | public void setUser(User u) { 65 | this.user = u; 66 | } 67 | 68 | /* 69 | * set a new user with username, the user inside will be blank if failed. 70 | */ 71 | public boolean setUser(String username) { 72 | PreparedStatement state = null; 73 | ResultSet rs = null; 74 | boolean setFlag = false; 75 | try { 76 | state = connection.prepareStatement( 77 | "select uid, username, passwd, nickname, admin from q_user where username = ?;"); 78 | state.setString(1, username); 79 | rs = state.executeQuery(); 80 | 81 | if (rs.next()) { 82 | this.user.setUid(rs.getLong("uid")); 83 | this.user.setUsername(rs.getString("username")); 84 | this.user.setNickname(rs.getString("nickname")); 85 | this.user.setAdmin(rs.getBoolean("admin")); 86 | this.user.setPasswd(rs.getString("passwd")); 87 | setFlag = true; 88 | } else { 89 | setFlag = false; 90 | } 91 | } catch (SQLException e) { 92 | e.printStackTrace(); 93 | } 94 | return setFlag; 95 | } 96 | 97 | /* 98 | * set user by uid 99 | */ 100 | public boolean setUser(long uid) { 101 | PreparedStatement state = null; 102 | ResultSet rs = null; 103 | boolean setFlag = false; 104 | try { 105 | state = connection.prepareStatement( 106 | "select uid, username, passwd, nickname, admin from q_user where uid = ?;"); 107 | state.setLong(1, uid); 108 | rs = state.executeQuery(); 109 | 110 | if (rs.next()) { 111 | this.user.setUid(rs.getLong("uid")); 112 | this.user.setUsername(rs.getString("username")); 113 | this.user.setNickname(rs.getString("nickname")); 114 | this.user.setAdmin(rs.getBoolean("admin")); 115 | this.user.setPasswd(rs.getString("passwd")); 116 | setFlag = true; 117 | } else { 118 | setFlag = false; 119 | } 120 | } catch (SQLException e) { 121 | e.printStackTrace(); 122 | } 123 | return setFlag; 124 | } 125 | 126 | /* 127 | * check if the username and passwd accurate, if accurate, the user inside 128 | * UserDao will be set to the user checked in database 129 | */ 130 | public boolean checkUser(String username, String passwd) { 131 | boolean checkFlag = false; 132 | this.user = new User(); 133 | this.user.setUsername(username); 134 | this.user.setPasswd(passwd); 135 | PreparedStatement state = null; 136 | 137 | try { 138 | state = connection.prepareStatement("call check_user_passwd(?, ?);"); 139 | state.setString(1, this.user.getUsername()); 140 | state.setString(2, passwd); 141 | ResultSet rs = state.executeQuery(); 142 | 143 | if (rs.next() && rs.getLong("result") == 1) { 144 | state = connection.prepareStatement( 145 | "select uid, username, passwd, nickname, admin from q_user where username = ?;"); 146 | state.setString(1, this.user.getUsername()); 147 | rs = state.executeQuery(); 148 | 149 | if (rs.next()) { 150 | this.user.setUid(rs.getLong("uid")); 151 | this.user.setNickname(rs.getString("nickname")); 152 | this.user.setAdmin(rs.getBoolean("admin")); 153 | this.user.setPasswd(rs.getString("passwd")); 154 | checkFlag = true; 155 | } else { 156 | checkFlag = false; 157 | } 158 | } else { 159 | checkFlag = false; 160 | } 161 | } catch (SQLException e) { 162 | System.out.println("Unknown SQLException!"); 163 | e.printStackTrace(); 164 | System.exit(-3); 165 | } 166 | 167 | return checkFlag; 168 | } 169 | 170 | // /* 171 | // * add a new user to database with the user passed in as parameter; 172 | // * if succeed, the user inside UserDao will be set to the user just added. 173 | // * if failed, the user inside will not change. 174 | // */ 175 | // public boolean addUser(User user) { 176 | // boolean addResult = true; 177 | // PreparedStatement test = null; 178 | // PreparedStatement state = null; 179 | // try { 180 | // // if username already exists 181 | // test = connection.prepareStatement("select * from q_user where username = ?"); 182 | // test.setString(1, user.getUsername()); 183 | // if (test.executeQuery().next()) { 184 | // addResult = false; 185 | // } else { 186 | // state = connection.prepareStatement( 187 | // "insert into q_user (uid, username, passwd, nickname, admin) values (default, ?, ?, ?, ?);"); 188 | // state.setString(1, user.getUsername()); 189 | // state.setString(2, user.getPasswd()); 190 | // state.setString(3, user.getNickname()); 191 | // state.setBoolean(4, user.isAdmin()); 192 | // state.executeUpdate(); 193 | // 194 | // // reset uid and other thing; 195 | // this.checkUser(user.getUsername(), user.getPasswd()); 196 | // addResult = true; 197 | // } 198 | // } catch (SQLException e) { 199 | // System.out.println("Unknown SQLException!"); 200 | // e.printStackTrace(); 201 | // System.exit(-4); 202 | // } 203 | // return addResult; 204 | // } 205 | 206 | /* 207 | * add a new user to database, using the user inside UserDao. 208 | * if succeed, the user inside UserDao will be set to the user just added. 209 | * if failed, the user inside will not change. 210 | */ 211 | public boolean addUser() { 212 | return this.addUser(user.getUsername(), user.getPasswd(), user.getNickname(), 213 | user.isAdmin()); 214 | } 215 | 216 | /* 217 | * you can just pass username, password, nickname and boolean admin as parameter; 218 | */ 219 | public boolean addUser(String username, String passwd, String nickname, boolean admin) { 220 | boolean addResult = true; 221 | PreparedStatement test = null; 222 | PreparedStatement state = null; 223 | try { 224 | // if username already exists 225 | test = connection.prepareStatement("select * from q_user where username = ?"); 226 | test.setString(1, user.getUsername()); 227 | if (test.executeQuery().next()) { 228 | addResult = false; 229 | } else { 230 | state = connection.prepareStatement( 231 | "insert into q_user (uid, username, passwd, nickname, admin) values (default, ?, ?, ?, ?);"); 232 | state.setString(1, username); 233 | state.setString(2, passwd); 234 | state.setString(3, nickname); 235 | state.setBoolean(4, admin); 236 | state.executeUpdate(); 237 | 238 | // reset uid and other thing; 239 | this.checkUser(username, passwd); 240 | addResult = true; 241 | } 242 | } catch (SQLException e) { 243 | System.out.println("Unknown SQLException!"); 244 | e.printStackTrace(); 245 | System.exit(-4); 246 | } 247 | return addResult; 248 | } 249 | 250 | /* 251 | * update the user's information, using the user inside UserDao, 252 | * you can update everything except the uid. 253 | */ 254 | public boolean updateUser() { 255 | PreparedStatement state = null; 256 | long result = 0; 257 | try { 258 | state = connection.prepareStatement("update q_user set username = ?, admin = ?, passwd = ?, nickname = ? where uid = ?"); 259 | state.setString(1, user.getUsername()); 260 | state.setBoolean(2, user.isAdmin()); 261 | state.setString(3, user.getPasswd()); 262 | state.setString(4, user.getNickname()); 263 | state.setLong(5, user.getUid()); 264 | result = state.executeUpdate(); 265 | } catch (SQLException e) { 266 | System.out.println("update user failed!"); 267 | e.printStackTrace(); 268 | } 269 | if (result == 0) { 270 | return false; 271 | } else { 272 | return true; 273 | } 274 | } 275 | 276 | /* 277 | * delete the user specific by username in database; 278 | */ 279 | public boolean deleteUser(String username) { 280 | PreparedStatement state = null; 281 | int result = 0; 282 | try { 283 | state = connection.prepareStatement( 284 | "call delete_user(?);"); 285 | state.setString(1, username); 286 | result = state.executeUpdate(); 287 | } catch (SQLException e) { 288 | result = 0; 289 | e.printStackTrace(); 290 | } 291 | return result != 0; 292 | } 293 | 294 | public static ArrayList getAllPapers() { 295 | ArrayList allPapers = new ArrayList(); 296 | PreparedStatement state = null; 297 | ResultSet rs = null; 298 | try { 299 | state = connection.prepareStatement( 300 | "select paperid, uid, paper_name, publish_time, cutoff_time from q_paper;"); 301 | rs = state.executeQuery(); 302 | while (rs.next()) { 303 | Paper paper = new Paper(); 304 | paper.setPaperid(rs.getLong("paperid")); 305 | paper.setPapername(rs.getString("paper_name")); 306 | paper.setOwnerid(rs.getLong("uid")); 307 | paper.setPublish_time(rs.getTimestamp("publish_time")); 308 | paper.setCutoff_time(rs.getTimestamp("cutoff_time")); 309 | allPapers.add(paper); 310 | } 311 | } catch (SQLException e) { 312 | System.out.println("Unknown SQLException!"); 313 | e.printStackTrace(); 314 | System.exit(-1); 315 | } 316 | return allPapers; 317 | } 318 | 319 | /* 320 | * retrun all papers own to the user inside UserDao. 321 | */ 322 | public ArrayList getOwnPapers() { 323 | ArrayList ownPapers = new ArrayList(); 324 | PreparedStatement state = null; 325 | ResultSet rs = null; 326 | try { 327 | state = connection.prepareStatement( 328 | "select paperid, paper_name, publish_time, cutoff_time from q_paper where uid = ?"); 329 | state.setLong(1, user.getUid()); 330 | rs = state.executeQuery(); 331 | while (rs.next()) { 332 | Paper paper = new Paper(); 333 | paper.setPaperid(rs.getLong("paperid")); 334 | paper.setPapername(rs.getString("paper_name")); 335 | paper.setOwnerid(user.getUid()); 336 | paper.setPublish_time(rs.getTimestamp("publish_time")); 337 | paper.setCutoff_time(rs.getTimestamp("cutoff_time")); 338 | ownPapers.add(paper); 339 | } 340 | } catch (SQLException e) { 341 | System.out.println("Unknown SQLException!"); 342 | e.printStackTrace(); 343 | System.exit(-1); 344 | } 345 | return ownPapers; 346 | } 347 | 348 | /* 349 | * return all papers allowed to do by the user inside UserDao, 350 | * means the papaer not allowed to do on the current time will not be shown 351 | */ 352 | public ArrayList getAllowedPaper() { 353 | PreparedStatement state = null; 354 | ArrayList allowedPaper = new ArrayList(); 355 | ResultSet rs = null; 356 | try { 357 | state = connection.prepareStatement( 358 | "select paperid, uid, paper_name, publish_time, cutoff_time " + 359 | "from q_paper where paperid not in " + 360 | "(select paperid from q_done_answer where uid = ?) " + 361 | " and NOW() between publish_time and cutoff_time;"); 362 | // "select q_paper.paperid as paperid, q_paper.uid as uid, paper_name, publish_time, cutoff_time " + 363 | // "from q_paper join q_allow_answer on q_paper.paperid = q_allow_answer.paperid " + 364 | // "where q_allow_answer.uid = ? and NOW() between publish_time and cutoff_time;"); 365 | state.setLong(1, user.getUid()); 366 | rs = state.executeQuery(); 367 | while (rs.next()) { 368 | Paper p = new Paper(); 369 | p.setPaperid(rs.getLong("paperid")); 370 | p.setOwnerid(rs.getLong("uid")); 371 | p.setPapername(rs.getString("paper_name")); 372 | p.setPublish_time(rs.getTimestamp("publish_time")); 373 | p.setCutoff_time(rs.getTimestamp("cutoff_time")); 374 | allowedPaper.add(p); 375 | } 376 | } catch (SQLException e) { 377 | System.out.println("Unknown SQLException!"); 378 | e.printStackTrace(); 379 | System.exit(-11); 380 | } 381 | 382 | return allowedPaper; 383 | } 384 | 385 | public static void main(String[] args) { 386 | Scanner input = new Scanner(System.in); 387 | 388 | // test all the methods in UserDao 389 | UserDao ud1 = new UserDao(); 390 | ud1.setUser(1); 391 | // ud1.addUser("add_user1", "test", "add_user1", false); 392 | // ud1.getUser().setNickname("add_user1_after_update"); 393 | // ud1.getUser().setAdmin(true); 394 | // ud1.updateUser(); 395 | 396 | PaperDao pd1 = new PaperDao(); 397 | pd1.addPaper(ud1.getUser().getUid(), "added_paper_to_added_user1", 398 | java.sql.Timestamp.valueOf("2019-06-01 00:00:00"), 399 | java.sql.Timestamp.valueOf("2019-06-20 00:00:00")); 400 | pd1.getPaper().setPapername("updated_paper_to_added_user1"); 401 | pd1.updatePaper(); 402 | pd1.addAllowUser(new UserDao("leafee").getUser().getUid()); 403 | QuestionDao qd1 = new QuestionDao(); 404 | QuestionDao qd2 = new QuestionDao(); 405 | 406 | qd1.addQuestion(pd1.getPaper().getPaperid(), "radio", "added_question_radio_to_added_paper1"); 407 | qd2.addQuestion(pd1.getPaper().getPaperid(), "check", "added_question_check_to_added_paper2"); 408 | qd1.addSelection("selection11_of_question1"); 409 | qd1.addSelection("selection12_of_question1"); 410 | qd2.addSelection("selection21_of_question2"); 411 | qd2.addSelection("selection22_of_question2"); 412 | qd1.getQuestion().setQuestion("updated_question_radio_to_added_paper1"); 413 | qd1.updateQuestion(); 414 | 415 | ArrayList selOfq1 = qd1.getSelection(); 416 | ArrayList selOfq2 = qd2.getSelection(); 417 | qd1.addAnswer(String.valueOf(selOfq1.get(0).getSelectionid()), new UserDao("leafee").getUser().getUid()); 418 | qd2.addAnswer(String.valueOf(selOfq2.get(0).getSelectionid()), new UserDao("leafee").getUser().getUid()); 419 | qd2.addAnswer(String.valueOf(selOfq2.get(1).getSelectionid()), new UserDao("leafee").getUser().getUid()); 420 | 421 | ArrayList arrPaper = ud1.getOwnPapers(); 422 | for (int i = 0; i < arrPaper.size(); ++i) { 423 | System.out.println(arrPaper.get(i).getPapername()); 424 | } 425 | System.out.println("============"); 426 | arrPaper = ud1.getAllowedPaper(); 427 | for (int i = 0; i < arrPaper.size(); ++i) { 428 | System.out.println(arrPaper.get(i).getPapername()); 429 | } 430 | System.out.println("============"); 431 | arrPaper = UserDao.getAllPapers(); 432 | for (int i = 0; i < arrPaper.size(); ++i) { 433 | System.out.println(arrPaper.get(i).getPapername()); 434 | } 435 | System.out.println("============"); 436 | System.out.println("============"); 437 | System.out.println("============"); 438 | 439 | for (User u : pd1.getAllowedUser()) { 440 | System.out.println(u.getNickname()); 441 | } 442 | System.out.println("============"); 443 | 444 | // for (Paper p : arrPaper) { 445 | // for (Question q : new PaperDao(p.getPaperid()).getQuestion()) { 446 | // System.out.println(q.getQuestion()); 447 | // QuestionDao qdd = new QuestionDao(q.getQuestionid()); 448 | // // for (Selection s : qdd.getSelection()) { 449 | // // System.out.println(s.getSelection_describe()); 450 | // // } 451 | // // for (Answer a : qdd.getAllAnswers()) { 452 | // // System.out.println(a.getAnswer()); 453 | // // } 454 | // TreeMap> qatm = qdd.analyzeSelection(); 455 | // Iterator>> it = qatm.entrySet().iterator(); 456 | // while (it.hasNext()) { 457 | // Map.Entry> mapIt = it.next(); 458 | // System.out.print(mapIt.getKey().getSelection_describe() + " ::: "); 459 | // System.out.println(mapIt.getValue().size()); 460 | // } 461 | // 462 | // ArrayList speAns = qdd.getSpecificAnswers(new UserDao("leafee").getUser().getUid()); 463 | // System.out.println("The User " + ud1.getUser().getNickname() + " 's Answer:"); 464 | // for (Answer a : speAns) { 465 | // System.out.println(a.getAnswer()); 466 | // } 467 | // 468 | // } 469 | // } 470 | System.out.println("============="); 471 | 472 | for (Paper p : arrPaper) { 473 | for (Question q : new PaperDao(p.getPaperid()).getQuestion()) { 474 | QuestionDao qdd = new QuestionDao(q.getQuestionid()); 475 | Selection s = qdd.getSelection().get(0); 476 | System.out.println(s.getSelection_describe()); 477 | QuestionDao.updateSelection(s.getSelectionid(), "Updated Selection"); 478 | QuestionDao.deleteSelection(s.getSelectionid()); 479 | break; 480 | } 481 | break; 482 | } 483 | QuestionDao.deleteQuestion(qd1.getQuestion().getQuestionid()); 484 | PaperDao.deletePaper(pd1.getPaper().getPaperid()); 485 | ud1.deleteUser("add_user1"); 486 | input.close(); 487 | } 488 | } 489 | -------------------------------------------------------------------------------- /src/dao/QuestionDao.java: -------------------------------------------------------------------------------- 1 | package dao; 2 | 3 | import dataobject.*; 4 | import util.*; 5 | import java.sql.Connection; 6 | import java.sql.SQLException; 7 | import java.sql.PreparedStatement; 8 | import java.sql.ResultSet; 9 | import java.util.ArrayList; 10 | import java.util.Comparator; 11 | import java.util.Iterator; 12 | import java.util.Map; 13 | import java.util.Map.Entry; 14 | import java.util.TreeMap; 15 | 16 | public class QuestionDao { 17 | private Question question; 18 | private static Connection connection; 19 | 20 | /* 21 | * init the QuestionDao, with initializing the question inside it with all value default 22 | */ 23 | public QuestionDao() { 24 | if (connection == null) 25 | connection = Conn.getConnection(); 26 | question = new Question(); 27 | } 28 | 29 | public QuestionDao(Long questionid) { 30 | if (connection == null) 31 | connection = Conn.getConnection(); 32 | question = new Question(); 33 | this.setQuestion(questionid); 34 | } 35 | 36 | /* 37 | * init the QuestionDao, with initializeing the question inside it with @parameter q 38 | */ 39 | public QuestionDao(Question q) { 40 | this(); 41 | this.setQuestion(q); 42 | } 43 | 44 | /* 45 | * get the question inside the QuestionDao 46 | */ 47 | public Question getQuestion() { 48 | return this.question; 49 | } 50 | 51 | /* 52 | * set the question inside the QuestionDao to @parameter quetion 53 | */ 54 | public Question setQuestion(Question question) { 55 | return this.question = question; 56 | } 57 | 58 | /* 59 | * set the question inside the QuestionDao to question found from 60 | * database by @parameter questionid 61 | */ 62 | public Question setQuestion(Long questionid) { 63 | PreparedStatement state = null; 64 | ResultSet rs = null; 65 | try { 66 | state = connection.prepareStatement( 67 | "select questionid, paperid, question_type, question from q_question where questionid = ?"); 68 | state.setLong(1, questionid); 69 | rs = state.executeQuery(); 70 | if (rs.next()) { 71 | question.setQuestionid(rs.getLong("questionid")); 72 | question.setPaperid(rs.getLong("paperid")); 73 | question.setQuestion(rs.getString("question")); 74 | question.setType(rs.getString("question_type")); 75 | } else { 76 | question = null; 77 | } 78 | } catch (SQLException e) { 79 | e.printStackTrace(); 80 | System.exit(-6); 81 | } 82 | return this.question; 83 | } 84 | 85 | // /* 86 | // * add a new question to database, the specified questionid property of question 87 | // * will be ignored, after adding, the question inside the QuestionDao will be set 88 | // * to @parameter question, with accurate questionid in database; 89 | // * take care that you **can not add two question be same on question content and queston_type 90 | // * under the same paper**; 91 | // */ 92 | // public boolean addQuestion(Question question) { 93 | // PreparedStatement state = null; 94 | // ResultSet rs = null; 95 | // int result = 0; 96 | // try { 97 | // state = connection.prepareStatement( 98 | // "insert into q_question (questionid, paperid, question_type, question) values (?, ?, ?, ?);"); 99 | // state.setNull(1, java.sql.Types.INTEGER); 100 | // state.setLong(2, question.getPaperid()); 101 | // state.setString(3, question.getType()); 102 | // state.setString(4, question.getQuestion()); 103 | // result = state.executeUpdate(); 104 | // if (result == 0) { 105 | // return false; 106 | // } 107 | // 108 | // // get the question id from database; 109 | // state = connection.prepareStatement( 110 | // "select questionid from q_question where paperid = ? and question_type = ? and question = ?;"); 111 | // state.setLong(1, question.getPaperid()); 112 | // state.setString(2, question.getType()); 113 | // state.setString(3, question.getQuestion()); 114 | // rs = state.executeQuery(); 115 | // 116 | // rs.next(); 117 | // question.setQuestionid(rs.getLong("questionid")); 118 | // this.question = question; 119 | // } catch (Exception e) { 120 | // e.printStackTrace(); 121 | // System.exit(-7); 122 | // } 123 | // return true; 124 | // } 125 | 126 | /* 127 | * add a new question to database, using the question inside the QuestionDao 128 | */ 129 | public boolean addQuestion() { 130 | return this.addQuestion(question.getPaperid(), question.getType(), 131 | question.getQuestion()); 132 | } 133 | 134 | /* 135 | * you can just pass paperid, question type, question content as parameters 136 | */ 137 | public boolean addQuestion(Long paperid, String questionType, String questionContent) { 138 | PreparedStatement state = null; 139 | ResultSet rs = null; 140 | int result = 0; 141 | try { 142 | state = connection.prepareStatement( 143 | "insert into q_question (questionid, paperid, question_type, question) values (?, ?, ?, ?);"); 144 | state.setNull(1, java.sql.Types.INTEGER); 145 | state.setLong(2, paperid); 146 | state.setString(3, questionType); 147 | state.setString(4, questionContent); 148 | result = state.executeUpdate(); 149 | if (result == 0) { 150 | return false; 151 | } 152 | 153 | // get the question id from database; 154 | state = connection.prepareStatement( 155 | "select questionid from q_question where paperid = ? and question_type = ? and question = ?;"); 156 | state.setLong(1, paperid); 157 | state.setString(2, questionType); 158 | state.setString(3, questionContent); 159 | rs = state.executeQuery(); 160 | 161 | rs.next(); 162 | question.setQuestionid(rs.getLong("questionid")); 163 | question.setPaperid(paperid); 164 | question.setType(questionType); 165 | question.setQuestion(questionContent); 166 | } catch (Exception e) { 167 | e.printStackTrace(); 168 | System.exit(-7); 169 | } 170 | return true; 171 | } 172 | 173 | /* 174 | * you can update the question_type and the question content, 175 | * do not change anything else; 176 | */ 177 | public boolean updateQuestion() { 178 | PreparedStatement state = null; 179 | int result = 0; 180 | try { 181 | state = connection.prepareStatement( 182 | "update q_question set question_type = ?, question = ? where questionid = ?;"); 183 | state.setString(1, question.getType()); 184 | state.setString(2, question.getQuestion()); 185 | state.setLong(3, question.getQuestionid()); 186 | result = state.executeUpdate(); 187 | } catch (SQLException e) { 188 | result = 0; 189 | e.printStackTrace(); 190 | } 191 | return result != 0; 192 | } 193 | 194 | /* 195 | * delete question specific by quiestionid 196 | */ 197 | public static boolean deleteQuestion(Long questionid) { 198 | PreparedStatement state = null; 199 | int result = 0; 200 | try { 201 | state = connection.prepareStatement( 202 | "call delete_question(?);"); 203 | state.setLong(1, questionid); 204 | result = state.executeUpdate(); 205 | } catch (SQLException e) { 206 | result = 0; 207 | e.printStackTrace(); 208 | } 209 | return result != 0; 210 | } 211 | 212 | /* 213 | * answer the question, add an answer to database, 214 | * you can just pass answer content and the uid of the user 215 | * who did this answer. 216 | */ 217 | public boolean addAnswer(String answerContent, Long respondentId) { 218 | PreparedStatement state = null; 219 | PreparedStatement stCheckDone = null; 220 | ResultSet rs = null; 221 | boolean addFlag = true; 222 | try { 223 | state = connection.prepareStatement( 224 | "insert into q_answer (questionid, answer, respondent) values (?, ?, ?);"); 225 | state.setLong(1, question.getQuestionid()); 226 | state.setString(2, answerContent); 227 | state.setLong(3, respondentId); 228 | if (state.executeUpdate() == 0) { 229 | addFlag = false; 230 | } 231 | 232 | 233 | stCheckDone = connection.prepareStatement( 234 | "select count(*) as cnt from q_done_answer where uid = ? and paperid = ?;"); 235 | stCheckDone.setLong(1, respondentId); 236 | stCheckDone.setLong(2, question.getPaperid()); 237 | rs = stCheckDone.executeQuery(); 238 | rs.next(); 239 | if (rs.getLong("cnt") == 0) { 240 | stCheckDone = connection.prepareStatement( 241 | "insert into q_done_answer (uid, paperid) values (?, ?);"); 242 | stCheckDone.setLong(1, respondentId); 243 | stCheckDone.setLong(2, question.getPaperid()); 244 | stCheckDone.executeUpdate(); 245 | } 246 | } catch (SQLException e) { 247 | addFlag = false; 248 | 249 | System.out.println("Unknown SQLException"); 250 | e.printStackTrace(); 251 | System.exit(-12); 252 | } 253 | return addFlag; 254 | } 255 | 256 | /* 257 | * get all answers belong to this question. 258 | */ 259 | public ArrayList getAllAnswers() { 260 | ArrayList answers = new ArrayList(); 261 | ResultSet rs = null; 262 | PreparedStatement state = null; 263 | try { 264 | state = connection.prepareStatement( 265 | "select answerid, questionid, answer, respondent from q_answer where questionid = ?;"); 266 | state.setLong(1, question.getQuestionid()); 267 | rs = state.executeQuery(); 268 | while (rs.next()) { 269 | Answer ans = new Answer(); 270 | ans.setAnswerid(rs.getLong("answerid")); 271 | ans.setQuestionid(rs.getLong("questionid")); 272 | ans.setAnswer(rs.getString("answer")); 273 | ans.setRespondent(rs.getLong("respondent")); 274 | answers.add(ans); 275 | } 276 | } catch (SQLException e) { 277 | System.out.println("Unknown SQLException!"); 278 | e.printStackTrace(); 279 | System.exit(-8); 280 | } 281 | return answers; 282 | } 283 | 284 | /* 285 | * get the specific answers belong to the question inside QuestionDao, 286 | * means you can get the specific user's answer. 287 | */ 288 | public ArrayList getSpecificAnswers(Long respondentId) { 289 | ArrayList answers = new ArrayList(); 290 | ResultSet rs = null; 291 | PreparedStatement state = null; 292 | try { 293 | state = connection.prepareStatement( 294 | "select answerid, questionid, answer from q_answer where questionid = ? and respondent = ?;"); 295 | state.setLong(1, question.getQuestionid()); 296 | state.setLong(2, respondentId); 297 | rs = state.executeQuery(); 298 | while (rs.next()) { 299 | Answer ans = new Answer(); 300 | ans.setAnswerid(rs.getLong("answerid")); 301 | ans.setQuestionid(rs.getLong("questionid")); 302 | ans.setAnswer(rs.getString("answer")); 303 | ans.setRespondent(respondentId); 304 | answers.add(ans); 305 | } 306 | } catch (SQLException e) { 307 | System.out.println("Unknown SQLException!"); 308 | e.printStackTrace(); 309 | System.exit(-8); 310 | } 311 | return answers; 312 | } 313 | 314 | /* 315 | * you can call this function only when the question is 'radio'(single select) or 316 | * 'check'(multiple select), if not, the result will be null or undefined. 317 | */ 318 | public TreeMap analyzeSelection() { 319 | TreeMap> tempRes = 320 | new TreeMap>(new Comparator() { 321 | public int compare(Selection a, Selection b) { 322 | return (int)(a.getSelectionid() - b.getSelectionid()); 323 | } 324 | }); 325 | ArrayList selections = this.getSelection(); 326 | ArrayList answers = this.getAllAnswers(); 327 | for (int i = 0; i < selections.size(); ++i) { 328 | tempRes.put(selections.get(i), new ArrayList()); 329 | } 330 | try { 331 | for (int i = 0; i < answers.size(); ++i) { 332 | tempRes.get(Selection.parseSelection( 333 | Long.parseLong(answers.get(i).getAnswer()) 334 | )).add(answers.get(i)); 335 | } 336 | } catch (NumberFormatException e) { 337 | tempRes = null; 338 | e.printStackTrace(); 339 | return null; 340 | } 341 | TreeMap anaResult = 342 | new TreeMap(new Comparator() { 343 | public int compare(Selection a, Selection b) { 344 | return (int)(a.getSelectionid() - b.getSelectionid()); 345 | } 346 | }); 347 | Iterator>> it = tempRes.entrySet().iterator(); 348 | while (it.hasNext()) { 349 | Map.Entry> mapIt = it.next(); 350 | anaResult.put(mapIt.getKey(), mapIt.getValue().size()); 351 | } 352 | return anaResult; 353 | } 354 | 355 | /* 356 | * get all selection for this question, 357 | * take care you should not use this function if this question with a random text answer. 358 | */ 359 | public ArrayList getSelection() { 360 | ArrayList selections = new ArrayList(); 361 | ResultSet rs = null; 362 | PreparedStatement state = null; 363 | try { 364 | state = connection.prepareStatement( 365 | "select selectionid, questionid, selection_describe from q_selection where questionid = ?"); 366 | state.setLong(1, question.getQuestionid()); 367 | rs = state.executeQuery(); 368 | while (rs.next()) { 369 | Selection sele = new Selection(); 370 | sele.setSelectionid(rs.getLong("selectionid")); 371 | sele.setQuestionid(rs.getLong("questionid")); 372 | sele.setSelection_describe(rs.getString("selection_describe")); 373 | selections.add(sele); 374 | } 375 | } catch (SQLException e) { 376 | System.out.println("Unknown SQLException!"); 377 | e.printStackTrace(); 378 | System.exit(-9); 379 | } 380 | return selections; 381 | } 382 | 383 | // /* 384 | // * add a selection to the question inside the QuestionDao 385 | // */ 386 | // public boolean addSelection(Selection selection) { 387 | // PreparedStatement state = null; 388 | // boolean addFlag = true; 389 | // try { 390 | // state = connection.prepareStatement( 391 | // "insert into q_selection (questionid, selection_describe) values (?, ?);"); 392 | // state.setLong(1, selection.getQuestionid()); 393 | // state.setString(2, selection.getSelection_describe()); 394 | // if (state.executeUpdate() == 0) { 395 | // addFlag = false; 396 | // } 397 | // } catch (SQLException e) { 398 | // addFlag = false; 399 | // 400 | // System.out.println("Unknown SQLException"); 401 | // e.printStackTrace(); 402 | // System.exit(-12); 403 | // } 404 | // return addFlag; 405 | // } 406 | 407 | /* 408 | * add a selection to the question inside the QuestionDao, 409 | * you can just pass a String as parameter 410 | */ 411 | public boolean addSelection(String selectionDesc) { 412 | PreparedStatement state = null; 413 | boolean addFlag = true; 414 | try { 415 | state = connection.prepareStatement( 416 | "insert into q_selection (questionid, selection_describe) values (?, ?);"); 417 | state.setLong(1, question.getQuestionid()); 418 | state.setString(2, selectionDesc); 419 | if (state.executeUpdate() == 0) { 420 | addFlag = false; 421 | } 422 | } catch (SQLException e) { 423 | addFlag = false; 424 | 425 | System.out.println("Unknown SQLException"); 426 | e.printStackTrace(); 427 | System.exit(-12); 428 | } 429 | return addFlag; 430 | } 431 | 432 | // /* 433 | // * update the selection with the same selectionid with @parameter selection 434 | // */ 435 | // public boolean updateSelection(Selection selection) { 436 | // PreparedStatement state = null; 437 | // int result = 0; 438 | // try { 439 | // state = connection.prepareStatement( 440 | // "update q_selection set selection_describe = ?, questionid = ? where selectionid = ?;"); 441 | // state.setString(1, selection.getSelection_describe()); 442 | // state.setLong(2, selection.getSelectionid()); 443 | // state.setLong(3, selection.getSelectionid()); 444 | // result = state.executeUpdate(); 445 | // } catch (SQLException e) { 446 | // e.printStackTrace(); 447 | // } 448 | // if (result == 0) { 449 | // return false; 450 | // } else { 451 | // return true; 452 | // } 453 | // } 454 | 455 | /* 456 | * update the selection describe 457 | */ 458 | public static boolean updateSelection(Long selectionid, String selectionDescribe) { 459 | PreparedStatement state = null; 460 | int result = 0; 461 | try { 462 | state = connection.prepareStatement( 463 | "update q_selection set selection_describe = ? where selectionid = ?;"); 464 | state.setString(1, selectionDescribe); 465 | state.setLong(2, selectionid); 466 | result = state.executeUpdate(); 467 | } catch (SQLException e) { 468 | result = 0; 469 | e.printStackTrace(); 470 | } 471 | return result != 0; 472 | } 473 | 474 | /* 475 | * delete the selection specific by selection id 476 | */ 477 | public static boolean deleteSelection(Long selectionid) { 478 | PreparedStatement state = null; 479 | int result = 0; 480 | try { 481 | state = connection.prepareStatement( 482 | "delete from q_selection where selectionid = ?;"); 483 | state.setLong(1, selectionid); 484 | result = state.executeUpdate(); 485 | } catch (SQLException e) { 486 | result = 0; 487 | e.printStackTrace(); 488 | } 489 | return result != 0; 490 | } 491 | 492 | // /* 493 | // * answer the question, add an answer to database, 494 | // * the property answerid will be ignored 495 | // */ 496 | // public boolean addAnswer(Answer ans) { 497 | // PreparedStatement state = null; 498 | // boolean addFlag = true; 499 | // try { 500 | // state = connection.prepareStatement( 501 | // "insert into q_answer (questionid, answer, respondent) values (?, ?, ?);"); 502 | // state.setLong(1, ans.getQuestionid()); 503 | // state.setString(2, ans.getAnswer()); 504 | // state.setLong(3, ans.getRespondent()); 505 | // if (state.executeUpdate() == 0) { 506 | // addFlag = false; 507 | // } 508 | // } catch (SQLException e) { 509 | // addFlag = false; 510 | // 511 | // System.out.println("Unknown SQLException"); 512 | // e.printStackTrace(); 513 | // System.exit(-12); 514 | // } 515 | // return addFlag; 516 | // } 517 | } --------------------------------------------------------------------------------