-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSQLMethods.java
190 lines (160 loc) · 4.96 KB
/
SQLMethods.java
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
/**
* SQL class containing all methods to
* read and write to the database.
* Methods can then be called in the report
* class.
* @author amy
*
*/
public class SQLMethods {
private int gameID;
private Connection connection = null;
public SQLMethods() {
String DBname = "m_16_0813273j";
String username = "m_16_0813273j";
String pass = "0813273j";
try {
connection = DriverManager.getConnection("jdbc:postgresql://yacata.dcs.gla.ac.uk:5432/" + DBname, username,
pass);
}
catch (SQLException e) {
e.printStackTrace();
}
}
//method to count the number of games stored
public int gameCount() {
int gameCount = 0;
Statement sizeStmt = null;
String gameCountQuery = "SELECT COUNT(*) from gameplayData";
try{
sizeStmt = connection.createStatement();
ResultSet sizeRS = sizeStmt.executeQuery(gameCountQuery);
sizeRS.next();
gameCount = sizeRS.getInt("count");
} catch (SQLException e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Error executing query.",
"Error: Query failed.", JOptionPane.ERROR_MESSAGE);
}
return gameCount;
}
//method to count the number of P1/human wins
public int humanWinner() {
int humanWin = 0;
Statement humanWinnerStmt = null;
String humanWinQuery = "SELECT COUNT (winner) FROM gameplayData WHERE winner = 'Player One'";
try {
humanWinnerStmt = connection.createStatement();
ResultSet humanRS = humanWinnerStmt.executeQuery(humanWinQuery);
humanRS.next();
humanWin = humanRS.getInt("count");
} catch (SQLException e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Error executing query.",
"Error: Query failed.", JOptionPane.ERROR_MESSAGE);
}
return humanWin;
}
//method to get the average number of draws
public int avgDraws() {
int avgDraws = 0;
Statement drawsStmt = null;
String avgDrawsQuery = "SELECT AVG (totDraws) AS AvgDraws FROM gameplayData";
try{
drawsStmt = connection.createStatement();
ResultSet drawsRS = drawsStmt.executeQuery(avgDrawsQuery);
drawsRS.next();
avgDraws = drawsRS.getInt("avgdraws");
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Error executing query.",
"Error: Query failed.", JOptionPane.ERROR_MESSAGE);
}
return avgDraws;
}
//method to get the number of computer wins
public int compWin(){
int computerWins = 0;
Statement compStmt = null;
String compWinQuery = "SELECT COUNT (winner) FROM gameplayData WHERE winner != 'Player One'";
try{
compStmt = connection.createStatement();
ResultSet compWinRS = compStmt.executeQuery(compWinQuery);
compWinRS.next();
computerWins = compWinRS.getInt("count");
} catch (SQLException e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Error executing query.",
"Error: Query failed.", JOptionPane.ERROR_MESSAGE);
}
return computerWins;
}
//method to get the largest number of rounds played in a game
public int largestRounds() {
int largestRounds = 0;
Statement roundsStmt = null;
String largestRoundsQuery = "SELECT MAX (totRounds) FROM gameplayData";
try{
roundsStmt = connection.createStatement();
ResultSet roundsRS = roundsStmt.executeQuery(largestRoundsQuery);
roundsRS.next();
largestRounds = roundsRS.getInt("max");
} catch (SQLException e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Error executing query.",
"Error: Query failed.", JOptionPane.ERROR_MESSAGE);
}
return largestRounds;
}
//method to generate a new gameID
public boolean generateGameID(){
gameID = gameCount() + 1;
return true;
}
//method to write game data to the DB
public boolean writeGameplay(String input) {
String queryAddGameplay = "INSERT INTO gameplayData VALUES(" + gameID + "," + input + ");";
Statement stmt = null;
try {
stmt = connection.createStatement();
int rs = stmt.executeUpdate(queryAddGameplay);
return true;
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Failed to save results. Please contact support",
"Error: Saving Failed", JOptionPane.ERROR_MESSAGE);
}
return false;
}
public boolean writeRoundPlay(String input) {
String queryAddGameplay = "INSERT INTO roundswon VALUES(" + gameID + "," + input + ");";
Statement stmt = null;
try {
stmt = connection.createStatement();
int rs = stmt.executeUpdate(queryAddGameplay);
return true;
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Failed to save results. Please contact support",
"Error: Saving Failed", JOptionPane.ERROR_MESSAGE);
}
return false;
}
//method to close the connection
public boolean closeConnection() {
try {
connection.close();
return true;
}
catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}