-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLconnection.java
More file actions
248 lines (210 loc) · 9.67 KB
/
Copy pathSQLconnection.java
File metadata and controls
248 lines (210 loc) · 9.67 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package parksmartdatabaseconnector;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
/**
*
* @author jorda
*/
public class SQLconnection {
Connection connection;
PreparedStatement statement;
public SQLconnection(String address, int port, String user, String password) throws ClassNotFoundException, SQLException {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl
= "jdbc:sqlserver://" + address + ";"
+ "database=master;"
+ "user=" + user + ";"
+ "password=" + password + ";"
+ "encrypt=true;"
+ "trustServerCertificate=true;"
+ "loginTimeout=30;";
//ResultSet resultSet = null;
connection = DriverManager.getConnection(connectionUrl);
if(connection.isValid(30))
System.out.println("SQL Connection has been made");
//resultSet = statement.executeQuery(selectSql);
}
//all cars will be sent to this to be proceesed
//1 check if the plate exists. If it does, update history and lot usage
//2 if the plate doesn't exists add the plate, update history and lot usage
//3 make an overloaded method if the plate number wasn't caught, only updatelot usage
public void scannedVehicle(String PlateNumber) {
}
//with pass
public void addVehicle(String Pass, String PlateNumber, String color, String Make, String Region, String Model) throws SQLException {
//if the pass is present in the addVechicle, check if it exists,
//check if the pass exists
if (!passexists(Pass) & Pass != null) {
//add the pass to the database here
this.addPass(Pass);
//type and year will be null
}
//the pass exists
//if the plate doesn't exist, add it
if (!this.plateexists(PlateNumber)) {
statement = connection.prepareStatement("INSERT INTO dbo.Vehicle VALUES (?,?,?,?,?,?,?)");
statement.setString(1, Pass);
statement.setString(2, PlateNumber);
statement.setString(3, Region);
statement.setString(4, color);
statement.setString(5, Make);
statement.setString(6, Model);
statement.setNull(7, Types.NULL);
statement.executeUpdate();
}
//if doesn't exist add time stamp for in (maybe this shouldn't be done in this method
}
public void addVehicle(String PlateNumber, String color, String Make, String Region, String Model, float confidence) throws SQLException {
//if the plate doesn't exist, add it
if (!this.plateexists(PlateNumber, Region)) {
statement = connection.prepareStatement("INSERT INTO dbo.Vehicle VALUES (?,?,?,?,?,?,?)");
statement.setNull(1, Types.NULL);
statement.setString(2, PlateNumber);
statement.setString(3, Region);
statement.setString(4, color);
statement.setString(5, Make);
statement.setString(6, Model);
statement.setFloat(7, confidence);
statement.execute();
}else{
System.out.println("Plate not added, likley already exists: " + PlateNumber);
}
//if doesn't exist add time stamp for in (maybe this shouldn't be done in this method
}
public boolean plateexists(String PlateNumber, String Region) throws SQLException {
//return weather or not a plate is in the DB
//true = plate in database
statement = connection.prepareStatement("SELECT License_Plate_Number FROM dbo.Vehicle WHERE License_Plate_Number = ?");
statement.setString(1, PlateNumber);
//statement.setString(2, Region);
statement.execute();
ResultSet rs = statement.getResultSet();
return (rs.next());
}
public boolean plateexists(String PlateNumber) throws SQLException {
//return weather or not a plate is in the DB
//true = plate in database
statement = connection.prepareStatement("SELECT License_Plate_Number FROM dbo.Vehicle WHERE License_Plate_Number = ?");
statement.setString(1, PlateNumber);
statement.execute();
ResultSet rs = statement.getResultSet();
return (rs.next());
}
public boolean passexists(String Pass) throws SQLException {
//return weather or not a pass exists
statement = connection.prepareStatement("SELECT Pass_Number FROM dbo.Valid_Passes WHERE Pass_Number = ?");
statement.setString(1, Pass);
ResultSet PassNumberResults = statement.executeQuery();
return PassNumberResults.equals(Pass);
}
public void addPass(String Pass, Date date, int type) throws SQLException {
//add the pass to the database here
statement = connection.prepareStatement("INSERT INTO dbo.Valid_Passes VALUES (?,?,?)");
statement.setString(1, Pass);
statement.setInt(2, type);
statement.setDate(3, date);
statement.executeUpdate();
//type and year will be null
}
//add the pass if it doesn't exist
//update the Vechicle (by plate number) to include the pass
public void addPassToVehicle(String Plate, String Pass) throws SQLException {
if (!this.passexists(Pass)) {
this.addPass(Pass);
}
statement = connection.prepareStatement("UPDATE dbo.Vehicle SET Valid_Pass = ? WHERE License_Plate_Number = ?");
statement.setString(1, Pass);
statement.setString(2, Plate);
}
public void addPass(String Pass) throws SQLException {
//add the pass to the database here
statement = connection.prepareStatement("INSERT INTO dbo.Valid_Passes VALUES (?)");
statement.setString(1, Pass);
statement.setNull(2, Types.NULL);
statement.setNull(3, Types.NULL);
statement.executeUpdate();
//type and year will be null
}
public void addHistory(String PlateNumber, boolean in, int lotkey, Timestamp stamp) throws SQLException {
//update the dbo.Parking_lots first
this.updateLotUsed(lotkey, in);
//add entry to the plate's history with the included timestamp. need to finalized the time formate
if (plateexists(PlateNumber)) {
statement = connection.prepareStatement("INSERT INTO dbo.Vehicle_History VALUES (?,?,?,?)");
statement.setString(1, PlateNumber);
statement.setInt(2, lotkey);
statement.setBoolean(3, in);
statement.setTimestamp(4, stamp);
statement.executeUpdate();
} else {
System.out.println("Warning: Tried to update plate history for a plate that doesn't exisit in database: " + PlateNumber);
}
}
//add history with date "now" or the time of execution
//Lot key 1 is Luara Lander with 95 total spots
//when "in" is true is means the car was going into the parking lot. If it's false the car was leaving
public void addHistory(String PlateNumber, boolean in, int lotkey) throws SQLException {
//add entry to the plate's history with the current time.
Timestamp stamp = new Timestamp(System.currentTimeMillis());
this.updateLotUsed(lotkey, in);
if (plateexists(PlateNumber)) {
statement = connection.prepareStatement("INSERT INTO dbo.Vehicle_History VALUES (?,?,?,?)");
statement.setString(1, PlateNumber);
statement.setInt(2, lotkey);
statement.setBoolean(3, in);
statement.setTimestamp(lotkey, stamp);
statement.executeUpdate();
} else {
System.out.println("Warning: Tried to update plate history for a plate that doesn't exisit in database: " + PlateNumber);
}
}
//Lot key 1 is Luara Lander with 95 total spots
public void updateLotUsed(int lotkey, boolean in) throws SQLException {
//increae or decrease the number in each parking lot
int Num_Used;
String num;
ResultSet set;
//get the number of spots used before
statement = connection.prepareStatement("SELECT Num_Used FROM dbo.Parking_lots WHERE Parking_Lot_Key = ?");
statement.setInt(1, lotkey);
statement.execute();
set = statement.getResultSet();
//System.out.println(set.getRow());
set.next();
num = set.getString("Num_Used");
Num_Used = Integer.parseInt(num);
//add or subtract?
if (in) {
Num_Used++;
} else {
Num_Used--;
}
//after getting the current number of spots, add to it and update it
statement = connection.prepareStatement("UPDATE dbo.Parking_lots SET Num_Used = ? WHERE Parking_Lot_Key = ?");
statement.setInt(2, lotkey);
statement.setInt(1, Num_Used);
statement.execute();
//verify the update
statement = connection.prepareStatement("SELECT Num_Used FROM dbo.Parking_lots WHERE Parking_Lot_Key = ?");
statement.setInt(1, lotkey);
statement.execute();
set = statement.getResultSet();
set.next();
int new_Num_Used = set.getInt(1);
if (new_Num_Used != Num_Used) {
System.out.println("Warning: Spots used in lot " + lotkey + " could not be updated correctly");
}
}
}