-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUser.java
315 lines (254 loc) · 11.8 KB
/
User.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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
// Java-->Driver-->Mysql
public class User {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://Localhost:3306/test";
static final String USER = "Username";
static final String PASS = "Passwor";
public void fun(Scanner scan, Integer Id1)
{
Connection conn = null;
Statement stmt = null;
try{
//STEP 1: Register JDBC driver
Class.forName(JDBC_DRIVER);
//STEP 2: Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
//STEP 3: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
//Welcome user!
System.out.println("Welcome to the Washing Machine Purchasing Program!");
//Initialize our querying variable
boolean querying = true;
try {
while(querying)
{
//Print the menu
System.out.println();
System.out.println("********************************");
System.out.println("Please select a query below!");
//5 available options
System.out.println("1. List of all Models of Washing Machine");
System.out.println("2. List of Details of Model ");
System.out.println("3. Purchase Washine Machine");
System.out.println("4. Check Your Orders");
System.out.println("5. Dashboard");
System.out.println("6. Logout");
System.out.println("********************************");
System.out.println();
System.out.println("Enter Your Choice");
String input = scan.nextLine();
int answer;
try{
answer=Integer.valueOf(input);
}
catch(Exception e)
{
answer=-1;
}
if(answer == -1 || answer < 1 || answer > 5)
{
//User entered incorrect input
System.out.println("Incorrect Input!");
System.out.println("Please enter a number 1-5");
}
else if (answer == 1)
{
String sql = "SELECT Title FROM wm";
try (ResultSet rs = stmt.executeQuery(sql)) {
System.out.println("Models:");
while(rs.next()){
//Retrieve by column name
String title = rs.getString("Title");
//Display values
System.out.println(title);
}
//Close the result set
rs.close();
}
catch(Exception e) {
e.printStackTrace();
System.out.println("Error! Could Not get result from the Database");
}
}
else if (answer == 2)
{
System.out.println("Please enter the Model Name");
input = scan.nextLine();
//Query the DB
String sql = "SELECT * FROM wm WHERE LOWER(Title) LIKE LOWER('%" + input + "%')";
ResultSet rs = stmt.executeQuery(sql);
if(rs.next())
{
//Retrieve by column name
String title = rs.getString("Title");
String details = rs.getString("Details");
String Price = rs.getString("Price");
String Availablity = rs.getString("Availablity");
//Display values
System.out.print("Model: ");
System.out.println(title);
System.out.print("Details: ");
System.out.println(details);
System.out.print("Price: ");
System.out.println(Price);
System.out.print("Availablity: ");
System.out.println(Availablity);
}
else
{
System.out.println("That model does not exist...");
}
rs.close();
}
else if (answer==3)
{
System.out.println("Please enter the Model Name");
input = scan.nextLine();
String sql = "SELECT Availablity FROM wm WHERE LOWER(Title) LIKE LOWER('%" + input + "%')";
ResultSet rs = stmt.executeQuery(sql);
if(rs.next())
{
String Availablity = rs.getString("Availablity");
int val=Integer.valueOf(Availablity);
if (val<=0)
{
System.out.println("Not Avaiable");
}
else
{
val=val-1;
String sql1 = "UPDATE wm SET wm.Availablity = ? where wm.Title=?";
PreparedStatement stmt1 = conn.prepareStatement(sql1);
stmt1.setInt(1, val);
stmt1.setString(2, input);
stmt1.executeUpdate();
System.out.println("Enter your Address");
String addr= scan.nextLine();
String sql2 = "INSERT INTO book Values" + "("+ Id1 + ", " + "'" + input + "', " + "'" + addr + "')";
stmt.execute(sql2);
System.out.println("Booking Confirmed");
}
}
else
{
System.out.println("That model does not exist...");
}
rs.close();
}
else if (answer == 4)
{
String sql = "SELECT Model FROM book where Id = "+Id1+"";
try (ResultSet rs = stmt.executeQuery(sql)) {
System.out.println("Model No");
while(rs.next()){
//Retrieve by column name
String title = rs.getString("Model");
//Display values
System.out.println(title);
}
//Close the result set
rs.close();
}
catch(Exception e) {
e.printStackTrace();
System.out.println("Error! Could Not get result from the Database");
}
}
else if (answer == 5)
{
System.out.println("Dashboard");
System.out.println("********************************");
System.out.println("Please select a query below!");
//5 available options
System.out.println("1. Cart");
System.out.println("2. Edit info ");
System.out.println("3. Buy");
System.out.println("4. Exit from Dashboard");
System.out.println("********************************");
System.out.println("Enter Your Choice");
String input1 = scan.nextLine();
int answer1;
try{
answer1=Integer.valueOf(input1);
}
catch(Exception e)
{
answer1=-1;
}
if(answer1 == -1 || answer1 < 1 || answer1 > 4)
{
//User entered incorrect input
System.out.println("Incorrect Input!");
System.out.println("Please enter a number 1-5");
}
else if (answer1==1)
{
String sql = "SELECT Model FROM book where Id = "+Id1+"";
try (ResultSet rs = stmt.executeQuery(sql)) {
System.out.println("Model No");
while(rs.next()){
//Retrieve by column name
String title = rs.getString("Model");
//Display values
System.out.println(title);
}
//Close the result set
rs.close();
}
catch(Exception e) {
e.printStackTrace();
System.out.println("Error! Could Not get result from the Database");
}
}
else if (answer1==3)
{
System.out.println("Enter your Address");
String addr= scan.nextLine();
String sql2 = "INSERT INTO book Values" + "("+ Id1 + ", " + "'" + input + "', " + "'" + addr + "')";
stmt.execute(sql2);
}
}
else if (answer == 6)
{
querying = false;
}
}
}
catch (Exception e)
{
System.out.println(e);
}
}
catch(SQLException | ClassNotFoundException e)
{
e.printStackTrace();
}
finally{
//finally block used to close resources
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
//Inform user of error
System.out.println("Error closing connection to the database!");
}//end finally try
}//end try
System.out.println("Goodbye!");
}
}