-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessResultsDAOImplementation.java
More file actions
183 lines (137 loc) · 5.03 KB
/
Copy pathChessResultsDAOImplementation.java
File metadata and controls
183 lines (137 loc) · 5.03 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
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
package edu.unlv.mis768.finalproject;
/**
* This class implements the DAO for ChessResultsDAO
* @author William Brasic and Sergio Torres
*/
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class ChessResultsDAOImplementation implements ChessResultsDAO{
@Override
public boolean createChessResult(ChessResult chessResult) {
// initialize flag
boolean flag=false;
try {
// connect to database
Connection conn = ChessResultsDBUtil.getDBConnection();
Statement stmt = conn.createStatement();
// sql statement for insertion
String sql = "INSERT INTO " + "ChessResults"+
" VALUES ('"+
chessResult.getMatchID()+"', '"+
chessResult.getPlayer1().getPlayerName()+"', '"+
chessResult.getPlayer2().getPlayerName()+"')";
System.out.println(sql);
//Execute the query.
int rows = stmt.executeUpdate(sql);
// if we inserted a record set the flag to true
if (rows ==1)
flag=true;
// close connection
stmt.close();
ChessResultsDBUtil.closeDBConnection(conn);
// print error if needed
} catch (Exception ex) {
System.out.println("ERROR: " + ex.getMessage());
}
return flag;
}
@Override
public boolean deleteChessResult(ChessResult chessResult) {
// initialize flag
boolean flag=false;
try {
// connect to database
Connection conn = ChessResultsDBUtil.getDBConnection();
// sql statement
String sql = "DELETE FROM ChessResults WHERE MatchID = ?";
// prepared statement object
PreparedStatement prepStmt = conn.prepareStatement(sql);
// provide the value
prepStmt.setString(1, chessResult.getMatchID());
// execute it
int rows = prepStmt.executeUpdate();
// print query result
System.out.println(sql);
// if a row has been deleted then set the flag to true
if (rows ==1)
flag=true;
// close connection
prepStmt.close();
ChessResultsDBUtil.closeDBConnection(conn);
// print error if needed
} catch (Exception ex) {
System.out.println("ERROR: " + ex.getMessage());
}
// return flag
return flag;
}
@Override
//@Return list of player objects.
public String getTotalGamesPlayed(Player player) {
// Create a string object for the data.
String gamesList = new String();
try {
// connect to database
Connection conn = ChessResultsDBUtil.getDBConnection();
// sql command with backtick on columns and string literals to reference player names in query.
String sql = "SELECT * FROM ChessResults WHERE `Player1` = '"+player.getPlayerName()+"'OR `Player2` = '"+player.getPlayerName()+"'" ;
// create statement object
Statement stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
// result set
ResultSet result = stmt.executeQuery(sql);
//Get the number of rows.
result.last(); // Move to last row
int numRows = result.getRow(); // Get row number
result.first(); // Move to first row
for (int row = 0; row < numRows; row++) {
// Add fields from database to gamesList with the values from the result set.
gamesList += "Match: " + result.getString("MatchID") + ", " +
result.getString("Player1") + " VS " +
result.getString("Player2") + " \n";
// Go to the next row in the ResultSet.
result.next();
}
// close connection
stmt.close();
ChessResultsDBUtil.closeDBConnection(conn);
} catch (SQLException e) {
System.out.print(e.getMessage());
}
return gamesList;
}
@Override
public boolean updateChessResult(ChessResult chessResult) {
// initialize flag
boolean flag=false;
try {
// connect to database
Connection conn = ChessResultsDBUtil.getDBConnection();
// sql statement
String sql = "UPDATE ChessResults SET Player1 = ? , Player2 = ? WHERE MatchID = ?;";
// prepared statement object
PreparedStatement prepStmt = conn.prepareStatement(sql);
// provide the value
prepStmt.setString(3, chessResult.getMatchID());
prepStmt.setString(1, chessResult.getPlayer1().getPlayerName());
prepStmt.setString(2,chessResult.getPlayer2().getPlayerName());
// execute it
int rows = prepStmt.executeUpdate();
// print query result
System.out.println(sql);
// if a row has been deleted then set the flag to true
if (rows ==1)
flag=true;
// close connection
prepStmt.close();
ChessResultsDBUtil.closeDBConnection(conn);
// print error if needed
} catch (Exception ex) {
System.out.println("ERROR: " + ex.getMessage());
}
// return flag
return flag;
}
}