-
Notifications
You must be signed in to change notification settings - Fork 0
/
Accreditor.java
204 lines (190 loc) · 6.32 KB
/
Accreditor.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import java.sql.*;
import java.util.*; // ArrayList, Random
import javax.swing.JOptionPane;
public class Accreditor{
String myMatric,myFName,mySName,myDept,mySchool, myYear,mySex,myEmail,myPin;
public Accreditor(String matric){
myMatric = matric;
}
// calls all the other private and inherited methods
final String DATABASE_URL = "jdbc:mysql://localhost/vote";
Connection connection;
Statement statement;
ResultSet resultSet;
private void connect() throws SQLException{
try{
connection = DriverManager.getConnection(DATABASE_URL, "root", "mysqlrootpassword");
}catch(SQLException sqlErr){
sqlErr.printStackTrace();
JOptionPane.showMessageDialog(null, "Could not connect to Database\nPlease try again" + sqlErr.getMessage(), "ERROR OCCURED", JOptionPane.ERROR_MESSAGE);
}
}
protected void closeConnection() throws SQLException{
try{
connection.close();
statement.close();
resultSet.close();
}catch(Exception err){
err.printStackTrace();
}
}
protected void getStringDetails() throws SQLException{
connect();
String query_string_details = "SELECT fname, sname, department, school, year, sex FROM students WHERE matric = " + this.myMatric;// image is missing
try{
statement = connection.createStatement();
resultSet = statement.executeQuery(query_string_details);
while(resultSet.next()){
this.myFName = resultSet.getString("fname");
this.mySName = resultSet.getString("sname");
this.myDept = resultSet.getString("department");
this.mySchool = resultSet.getString("school");
this.myYear = resultSet.getString("year");
this.mySex = resultSet.getString("sex");
}
//this.myEmail = resultSet.getString("email");
//Image myImage = resultSet.get
//String myDetails = "Here are your details: " + myName + "\n" + myMatric + "\n" + myDept + "\n" + mySchool + "\n" + myYear + "\n" + mySex;
}catch(SQLException error){
error.printStackTrace();
}
}
private boolean confirmPresence()throws SQLException{
connect();
boolean isAStudent = false;
String cfmQuery = "SELECT 1 FROM students WHERE matric = ?";
try {
PreparedStatement prepStatement = connection.prepareStatement(cfmQuery);
if (prepStatement != null) {
prepStatement.setString(1, this.myMatric);
try{
ResultSet rSet = prepStatement.executeQuery();
if(rSet != null){
try{
if(rSet.next()){
isAStudent = true;
}
}catch(Exception rSetException){
rSetException.printStackTrace();
}
rSet.close();
}
}catch(Exception statementException){
statementException.printStackTrace();
}
//statement.close();
}
}catch(Exception genException){
genException.printStackTrace();
}
connection.close();
return isAStudent;
}
public void accredit() throws SQLException{
connect();
try{
if(confirmPresence()){
if(!isAccredited()){
generatePin();
addPinToDB();
if(quicklyVerifyPin()){
JOptionPane.showMessageDialog(null, "You have been successfully accredited\nYour pin is " + this.myPin + "Please keep it secure, you will need it to vote.\nHowever, it will be sent to the email you provided","Success", JOptionPane.INFORMATION_MESSAGE);
activateAccreditation();
}
else{
JOptionPane.showMessageDialog(null, "An error occured, please try again","Error", JOptionPane.ERROR_MESSAGE);
}
}
else{
// you have been accredited
JOptionPane.showMessageDialog(null, "This student has been accredited", "You have been accredited", JOptionPane.INFORMATION_MESSAGE);
}
}
else{
JOptionPane.showMessageDialog(null, "You are not a registered student\nYour name was not found in the school database");
}
}catch(SQLException bad){
bad.printStackTrace();
}
}
// makes a new pin, saves it in this.pin
private void generatePin(){
GeneratePin generatePin = new GeneratePin();
this.myPin = generatePin.getPin();
}
//checks if the generated pin has been saved
private boolean quicklyVerifyPin() throws SQLException{
boolean allFine = false;
String verifyQuery = "SELECT * FROM students WHERE matric = '" + this.myMatric +"'";
connect();
try{
statement = connection.createStatement();
resultSet = statement.executeQuery(verifyQuery);
while(resultSet.next()){
String thePin = resultSet.getString("pin");
if(thePin.equals(this.myPin)){
allFine = true;
}else{
allFine = false;
}
}
}catch(SQLException baddy){
baddy.printStackTrace();
}finally{
closeConnection();
}
return allFine;
}
private void activateAccreditation()throws SQLException{
String query = "UPDATE students SET accredited = ? WHERE matric = ?";
connect();
PreparedStatement setAccrStat = null;
try{
setAccrStat = connection.prepareStatement(query);
setAccrStat.setInt(1, 1);
setAccrStat.setString(2, this.myMatric);
setAccrStat.executeUpdate();
}catch(SQLException anErr){
anErr.printStackTrace();
JOptionPane.showMessageDialog(null, "Sorry, something happened We are trying to fix it" + anErr.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
}finally{
closeConnection();
setAccrStat.close();
}
}
private boolean isAccredited()throws SQLException{
String check = "SELECT accredited FROM students WHERE matric = '" + this.myMatric +"'";
connect();
boolean valid = false;
Statement state = connection.createStatement();
ResultSet resSet;
try{
resSet = state.executeQuery(check);
int a = resSet.getInt("accredited");
if(a == 1 || a != 0){
valid = true;
}
}catch(SQLException e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, e.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
return valid;
}
// adds the pin to the database
private void addPinToDB() throws SQLException{
String addQuery = "UPDATE students SET pin = ? WHERE matric = ?" ;
connect();
PreparedStatement pStatement = null;
try{
pStatement = connection.prepareStatement(addQuery);
pStatement.setString(1, this.myPin);
pStatement.setString(2, this.myMatric);
pStatement.executeUpdate();
}catch(SQLException err404){
err404.printStackTrace();
}finally{
closeConnection();
pStatement.close();
}
}
}