-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestionGateway.java
More file actions
135 lines (128 loc) · 4.96 KB
/
QuestionGateway.java
File metadata and controls
135 lines (128 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package mvc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.servlet.http.HttpServlet;
public class QuestionGateway {
public static ArrayList<Question> questions;
public static ArrayList<Answer> answers;
private static final long serialVersionUID = 1L;
private static Connection con;
private static Statement stmt;
/**
* @throws SQLException
* @see HttpServlet#HttpServlet()
*/
public static void makeGate() throws SQLException {
String connectionString = "jdbc:mysql://kc-sce-appdb01.kc.umkc.edu/ertgd8";
String userID = "ertgd8";
String password = "NRASn1pgbLn";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch(java.lang.ClassNotFoundException e) {
System.out.println(e);
System.exit(0);
}
try {
con = DriverManager.getConnection(connectionString,userID,password);
stmt = con.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
questions=new ArrayList<Question>();
answers=new ArrayList<Answer>();
createQuestionTable();
createAnswerTable();
}
public static void cleanup() throws SQLException {
// Close connection and statement
// Connections, statements, and result sets are
// closed automatically when garbage collected
// but it is a good idea to free them as soon
// as possible.
// Closing a statement closes its current result set.
// Operations that cause a new result set to be
// created for a statement automatically close
// the old result set.
stmt.close();
con.close();
}
//creates question table
public static void createQuestionTable() throws SQLException {
String trunc="TRUNCATE TABLE Questions"; //clear old table - start fresh
String creQTable="CREATE TABLE IF NOT EXISTS Questions "+
"(ID INT PRIMARY KEY AUTO_INCREMENT, "+
"Question VARCHAR(255))";
stmt.executeUpdate(creQTable);
//stmt.executeUpdate(trunc);
}
//creates answer table
public static void createAnswerTable() throws SQLException{
String trunc="TRUNCATE TABLE Answers"; //clear old table - start fresh
String creATable="CREATE TABLE IF NOT EXISTS Answers "+
"(QID INT, "+
"Answer VARCHAR(255))";
stmt.executeUpdate(creATable);
//stmt.executeUpdate(trunc);
}
//adds new question
public static void addQuestion(String newQuestion) throws SQLException{
String addQ="INSERT INTO Questions(Question) VALUES('"+newQuestion+"')";
stmt.executeUpdate(addQ);
}
//adds new answer with specified question
public static void addAnswer(String newAnswer,String qid) throws SQLException{
String addA="INSERT INTO Answers(QID, Answer) VALUES('"+Integer.parseInt(qid)+"','"+newAnswer+"')";
stmt.executeUpdate(addA);
}
//makes an array of questions from table
public static ArrayList<Question> getQuestions() throws SQLException{
String getQ="SELECT * FROM Questions";
ResultSet rs=stmt.executeQuery(getQ);
ResultSetMetaData rsmd1=rs.getMetaData();
int numCols=rsmd1.getColumnCount();
//for each row, make question to pass to webpage
boolean more=rs.next();
while(more){
//create new question object
Question question=new Question();
ArrayList<String> val_list=new ArrayList<String>(); //list of data vals
for(int i=1;i<=numCols;++i){
//get data for question
val_list.add(rs.getString(i));
}
question.setID(val_list.get(0)); //val_list 0 is the id
question.setQuestion(val_list.get(1)); //val_list 1 is the question string
questions.add(question);
more=rs.next();
}
return questions;
}
//makes an array of answers from table specific to a question
public static ArrayList<Answer> getAnswers(String qid) throws SQLException{
String getA="SELECT * FROM ANSWERS WHERE QID='"+Integer.parseInt(qid)+"'";
ResultSet rs=stmt.executeQuery(getA);
ResultSetMetaData rsmd1=rs.getMetaData();
int numCols=rsmd1.getColumnCount();
//for each row, make answer to pass to webpage
boolean more=rs.next();
while(more){
//create new answer object
Answer answer=new Answer();
ArrayList<String> val_list=new ArrayList<String>(); //list of data vals
for(int i=1;i<=numCols;++i){
//get data for answer
val_list.add(rs.getString(i));
}
answer.setID(val_list.get(0)); //val_list 0 is the id
answer.setQuestion(val_list.get(1)); //val_list 1 is the answer string
answers.add(answer);
more=rs.next();
}
return answers;
}
}