-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnInterface.java
363 lines (331 loc) · 11.9 KB
/
ConnInterface.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* ----------------------------------------------------------------------------
* Class ConnInterface: provides methods for establishing a database connection,
* querying and updating the database, and closing the connection.
*/
import java.sql.*;
import java.util.Scanner;
class ConnInterface
{
private Connection conn = null;
private String user, pass;
private Scanner userInput = new Scanner(System.in);
// constructor
public ConnInterface(String user, String pass)
{
setUser(user);
setPass(pass);
} // end constructor
public boolean openConn()
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:" + user + "/"
+ pass + "@//127.0.01:1521/PDB1");
}
catch (ClassNotFoundException e)
{
System.out.println("Could not load the driver");
return false;
}
catch (SQLException e)
{
System.out.println("Error establishing connection");
return false;
}
return true;
} // end openConn
public void closeConn()
{
try
{
conn.close();
}
catch (Exception e)
{
System.out.println("Error closing connection");
e.printStackTrace();
}
} // end closeConn
public void closeScanner()
{
userInput.close();
}
public String getUser()
{
return user;
} // end getUser
public void setUser(String user)
{
this.user = user;
} // end setUser
public String getPass()
{
return pass;
} // end getPass
public void setPass(String pass)
{
this.pass = pass;
} // end setPass
public boolean printWines()
{
try
{
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("SELECT * FROM Wines");
System.out.println("\nWine Name Vintage Maker Name"
+ " Color\n");
String winePad, vintagePad, makerPad;
String padBuffer = " ";
while (rset.next())
{
winePad = padBuffer.substring(0, 21 - rset.getString(1).length());
vintagePad = " ";
makerPad = padBuffer.substring(0, 31 - rset.getString(3).length());
System.out.println(rset.getString(1) + winePad
+ rset.getString(2) + vintagePad
+ rset.getString(3) + makerPad
+ rset.getString(4));
}
stmt.close();
}
catch (SQLException e)
{
System.out.println("SQL error: bad connection, statement,"
+ " or result set");
e.printStackTrace();
return false;
}
return true;
} // end printWines
public boolean lookupAppellation()
{
try
{
// create prepared statement
PreparedStatement stmt = conn.prepareStatement(
"SELECT Appellations.appellationName, county, climate "
+ "FROM Appellations, Origin "
+ "WHERE Appellations.appellationName = Origin.appellationName "
+ "AND Origin.wineName LIKE ? "
+ "AND Origin.vintage LIKE ? "
+ "AND Origin.makerName LIKE ? ");
// Query user for wine and set statement parameters
System.out.println("Enter the wine name, exactly: ");
stmt.setString(1, userInput.nextLine());
System.out.println("Enter the vintage: ");
stmt.setString(2, userInput.nextLine());
System.out.println("Enter the maker name, exactly: ");
stmt.setString(3, userInput.nextLine());
// execute the query and display the results
ResultSet rset = stmt.executeQuery();
System.out.println("\nAppellation Name County Climate\n");
String appellationPad, countyPad;
String padBuffer = " ";
while (rset.next())
{
appellationPad = padBuffer.substring(0,
21 - rset.getString(1).length());
countyPad = padBuffer.substring(0, 11 - rset.getString(2).length());
System.out.println(rset.getString(1) + appellationPad
+ rset.getString(2) + countyPad
+ rset.getString(3));
}
stmt.close();
}
catch (SQLException e)
{
System.out.println("SQL error: bad connection, statement,"
+ " or result set");
e.printStackTrace();
return false;
}
return true;
} // end lookupAppellation
public boolean findRestaurants()
{
try
{
// create prepared statement
PreparedStatement stmt = conn.prepareStatement(
"SELECT DISTINCT Restaurants.restaurantName, "
+ "Restaurants.restaurantAddress "
+ "FROM Restaurants, Serves "
+ "WHERE Restaurants.restaurantAddress = "
+ "Serves.restaurantAddress "
+ "AND Serves.wineName LIKE ? "
+ "AND Serves.vintage LIKE ? "
+ "AND Serves.makerName LIKE ? ");
// Query user for wine and set statement parameters
System.out.println("Enter the wine name, exactly: ");
stmt.setString(1, userInput.nextLine());
System.out.println("Enter the vintage: ");
stmt.setString(2, userInput.nextLine());
System.out.println("Enter the maker name, exactly: ");
stmt.setString(3, userInput.nextLine());
// execute the query and display the results
ResultSet rset = stmt.executeQuery();
System.out.println("\nRestaurant Name "
+ "Restaurant Address\n");
String namePad;
String padBuffer = " ";
while (rset.next())
{
namePad = padBuffer.substring(0, 31 - rset.getString(1).length());
System.out.println(rset.getString(1) + namePad
+ rset.getString(2));
}
stmt.close();
}
catch (SQLException e)
{
System.out.println("SQL error: bad connection, statement,"
+ " or result set");
e.printStackTrace();
return false;
}
return true;
} // end findRestaurants
public boolean findPotBuddies()
{
try
{
// create prepared statement
PreparedStatement stmt = conn.prepareStatement(
"SELECT DISTINCT F2.clientName, F2.clientAddress "
+ "FROM Frequents F1, Frequents F2 "
+ "WHERE F1.restaurantName = F2.restaurantName "
+ "AND F1.restaurantAddress = F2.restaurantAddress "
+ "AND F1.clientName LIKE ? "
+ "AND F1.clientAddress LIKE ? "
+ "AND F2.clientName NOT LIKE ? "
+ "AND F2.clientAddress NOT LIKE ? ");
// Query user for client info and set statement parameters
String name, address;
System.out.println("Enter the client's name, exactly: ");
name = userInput.nextLine();
stmt.setString(1, name);
stmt.setString(3, name);
System.out.println("Enter the client's address, exactly: ");
address = userInput.nextLine();
stmt.setString(2, address);
stmt.setString(4, address);
// execute the query and display the results
ResultSet rset = stmt.executeQuery();
System.out.println("\nClient Names Client Addresses\n");
String namePad;
String padBuffer = " ";
while (rset.next())
{
namePad = padBuffer.substring(0, 21 - rset.getString(1).length());
System.out.println(rset.getString(1) + namePad
+ rset.getString(2));
}
stmt.close();
}
catch (SQLException e)
{
System.out.println("SQL error: bad connection, statement,"
+ " or result set");
e.printStackTrace();
return false;
}
return true;
} // end findPotBuddies
public boolean addWine()
{
try
{
// create prepared statement
PreparedStatement stmt = conn.prepareStatement(
"INSERT INTO Wine_Info VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
// Query user for wine info and set statement parameters
System.out.println("Enter the wine name: ");
stmt.setString(1, userInput.nextLine());
System.out.println("Enter the vintage (later than 1850): ");
stmt.setString(2, userInput.nextLine());
System.out.println("Enter the maker's name, exactly "
+ "(must be an existing maker): ");
stmt.setString(3, userInput.nextLine());
System.out.println("Enter the color (Red, White, or Rose): ");
stmt.setString(4, userInput.nextLine());
System.out.println("Enter the varietal: ");
stmt.setString(5, userInput.nextLine());
System.out.println("Enter the appellation: ");
stmt.setString(6, userInput.nextLine());
System.out.println("Enter the county (must be Sonoma or Napa: ");
stmt.setString(7, userInput.nextLine());
System.out.println("Enter the climate (less than 5 characters): ");
stmt.setString(8, userInput.nextLine());
// execute the query and display the results
int rows = stmt.executeUpdate();
System.out.println("Inserted " + rows + " wine into database.");
stmt.close();
}
catch (SQLException e)
{
System.out.println("SQL error: bad connection, statement,"
+ " or update");
e.printStackTrace();
return false;
}
return true;
} // end addWine
public boolean addMakerToMenu()
{
try
{
// create prepared statement
CallableStatement stmt = conn.prepareCall(
"{CALL AddMakerToMenu(?, ?)}");
// Query user for wine info and set statement parameters
System.out.println("Enter the maker's name, exactly "
+ "(must be an existing maker): ");
String maker = userInput.nextLine();
stmt.setString(1, maker);
System.out.println("Enter the restaurant's address, exactly "
+ "(must be an existing location): ");
stmt.setString(2, userInput.nextLine());
// execute the query and display the results
stmt.execute();
System.out.println("Inserted " + maker + " into Serves table.");
stmt.close();
}
catch (SQLException e)
{
System.out.println("SQL error: bad connection, statement,"
+ " or update");
e.printStackTrace();
return false;
}
return true;
} // end addMakerToMenu
public boolean addLocation()
{
try
{
// create prepared statement
CallableStatement stmt = conn.prepareCall(
"{CALL NewLocationSameMenu(?, ?)}");
// Query user for wine info and set statement parameters
System.out.println("Enter the restaurant's name, exactly "
+ "(must be an existing restaurant): ");
String name = userInput.nextLine();
stmt.setString(1, name);
System.out.println("Enter the new location's address: ");
stmt.setString(2, userInput.nextLine());
// execute the query and display the results
stmt.execute();
System.out.println("Inserted the new location for " + name);
stmt.close();
}
catch (SQLException e)
{
System.out.println("SQL error: bad connection, statement,"
+ " or update");
e.printStackTrace();
return false;
}
return true;
} // end addLocation
} // end class ConnInterface ---------------------------------------------------