From 73c60d9c528540ea533eb0f6ce5ab093064afe5c Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Thu, 15 Feb 2024 13:28:33 +0100 Subject: [PATCH] New servlet containing all survey results --- Makefile | 1 + database.sql | 49 +++++++++++------ src/main/java/servlet/Database.java | 30 ++++++++++- src/main/java/servlet/FormGenerator.java | 13 +++++ src/main/java/servlet/Results.java | 67 ++++++++++++++++++++++++ src/main/java/servlet/Survey.java | 6 ++- src/main/java/servlet/SurveyResult.java | 36 +++++++++++++ 7 files changed, 183 insertions(+), 19 deletions(-) create mode 100644 src/main/java/servlet/Results.java create mode 100644 src/main/java/servlet/SurveyResult.java diff --git a/Makefile b/Makefile index b69a45b..34899d3 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,7 @@ dumptables: sqlite3 $(DB_NAME) "SELECT * FROM Respondents;" sqlite3 $(DB_NAME) "SELECT * FROM Answers;" sqlite3 $(DB_NAME) "SELECT * FROM ProjectDetails;" + sqlite3 $(DB_NAME) "SELECT * FROM SurveyResults;" # Phony targets .PHONY: all init_db clean diff --git a/database.sql b/database.sql index 0f34e15..5339b1a 100644 --- a/database.sql +++ b/database.sql @@ -1,18 +1,35 @@ -CREATE TABLE IF NOT EXISTS Respondents ( - name VARCHAR(255) PRIMARY KEY -); +CREATE TABLE + IF NOT EXISTS Respondents (name VARCHAR(255) PRIMARY KEY); -CREATE TABLE IF NOT EXISTS Answers ( - answerOne INT, - answerTwo INT, - answerThree INT, - answerFour INT, - name VARCHAR(255), - FOREIGN KEY (name) REFERENCES Respondents(name) -); +CREATE TABLE + IF NOT EXISTS Answers ( + answerOne INT, + answerTwo INT, + answerThree INT, + answerFour INT, + name VARCHAR(255) NOT NULL, + projectName VARCHAR(255) NOT NULL, + FOREIGN KEY (name) REFERENCES Respondents (name), + FOREIGN KEY (projectName) REFERENCES ProjectDetails (projectName) + ); -CREATE TABLE IF NOT EXISTS ProjectDetails ( - projectID INT PRIMARY KEY, - projectName VARCHAR(255), - projectDescription TEXT -); \ No newline at end of file +CREATE TABLE + IF NOT EXISTS ProjectDetails ( + projectName VARCHAR(255) PRIMARY KEY, + projectDescription TEXT + ); + +CREATE VIEW + IF NOT EXISTS SurveyResults AS +SELECT + Respondents.name, + Answers.answerOne, + Answers.answerTwo, + Answers.answerThree, + Answers.answerFour, + Answers.projectName, + ProjectDetails.projectDescription +FROM + Respondents + JOIN Answers ON Respondents.name = Answers.name + JOIN ProjectDetails ON Answers.projectName = ProjectDetails.projectName; \ No newline at end of file diff --git a/src/main/java/servlet/Database.java b/src/main/java/servlet/Database.java index 1a3c4a5..104d3c2 100644 --- a/src/main/java/servlet/Database.java +++ b/src/main/java/servlet/Database.java @@ -66,8 +66,8 @@ public class Database implements AutoCloseable { } } - public boolean addAnswer(String name, String answerOne, String answerTwo, String answerThree, String answerFour) { - String sql = "insert into Answers (answerOne, answerTwo, answerThree, answerFour, name) values(?,?,?,?,?)"; + public boolean addAnswer(String name, String projectName, String answerOne, String answerTwo, String answerThree, String answerFour) { + String sql = "insert into Answers (answerOne, answerTwo, answerThree, answerFour, name, projectName) values(?,?,?,?,?,?)"; try (PreparedStatement ps = conn.prepareStatement(sql)) { ps.setString(1, answerOne); @@ -75,6 +75,7 @@ public class Database implements AutoCloseable { ps.setString(3, answerThree); ps.setString(4, answerFour); ps.setString(5, name); + ps.setString(6, projectName); ps.executeUpdate(); return true; } catch (SQLException e) { @@ -86,7 +87,32 @@ public class Database implements AutoCloseable { } return false; } + } + /** + * Get all the survey results from the database + */ + public SurveyResult[] getSurveyResults() { + String sql = "select * from SurveyResults"; + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery(sql); + SurveyResult[] results = new SurveyResult[100]; + int i = 0; + while (rs.next()) { + String respName = rs.getString("name"); + String projectName = rs.getString("projectName"); + String projectDescription = rs.getString("projectDescription"); + + int[] data = { rs.getInt("answerOne"), rs.getInt("answerTwo"), rs.getInt("answerThree"), + rs.getInt("answerFour") }; + results[i] = new SurveyResult(respName, projectName, projectDescription, data); + i++; + } + return results; + } catch (SQLException e) { + printSqlError(e); + return null; + } } private void printSqlError(SQLException e) { diff --git a/src/main/java/servlet/FormGenerator.java b/src/main/java/servlet/FormGenerator.java index dc704d8..eb4bd07 100644 --- a/src/main/java/servlet/FormGenerator.java +++ b/src/main/java/servlet/FormGenerator.java @@ -56,4 +56,17 @@ public class FormGenerator { return html; } + public String resultsTable(SurveyResult[] results) { + String html = "
Respondent | Project | Description | Factor 1 | Factor 2 | Factor 3 | Factor 4 |
---|---|---|---|---|---|---|
" + result.getName() + " | " + result.getProjectName() + " | " + + result.getProjectDesc() + " | " + result.getData()[0] + " | " + result.getData()[1] + + " | " + result.getData()[2] + " | " + result.getData()[3] + " |
Hello " + name); out.println("
The sum of the values you entered is " + sum); + out.println("
To view other results, click here"); sessionShouldBeEnded = true; } else { out.println("The values you entered were not OK"); diff --git a/src/main/java/servlet/SurveyResult.java b/src/main/java/servlet/SurveyResult.java new file mode 100644 index 0000000..84b4c3d --- /dev/null +++ b/src/main/java/servlet/SurveyResult.java @@ -0,0 +1,36 @@ +package servlet; + +public class SurveyResult { + private String respName; + private String projectName; + private String projectDesc; + private int[] data; + + public SurveyResult(String respName, String projectName, String projectDesc, int[] data) { + this.respName = respName; + this.projectName = projectName; + this.projectDesc = projectDesc; + this.data = data; + } + + public String getName() { + return respName; + } + + public String getProjectName() { + return projectName; + } + + public String getProjectDesc() { + return projectDesc; + } + + public int[] getData() { + return data; + } + + public String toString() { + return String.format("Name: %s, Project: %s, Project Description: %s, Data: %d, %d, %d, %d", respName, + projectName, projectDesc, data[0], data[1], data[2], data[3]); + } +}