-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataBase.java
More file actions
585 lines (577 loc) · 22.6 KB
/
DataBase.java
File metadata and controls
585 lines (577 loc) · 22.6 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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
package Amazon;
import javax.swing.table.DefaultTableModel;
import java.sql.*;
import java.util.Objects;
import java.util.Vector;
public class DataBase {
private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://localhost:8889/YourStore";
private static final String USER = "root";
private static final String PASS = "root";
private static Connection conn;
private static Statement stmt;
public DataBase() {
conn = null;
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
} catch(Exception e){
e.printStackTrace();
}
}
private boolean exec(String qry){
try {
stmt = conn.createStatement();
int i = stmt.executeUpdate(qry);
return i > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
private ResultSet get_result(String qry){
try {
stmt = conn.createStatement();
return stmt.executeQuery(qry);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public void close() {
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {
se2.printStackTrace();
}
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
public boolean save_user(USER user ,String type){
String cid = String.valueOf(Add_cart());
String pid = String.valueOf(add_payment(user.getPayment()));
String sql = "INSERT INTO `User` (`Uid`, `Name`, `Pass`, `BirthDate`, `Address`, `Email`, `Phone`, `type`, `Cartid`, `Payid`)VALUES( '"+user.getID()+"' , '"+user.getName()+"', '"+user.getPassword()+"', '"+user.getBirthDate()+"', '"+user.getAddress()+"', '"+user.getEmail()+"', '"+user.getNumber()+"', '"+type+"' , "+cid+", "+pid+");";
exec(sql);
sql = "update `Cart` set `Uid` = '"+user.getID()+"' where `Cid` = "+cid+"";
return exec(sql);
}
public int add_payment(Payment payment){
String sql = "INSERT INTO `Payment` (`type`, `Amount`)\n" +
"VALUES\n" +
"\t( '"+payment.getType()+"', "+String.valueOf(payment.getAmount())+");";
exec(sql);
sql = "select max(`Payid`) from `Payment`";
ResultSet rs = get_result(sql);
try {
rs.first();
return rs.getInt(1);
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
public boolean search(String id, String pass){
String sql = "select `Uid` , `Pass` from `User`";
ResultSet rs = get_result(sql);
try {
assert rs != null;
while (rs.next()){
if (Objects.equals(rs.getString("Uid"), id) && Objects.equals(rs.getString("Pass"), pass))
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public ResultSet search(String key, char t){
String qry;
if (t == 'n'){
qry = "Select p.`Productid` as 'Product ID', p.`Name` as 'Product Name' , p.`price` , p.`Category` , p.`Type` , b.`Name` as 'Brand Name'\n" +
"from `Product` as p, `Brand` as b\n" +
"where b.`Bid` = p.`Brand_id`\n" +
"and (p.`Name` like '%"+key+"%' or b.`Name` like '%"+key+"%' or p.`Category` like '%"+key+"%' );";
}
else {
qry = "Select p.`Productid` as 'Product ID', p.`Name` as 'Product Name' , p.`price` , p.`Category`, p.`Type` , p.`Shows` as 'Number of views' , b.`Name` as 'Brand Name'\n" +
"from `Product` as p, `Brand` as b\n" +
"where b.`Bid` = p.`Brand_id`\n" +
"and (p.`Name` like '%" + key + "%' or b.`Name` like '%" + key + "%' or p.`Category` like '%\"+key+\"%');";
}
ResultSet rs = get_result(qry);
try {
assert rs != null;
while (rs.next()){
exec("update `Product` set `Shows` = `Shows` + 1 where `Productid` = "+rs.getString(1)+"");
}
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public int add_product(Product product){
String sql_check = "SELECT `Name`, `Bid` from `Brand`;";
boolean done = false;
int bid = -1;
try {
ResultSet rs = get_result(sql_check);
assert rs != null;
while (rs.next()){
if (rs.getString(1).equals(product.getBrand())){
done = true;
bid = rs.getInt(2);
break;
}
}
}catch (SQLException e){
e.printStackTrace();
}
if (done) {
String sql = "INSERT INTO `Product` (`Name`, `Brand_id`, `price`, `Category` , `Type`)" +
"VALUES" +
"('"+product.getName()+"', "+String.valueOf(bid)+", "+String.valueOf(product.getPrice())+" , '"+product.getCategory()+"', '"+product.getType()+"');";
exec(sql);
try {
ResultSet resultSet = get_result("select max(`Productid`)from `Product`;");
resultSet.first();
bid = resultSet.getInt(0);
}catch (SQLException e){
e.printStackTrace();
}
}
return bid;
}
public boolean add_store(Store store){
Vector<Product> products = store.getProducts();
Vector<String> brands = store.getBrands();
String sql_check = "SELECT `Name` from `Store`;";
boolean done = true;
try {
ResultSet rs = get_result(sql_check);
assert rs != null;
while (rs.next()){
if (Objects.equals(rs.getString(0), store.getName())){
done = false;
break;
}
}
}catch (SQLException e){
e.printStackTrace();
}
if (done){
String sql = "INSERT INTO `Store` (`Name`, `Owner`, `Address`, `Type`) " +
"VALUES " +
"('"+store.getName()+"', '"+store.getOwner().getID()+"', '"+store.getAdd()+"', '"+store.getType()+"');";
exec(sql);
String sid = "1";
try {
sid = get_result("select max(`Sid`)from `Store`;").getString(0);
} catch (SQLException e) {
e.printStackTrace();
}
for (String brand : brands)
exec("INSERT INTO `Store_brand` (`Sid`, `Bid`) " +
"VALUES " +
" (" + sid + ", (SELECT `Bid` from `Brand` where `Name` = '" + brand + "';) );");
for (Product product : products)
exec("INSERT INTO `Store_item` (`Sid`, `Pid`) " +
"VALUES " +
"(" + sid + ", " + String.valueOf(add_product(product)) + ");");
return true;
}
else {
return false;
}
}
public boolean add_brand(String brand){
String sql_check = "SELECT `Name`, `Bid` from `Brand`;";
boolean done = true;
try {
ResultSet rs = get_result(sql_check);
assert rs != null;
while (rs.next()){
if (Objects.equals(rs.getString(0), brand)){
done = false;
break;
}
}
}catch (SQLException e){
e.printStackTrace();
}
if (done){
String sql = "INSERT INTO `Brand` (`Name`) " +
"VALUES" +
" ('"+brand+"');";
exec(sql);
}
return done;
}
public String getName(String id){
try {
String sql = "SELECT `Name` FROM `User` where `Uid` = '"+id+"';";
ResultSet rs = get_result(sql);
assert rs != null;
rs.first();
return rs.getString(1);
} catch (SQLException e) {
e.printStackTrace();
}
return "";
}
public int Add_cart(){
String sql = "insert into `Cart` (`price`) values (0)";
exec(sql);
sql = "select max(`Cid`) from `Cart`";
ResultSet rs = get_result(sql);
try {
rs.first();
return rs.getInt(1);
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
public boolean add_to_cart(String uid, String pid){
String sql = null;
try {
String cid;
ResultSet resultSet = get_result("select `Cartid` from `User` where `Uid` = "+uid+"");
resultSet.first();
cid = resultSet.getString(1);
sql = "INSERT INTO `CartItem` (`Pid`, `Cid`)\n" +
"VALUES\n" +
"\t("+pid+", "+cid+");\n";
double total = get_cart(cid).getPrice() + get_product(pid).getPrice();
exec("update `Cart` set `price` = "+String.valueOf(total)+" where `Cid` = "+cid+" ;");
} catch (SQLException e) {
e.printStackTrace();
}
return exec(sql);
}
public boolean add_to_fav(String uid, String pid){
String sql = null;
sql = "INSERT INTO `FavItem` (`Pid`, `Uid`)\n" +
"VALUES\n" +
"\t("+pid+", "+uid+");\n";
return exec(sql);
}
public ResultSet get_carts(String uid, char t){
String sql;
if (t == 'n'){
sql = "select `Pid` as 'Product Id' , p.`Name` as 'Product Name' , (SELECT `Name` from `Brand` where `Bid` = `Brand_id`) as 'Brand', p.`Price` , p.`Category` , p.`Type` \n" +
"from `Cart` as c, `CartItem` as ci, `Product` as p\n" +
"WHERE c.`Uid` = '"+uid+"'\n" +
"and c.`Cid` = ci.`Cid`\n" +
"and p.`Productid` = ci.`Pid`;";
}
else {
sql = "select `Pid` as 'Product Id' , p.`Name` as 'Product Name' , (SELECT `Name` from `Brand` where `Bid` = `Brand_id`) as 'Brand', p.`Price` , p.`Category` , p.`Type` , p.`Shows` as 'Number of views'\n" +
"from `Cart` as c, `CartItem` as ci, `Product` as p\n" +
"WHERE c.`Uid` = '" + uid + "'\n" +
"and c.`Cid` = ci.`Cid`\n" +
"and p.`Productid` = ci.`Pid`;";
}
return get_result(sql);
}
public ResultSet get_fav(String uid, char t){
String sql;
if (t == 'n'){
sql = "select `Pid` as 'Product Id' , p.`Name` as 'Product Name' , (SELECT `Name` from `Brand` where `Bid` = `Brand_id`) as 'Brand', p.`Price` , p.`Category` , p.`Type` \n" +
" from `FavItem` as ci, `Product` as p\n" +
" WHERE ci.`Uid` = '"+uid+"'\n" +
" and p.`Productid` = ci.`Pid`;";
}
else {
sql = "select `Pid` as 'Product Id' , p.`Name` as 'Product Name' , (SELECT `Name` from `Brand` where `Bid` = `Brand_id`) as 'Brand', p.`Price` , p.`Category` , p.`Type` , p.`Shows` as 'Number of views'\n" +
" from `FavItem` as ci, `Product` as p\n" +
" WHERE ci.`Uid` = '"+uid+"'\n" +
" and p.`Productid` = ci.`Pid`;";
}
return get_result(sql);
}
public USER get_user(String uid){
USER user = new USER();
ResultSet resultSet = get_result("select * from `User` where `Uid` = '"+uid+"';");
try {
resultSet.first();
user.setID(uid);
user.setPayment(get_payment(resultSet.getString("PayID")));
user.setCart(get_cart(resultSet.getString("Cartid")));
user.setType(resultSet.getString("type"));
} catch (SQLException e) {
e.printStackTrace();
}
return user;
}
public Cart get_cart(String cid){
String sql = "select * from `Cart` where `Cid` = "+cid+"";
ResultSet resultSet = get_result(sql);
Cart cart = new Cart(cid);
try {
resultSet.first();
cart.setPrice(resultSet.getDouble(3));
} catch (SQLException e) {
e.printStackTrace();
}
return cart;
}
public Payment get_payment(String pid){
String sql = "select * from `Payment` where `Payid` = "+pid+"";
ResultSet resultSet = get_result(sql);
Payment pay = new Payment(pid);
try {
resultSet.first();
pay.setAmount(resultSet.getFloat(3));
} catch (SQLException e) {
e.printStackTrace();
}
return pay;
}
public Product get_product(String pid){
String sql = "select * from `Product` where `Productid` = "+pid+"";
ResultSet resultSet = get_result(sql);
Product p = new Product(pid);
try {
resultSet.first();
p.setPrice(resultSet.getFloat(4));
} catch (SQLException e) {
e.printStackTrace();
}
return p;
}
public int buy(String uid, String type) {
USER user = get_user(uid);
String sql = "";
int on = -1;
if (user.getCart().getPrice() == 0)
return -1;
if (type.equals("cash")){
sql = "INSERT INTO `Order` ( `Dprice`, `Pprice`, `Total_price`, `Cid`, `Pid`)\n" +
"VALUES\n" +
"\t( 150, "+String.valueOf(user.getCart().getPrice())+", "+String.valueOf(user.getCart().getPrice() + 150)+", '"+uid+"', "+user.getPayment().getId()+");\n";
exec(sql);
sql = "select max(`O_num`) from `Order`";
ResultSet resultSet = get_result(sql);
try {
resultSet.first();
on = resultSet.getInt(1);
} catch (SQLException e) {
e.printStackTrace();
}
return on;
}
else {
if (user.getCart().getPrice() > user.getPayment().getAmount())
return -1;
else {
double new_balance = user.getPayment().getAmount() - user.getCart().getPrice();
sql = "update `Payment` set `Amount` = " + String.valueOf(new_balance) + " where `Payid` = " + String.valueOf(user.getPayment().getId()) + ";";
String order = "INSERT INTO `Order` ( `Dprice`, `Pprice`, `Total_price`, `Cid`, `Pid`)\n" +
"VALUES\n" +
"\t( 100, "+String.valueOf(user.getCart().getPrice())+", "+String.valueOf(user.getCart().getPrice())+", '"+uid+"', "+user.getPayment().getId()+");\n";
exec(sql);
exec(order);
sql = "select max(`O_num`) from `Order`";
ResultSet resultSet = get_result(sql);
try {
resultSet.first();
on = resultSet.getInt(1);
} catch (SQLException e) {
e.printStackTrace();
}
return on;
}
}
}
public boolean make_order(String on, String uid){
String sql;
ResultSet resultSet = get_carts(uid, 's');
try {
while (resultSet.next()){
sql = "INSERT INTO `OrderItem` (`Pid`, `Order_id`)\n" +
"VALUES\n" +
"\t("+resultSet.getString(1)+", "+on+");\n";
exec(sql);
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return true;
}
public boolean make_admin(String uid){
String sql = "update `User` set `type` = 'a' where `Uid` = '"+uid+"'";
return exec(sql);
}
public boolean set_voucher(String uid, int value){
USER user = get_user(uid);
String pid = user.getPayment().getId();
value += user.getPayment().getAmount();
String sql = "update `Payment` set `Amount` = "+String.valueOf(value)+" where `Payid` = "+pid+" ;";
return exec(sql);
}
public ResultSet getStores(String uid){
return get_result("select `Name` from `Store` where `Owner` = '"+uid+"';");
}
public String get_store_id(String name){
String sql = "Select `Sid` from `Store` where `Name` = '"+name+"';";
ResultSet resultSet = get_result(sql);
String id = "-1";
try {
resultSet.first();
id = resultSet.getString(1);
} catch (SQLException e) {
e.printStackTrace();
}
return id;
}
public boolean add_to_store(String pid, String sid){
String check = "select `Type` , (select `Type` from `Store` where `Sid` = "+sid+") as 'sid' from `Product` where `Productid` = "+pid+"";
ResultSet resultSet = get_result(check);
boolean done = false;
try {
resultSet.first();
done = (Objects.equals(resultSet.getString(1), resultSet.getString(2)));
} catch (SQLException e) {
e.printStackTrace();
}
if (!done)
return false;
String sql = "INSERT INTO `Store_item` (`Sid`, `Pid`)\n" +
"VALUES\n" +
"\t("+sid+", "+pid+");\n";
return exec(sql);
}
public boolean suggest(String pname, String bname){
String sql = "INSERT INTO `Sug` ( `pname`, `bname`)\n" +
"VALUES\n" +
"\t( '"+pname+"', '"+bname+"');\n";
return exec(sql);
}
public ResultSet get_suggested(){
String sql = "select `bname` as 'Brand Name', `pname` as 'Product Name' from `Sug`;";
ResultSet resultSet = get_result(sql);
return resultSet;
}
private static DefaultTableModel buildTableModel(ResultSet rs) throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<String>> data = new Vector<>();
rs.beforeFirst();
while (rs.next()) {
Vector<String> vector = new Vector<>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++)
vector.addElement(rs.getString(columnIndex));
data.addElement(vector);
}
return new DefaultTableModel(data, columnNames);
}
public ResultSet get_all_in_store(String s) {
String sid = get_store_id(s);
String sql = "Select p.`Productid` as 'Product ID', p.`Name` as 'Product Name' , p.`price` , (SELECT `Name` from `Brand` where `Bid` = `Brand_id`) as 'Brand', p.`Shows` as 'Number of views'\n" +
"from `Product` as p, `Store_item` as s\n" +
"where s.`Sid` = "+sid+"\n" +
"and p.`Productid` = s.`Pid`";
return get_result(sql);
}
public void delete_cart_items(String cid){
String sql;
sql = "delete from `CartItem` where `Cid` = "+cid+"";
exec(sql);
sql = "update `Cart` set `price` = 0 where `Cid` = "+cid+" ";
exec(sql);
}
public Order get_order(String on){
String sql = "select * from `Order` where `O_num` = "+on+"";
ResultSet resultSet = get_result(sql);
Order order = new Order();
try {
resultSet.first();
order.setCustomer(get_user(resultSet.getString("Cid")));
order.setDeliver_price(resultSet.getDouble(2));
order.setID(on);
order.setProduct_price(resultSet.getDouble(3));
order.setTotal_price(order.getDeliver_price() + order.getProduct_price());
} catch (SQLException e) {
e.printStackTrace();
}
return order;
}
public String max_order(){
String sql = "select `Pid` , count(`Pid`) as 'count', `Name` \n" +
"from `OrderItem`, `Product`\n" +
"where `Pid` = `Productid`\n" +
"group by `Pid`\n" +
"ORDER BY count desc;";
ResultSet resultSet = get_result(sql);
try {
resultSet.first();
return resultSet.getString(3);
} catch (SQLException e) {
e.printStackTrace();
}
return "";
}
public String max_brand(){
String sql = "select `Pid` , count(`Pid`) as 'count', `Brand`.`Name` from `OrderItem`, `Brand`, `Product`\n" +
"where `Bid` = `Brand_id`\n" +
"and `Productid` = `Pid`\n" +
"group by `Pid`\n" +
"ORDER BY count desc;";
ResultSet resultSet = get_result(sql);
try {
resultSet.first();
return resultSet.getString(3);
} catch (SQLException e) {
e.printStackTrace();
}
return "";
}
public String n_shows(String sid){
String sql = "select sum(`Shows`) \n" +
"from `Product`, `Store_item` , `Store` \n" +
"where `Store`.`Sid` = `Store_item`.`Sid`\n" +
"and `Product`.`Productid` = `Store_item`.`Pid`\n" +
"and `Store`.`Owner` = '"+sid+"';";
ResultSet resultSet = get_result(sql);
try {
resultSet.first();
return resultSet.getString(1);
} catch (SQLException e) {
e.printStackTrace();
}
return "";
}
public String n_users(String sid){
String sql = "select count(distinct `Productid`)\n" +
"from `Product`, `Store_item` , `Store`, `OrderItem`\n" +
"where `Store`.`Sid` = `Store_item`.`Sid`\n" +
"and `Product`.`Productid` = `Store_item`.`Pid`\n" +
"and `Store`.`Owner` = '"+sid+"'\n" +
"and `OrderItem`.`Pid` = `Product`.`Productid`;";
ResultSet resultSet = get_result(sql);
try {
resultSet.first();
return resultSet.getString(1);
} catch (SQLException e) {
e.printStackTrace();
}
return "";
}
public boolean delete_cart_item(String cid, String pid){
String sql = "delete from `CartItem` where `Cid` = "+cid+" and `Pid` = "+pid+"";
return exec(sql);
}
}