forked from jopereira/java-tpcw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TPCW_Database.mckoi.java
1413 lines (1243 loc) · 39.2 KB
/
TPCW_Database.mckoi.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
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* TPCW_Database.java - Contains all of the code involved with database
* accesses, including all of the JDBC calls. These
* functions are called by many of the servlets.
*
************************************************************************
*
* This is part of the the Java TPC-W distribution,
* written by Harold Cain, Tim Heil, Milo Martin, Eric Weglarz, and Todd
* Bezenek. University of Wisconsin - Madison, Computer Sciences
* Dept. and Dept. of Electrical and Computer Engineering, as a part of
* Prof. Mikko Lipasti's Fall 1999 ECE 902 course.
*
* Copyright (C) 1999, 2000 by Harold Cain, Timothy Heil, Milo Martin,
* Eric Weglarz, Todd Bezenek.
*
* This source code is distributed "as is" in the hope that it will be
* useful. It comes with no warranty, and no author or distributor
* accepts any responsibility for the consequences of its use.
*
* Everyone is granted permission to copy, modify and redistribute
* this code under the following conditions:
*
* This code is distributed for non-commercial use only.
* Please contact the maintainer for restrictions applying to
* commercial use of these tools.
*
* Permission is granted to anyone to make or distribute copies
* of this code, either as received or modified, in any
* medium, provided that all copyright notices, permission and
* nonwarranty notices are preserved, and that the distributor
* grants the recipient permission for further redistribution as
* permitted by this document.
*
* Permission is granted to distribute this code in compiled
* or executable form under the same conditions that apply for
* source code, provided that either:
*
* A. it is accompanied by the corresponding machine-readable
* source code,
* B. it is accompanied by a written offer, with no time limit,
* to give anyone a machine-readable copy of the corresponding
* source code in return for reimbursement of the cost of
* distribution. This written offer must permit verbatim
* duplication by anyone, or
* C. it is distributed by someone who received only the
* executable form, and is accompanied by a copy of the
* written offer of source code that they received concurrently.
*
* In other words, you are welcome to use, share and improve this codes.
* You are forbidden to forbid anyone else to use, share and improve what
* you give them.
*
************************************************************************
*
* Changed 2003 by Jan Kiefer.
*
************************************************************************/
import java.io.*;
import java.net.URL;
import java.sql.*;
import java.lang.Math.*;
import java.util.*;
import java.sql.Date;
import java.sql.Timestamp;
public class TPCW_Database {
static String driverName = "@jdbc.driver@";
static String jdbcPath = "@jdbc.path@";
// Pool of *available* connections.
static Vector availConn = new Vector(0);
static int checkedOut = 0;
static int totalConnections = 0;
static int createdConnections = 0;
static int closedConnections = 0;
// private static final boolean use_connection_pool = false;
private static final boolean use_connection_pool = true;
public static final int maxConn = @jdbc.connPoolMax@;
// Here's what the db line looks like for postgres
//public static final String url = "jdbc:postgresql://eli.ece.wisc.edu/tpcwb";
// Get a connection from the pool.
public static synchronized Connection getConnection() {
if (!use_connection_pool) {
return getNewConnection();
} else {
Connection con = null;
while (availConn.size() > 0) {
// Pick the first Connection in the Vector
// to get round-robin usage
con = (Connection) availConn.firstElement();
availConn.removeElementAt(0);
try {
if (con.isClosed()) {
continue;
}
}
catch (SQLException e) {
e.printStackTrace();
continue;
}
// Got a connection.
checkedOut++;
return(con);
}
if (maxConn == 0 || checkedOut < maxConn) {
con = getNewConnection();
totalConnections++;
}
if (con != null) {
checkedOut++;
}
return con;
}
}
// Return a connection to the pool.
public static synchronized void returnConnection(Connection con)
throws java.sql.SQLException
{
if (!use_connection_pool) {
con.close();
} else {
checkedOut--;
availConn.addElement(con);
}
}
// Get a new connection to DB2
public static Connection getNewConnection() {
try {
Class.forName(driverName);
// Class.forName("postgresql.Driver");
// Create URL for specifying a DBMS
Connection con;
while(true) {
try {
// con = DriverManager.getConnection("jdbc:postgresql://eli.ece.wisc.edu/tpcw", "milo", "");
con = DriverManager.getConnection(jdbcPath);
break;
} catch (java.sql.SQLException ex) {
System.err.println("Error getting connection: " +
ex.getMessage() + " : " +
ex.getErrorCode() +
": trying to get connection again.");
ex.printStackTrace();
java.lang.Thread.sleep(1000);
}
}
con.setAutoCommit(false);
createdConnections++;
return con;
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
return null;
}
public static String[] getName(int c_id) {
String name[] = new String[2];
try {
// Prepare SQL
// out.println("About to call getConnection!");
// out.flush();
Connection con = getConnection();
// out.println("About to preparestatement!");
// out.flush();
PreparedStatement get_name = con.prepareStatement
(@sql.getName@);
// Set parameter
get_name.setInt(1, c_id);
// out.println("About to execute query!");
// out.flush();
ResultSet rs = get_name.executeQuery();
// Results
rs.next();
name[0] = rs.getString("c_fname");
name[1] = rs.getString("c_lname");
rs.close();
get_name.close();
con.commit();
returnConnection(con);
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
return name;
}
public static Book getBook(int i_id) {
Book book = null;
try {
// Prepare SQL
Connection con = getConnection();
PreparedStatement statement = con.prepareStatement
(@sql.getBook@);
// Set parameter
statement.setInt(1, i_id);
ResultSet rs = statement.executeQuery();
// Results
rs.next();
book = new Book(rs);
rs.close();
statement.close();
con.commit();
returnConnection(con);
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
return book;
}
public static Customer getCustomer(String UNAME){
Customer cust = null;
try {
// Prepare SQL
Connection con = getConnection();
PreparedStatement statement = con.prepareStatement
(@sql.getCustomer@);
// Set parameter
statement.setString(1, UNAME);
ResultSet rs = statement.executeQuery();
// Results
if(rs.next())
cust = new Customer(rs);
else {
System.err.println("ERROR: NULL returned in getCustomer!");
rs.close();
statement.close();
returnConnection(con);
return null;
}
statement.close();
con.commit();
returnConnection(con);
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
return cust;
}
public static Vector doSubjectSearch(String search_key) {
Vector vec = new Vector();
try {
// Prepare SQL
Connection con = getConnection();
PreparedStatement statement = con.prepareStatement
(@sql.doSubjectSearch@);
// Set parameter
statement.setString(1, search_key);
ResultSet rs = statement.executeQuery();
// Results
while(rs.next()) {
vec.addElement(new Book(rs));
}
rs.close();
statement.close();
con.commit();
returnConnection(con);
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
return vec;
}
public static Vector doTitleSearch(String search_key) {
Vector vec = new Vector();
try {
// Prepare SQL
Connection con = getConnection();
PreparedStatement statement = con.prepareStatement
(@sql.doTitleSearch@);
// Set parameter
statement.setString(1, search_key+"%");
ResultSet rs = statement.executeQuery();
// Results
while(rs.next()) {
vec.addElement(new Book(rs));
}
rs.close();
statement.close();
con.commit();
returnConnection(con);
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
return vec;
}
public static Vector doAuthorSearch(String search_key) {
Vector vec = new Vector();
try {
// Prepare SQL
Connection con = getConnection();
PreparedStatement statement = con.prepareStatement
(@sql.doAuthorSearch@);
// Set parameter
statement.setString(1, search_key+"%");
ResultSet rs = statement.executeQuery();
// Results
while(rs.next()) {
vec.addElement(new Book(rs));
}
rs.close();
statement.close();
con.commit();
returnConnection(con);
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
return vec;
}
public static Vector getNewProducts(String subject) {
Vector vec = new Vector(); // Vector of Books
try {
// Prepare SQL
Connection con = getConnection();
PreparedStatement statement = con.prepareStatement
(@sql.getNewProducts@);
// Set parameter
statement.setString(1, subject);
ResultSet rs = statement.executeQuery();
// Results
while(rs.next()) {
vec.addElement(new ShortBook(rs));
}
rs.close();
statement.close();
con.commit();
returnConnection(con);
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
return vec;
}
public static Vector getBestSellers(String subject) {
Vector vec = new Vector(); // Vector of Books
try {
// Prepare SQL
Connection con = getConnection();
//The following is the original, unoptimized best sellers query.
PreparedStatement statement = con.prepareStatement
(@sql.getBestSellers@);
//This is Mikko's optimized version, which depends on the fact that
//A table named "bestseller" has been created.
/*PreparedStatement statement = con.prepareStatement
("SELECT bestseller.i_id, i_title, a_fname, a_lname, ol_qty " +
"FROM item, bestseller, author WHERE item.i_subject = ?" +
" AND item.i_id = bestseller.i_id AND item.i_a_id = author.a_id " +
" ORDER BY ol_qty DESC FETCH FIRST 50 ROWS ONLY");*/
// Set parameter
statement.setString(1, subject);
ResultSet rs = statement.executeQuery();
// Results
while(rs.next()) {
vec.addElement(new ShortBook(rs));
}
rs.close();
statement.close();
con.commit();
returnConnection(con);
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
return vec;
}
public static void getRelated(int i_id, Vector i_id_vec, Vector i_thumbnail_vec) {
try {
// Prepare SQL
Connection con = getConnection();
PreparedStatement statement = con.prepareStatement
(@sql.getRelated@);
// Set parameter
statement.setInt(1, i_id);
ResultSet rs = statement.executeQuery();
// Clear the vectors
i_id_vec.removeAllElements();
i_thumbnail_vec.removeAllElements();
// Results
while(rs.next()) {
i_id_vec.addElement(new Integer(rs.getInt(1)));
i_thumbnail_vec.addElement(rs.getString(2));
}
rs.close();
statement.close();
con.commit();
returnConnection(con);
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
}
public static synchronized void adminUpdate(int i_id, double cost, String image, String thumbnail) {
try {
// Prepare SQL
Connection con = getConnection();
PreparedStatement statement = con.prepareStatement
(@sql.adminUpdate@);
// Set parameter
statement.setDouble(1, cost);
statement.setString(2, image);
statement.setString(3, thumbnail);
statement.setInt(4, i_id);
statement.executeUpdate();
statement.close();
PreparedStatement related = con.prepareStatement
(@sql.adminUpdate.related@);
// Set parameter
related.setInt(1, i_id);
related.setInt(2, i_id);
ResultSet rs = related.executeQuery();
// changed by jk - 04/2003
int[] related_items = new int[5];
// Results
int counter = 0;
int last = 0;
while(rs.next() && counter<5) {
last = rs.getInt(1);
related_items[counter] = last;
counter++;
}
// This is the case for the situation where there are not 5 related books.
for (int i=counter; i<5; i++) {
last++;
related_items[i] = last;
}
rs.close();
related.close();
{
// Prepare SQL
statement = con.prepareStatement
(@sql.adminUpdate.related1@);
// Set parameter
statement.setInt(1, related_items[0]);
statement.setInt(2, related_items[1]);
statement.setInt(3, related_items[2]);
statement.setInt(4, related_items[3]);
statement.setInt(5, related_items[4]);
statement.setInt(6, i_id);
statement.executeUpdate();
}
statement.close();
con.commit();
returnConnection(con);
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
}
public static String GetUserName(int C_ID){
String u_name = null;
try {
// Prepare SQL
Connection con = getConnection();
PreparedStatement get_user_name = con.prepareStatement
(@sql.getUserName@);
// Set parameter
get_user_name.setInt(1, C_ID);
ResultSet rs = get_user_name.executeQuery();
// Results
rs.next();
u_name = rs.getString("c_uname");
rs.close();
get_user_name.close();
con.commit();
returnConnection(con);
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
return u_name;
}
public static String GetPassword(String C_UNAME){
String passwd = null;
try {
// Prepare SQL
Connection con = getConnection();
PreparedStatement get_passwd = con.prepareStatement
(@sql.getPassword@);
// Set parameter
get_passwd.setString(1, C_UNAME);
ResultSet rs = get_passwd.executeQuery();
// Results
rs.next();
passwd = rs.getString("c_passwd");
rs.close();
get_passwd.close();
con.commit();
returnConnection(con);
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
return passwd;
}
//This function gets the value of I_RELATED1 for the row of
//the item table corresponding to I_ID
private static int getRelated1(int I_ID, Connection con){
int related1 = -1;
try {
PreparedStatement statement = con.prepareStatement
(@sql.getRelated1@);
statement.setInt(1, I_ID);
ResultSet rs = statement.executeQuery();
rs.next();
related1 = rs.getInt(1);//Is 1 the correct index?
rs.close();
statement.close();
}catch (java.lang.Exception ex) {
ex.printStackTrace();
}
return related1;
}
public static Order GetMostRecentOrder(String c_uname, Vector order_lines){
try {
order_lines.removeAllElements();
int order_id;
Order order;
// Prepare SQL
Connection con = getConnection();
// System.out.println("cust_id: " + getCustomer(c_uname).c_id);
{
// *** Get the o_id of the most recent order for this user
PreparedStatement get_most_recent_order_id = con.prepareStatement
(@sql.getMostRecentOrder.id@);
// Set parameter
get_most_recent_order_id.setString(1, c_uname);
ResultSet rs = get_most_recent_order_id.executeQuery();
if (rs.next()) {
order_id = rs.getInt("o_id");
} else {
// There is no most recent order
rs.close();
get_most_recent_order_id.close();
con.commit();
returnConnection(con);
return null;
}
rs.close();
get_most_recent_order_id.close();
}
{
// *** Get the order info for this o_id
PreparedStatement get_order = con.prepareStatement
(@sql.getMostRecentOrder.order@);
// Set parameter
get_order.setInt(1, order_id);
ResultSet rs2 = get_order.executeQuery();
// Results
if (!rs2.next()) {
// FIXME - This case is due to an error due to a database population error
con.commit();
rs2.close();
// get_order.close();
returnConnection(con);
return null;
}
order = new Order(rs2);
rs2.close();
get_order.close();
}
{
// *** Get the order_lines for this o_id
PreparedStatement get_order_lines = con.prepareStatement
(@sql.getMostRecentOrder.lines@);
// Set parameter
get_order_lines.setInt(1, order_id);
ResultSet rs3 = get_order_lines.executeQuery();
// Results
while(rs3.next()) {
order_lines.addElement(new OrderLine(rs3));
}
rs3.close();
get_order_lines.close();
}
con.commit();
returnConnection(con);
return order;
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
return null;
}
// ********************** Shopping Cart code below *************************
// Called from: TPCW_shopping_cart_interaction
public static int createEmptyCart(){
int SHOPPING_ID = 0;
// boolean success = false;
Connection con = null;
try {
con = getConnection();
}
catch (java.lang.Exception ex) {
ex.printStackTrace();
}
//while(success == false) {
try {
PreparedStatement get_next_id = con.prepareStatement
(@sql.createEmptyCart@);
synchronized(Cart.class) {
ResultSet rs = get_next_id.executeQuery();
rs.next();
SHOPPING_ID = rs.getInt(1);
rs.close();
PreparedStatement insert_cart = con.prepareStatement
(@sql.createEmptyCart.insert@);
insert_cart.executeUpdate();
get_next_id.close();
con.commit();
}
returnConnection(con);
}catch (java.lang.Exception ex) {
ex.printStackTrace();
}
return SHOPPING_ID;
}
public static Cart doCart(int SHOPPING_ID, Integer I_ID, Vector ids, Vector quantities) {
Cart cart = null;
try {
Connection con = getConnection();
if (I_ID != null) {
addItem(con, SHOPPING_ID, I_ID.intValue());
}
refreshCart(con, SHOPPING_ID, ids, quantities);
addRandomItemToCartIfNecessary(con, SHOPPING_ID);
resetCartTime(con, SHOPPING_ID);
cart = TPCW_Database.getCart(con, SHOPPING_ID, 0.0);
// Close connection
con.commit();
returnConnection(con);
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
return cart;
}
//This function finds the shopping cart item associated with SHOPPING_ID
//and I_ID. If the item does not already exist, we create one with QTY=1,
//otherwise we increment the quantity.
private static void addItem(Connection con, int SHOPPING_ID, int I_ID){
try {
// Prepare SQL
PreparedStatement find_entry = con.prepareStatement
(@sql.addItem@);
// Set parameter
find_entry.setInt(1, SHOPPING_ID);
find_entry.setInt(2, I_ID);
ResultSet rs = find_entry.executeQuery();
// Results
if(rs.next()) {
//The shopping cart id, item pair were already in the table
int currqty = rs.getInt("scl_qty");
currqty+=1;
PreparedStatement update_qty = con.prepareStatement
(@sql.addItem.update@);
update_qty.setInt(1, currqty);
update_qty.setInt(2, SHOPPING_ID);
update_qty.setInt(3, I_ID);
update_qty.executeUpdate();
update_qty.close();
} else {//We need to add a new row to the table.
//Stick the item info in a new shopping_cart_line
PreparedStatement put_line = con.prepareStatement
(@sql.addItem.put@);
put_line.setInt(1, SHOPPING_ID);
put_line.setInt(2, 1);
put_line.setInt(3, I_ID);
put_line.executeUpdate();
put_line.close();
}
rs.close();
find_entry.close();
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
}
private static void refreshCart(Connection con, int SHOPPING_ID, Vector ids,
Vector quantities){
int i;
try {
for(i = 0; i < ids.size(); i++){
String I_IDstr = (String) ids.elementAt(i);
String QTYstr = (String) quantities.elementAt(i);
int I_ID = Integer.parseInt(I_IDstr);
int QTY = Integer.parseInt(QTYstr);
if(QTY == 0) { // We need to remove the item from the cart
PreparedStatement statement = con.prepareStatement
(@sql.refreshCart.remove@);
statement.setInt(1, SHOPPING_ID);
statement.setInt(2, I_ID);
statement.executeUpdate();
statement.close();
}
else { //we update the quantity
PreparedStatement statement = con.prepareStatement
(@sql.refreshCart.update@);
statement.setInt(1, QTY);
statement.setInt(2, SHOPPING_ID);
statement.setInt(3, I_ID);
statement.executeUpdate();
statement.close();
}
}
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
}
private static void addRandomItemToCartIfNecessary(Connection con, int SHOPPING_ID){
// check and see if the cart is empty. If it's not, we do
// nothing.
int related_item = 0;
try {
// Check to see if the cart is empty
PreparedStatement get_cart = con.prepareStatement
(@sql.addRandomItemToCartIfNecessary@);
get_cart.setInt(1, SHOPPING_ID);
ResultSet rs = get_cart.executeQuery();
rs.next();
if (rs.getInt(1) == 0) {
// Cart is empty
int rand_id = TPCW_Util.getRandomI_ID();
related_item = getRelated1(rand_id,con);
addItem(con, SHOPPING_ID, related_item);
}
rs.close();
get_cart.close();
}catch (java.lang.Exception ex) {
ex.printStackTrace();
System.out.println("Adding entry to shopping cart failed: shopping id = " + SHOPPING_ID + " related_item = " + related_item);
}
}
// Only called from this class
private static void resetCartTime(Connection con, int SHOPPING_ID){
try {
PreparedStatement statement = con.prepareStatement
(@sql.resetCartTime@);
// Set parameter
statement.setInt(1, SHOPPING_ID);
statement.executeUpdate();
statement.close();
}catch (java.lang.Exception ex) {
ex.printStackTrace();
}
}
public static Cart getCart(int SHOPPING_ID, double c_discount) {
Cart mycart = null;
try {
Connection con = getConnection();
mycart = getCart(con, SHOPPING_ID, c_discount);
con.commit();
returnConnection(con);
}catch (java.lang.Exception ex) {
ex.printStackTrace();
}
return mycart;
}
//time .05s
private static Cart getCart(Connection con, int SHOPPING_ID, double c_discount){
Cart mycart = null;
try {
PreparedStatement get_cart = con.prepareStatement
(@sql.getCart@);
get_cart.setInt(1, SHOPPING_ID);
ResultSet rs = get_cart.executeQuery();
mycart = new Cart(rs, c_discount);
rs.close();
get_cart.close();
}catch (java.lang.Exception ex) {
ex.printStackTrace();
}
return mycart;
}
// ************** Customer / Order code below *************************
//This should probably return an error code if the customer
//doesn't exist, but ...
public static synchronized void refreshSession(int C_ID) {
try {
// Prepare SQL
Connection con = getConnection();
// changed by jk - 04/2003
// sql.refreshSession="UPDATE customer SET c_login = CURRENT TIMESTAMP, c_expiration = CURRENT TIMESTAMP + 2 HOURS WHERE c_id = ?"
PreparedStatement updateLogin = con.prepareStatement
(@sql.refreshSession@);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR_OF_DAY, 2);
Timestamp currentTime = new Timestamp(cal.getTimeInMillis());
// Set parameter
updateLogin.setTimestamp(1, currentTime);
updateLogin.setInt(2, C_ID);
updateLogin.executeUpdate();
con.commit();
updateLogin.close();
returnConnection(con);
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
}
public static synchronized Customer createNewCustomer(Customer cust) {
try {
// Get largest customer ID already in use.
Connection con = getConnection();
cust.c_discount = (int) (java.lang.Math.random() * 51);
cust.c_balance =0.0;
cust.c_ytd_pmt = 0.0;
// FIXME - Use SQL CURRENT_TIME to do this
cust.c_last_visit = new Date(System.currentTimeMillis());
cust.c_since = new Date(System.currentTimeMillis());
cust.c_login = new Date(System.currentTimeMillis());
cust.c_expiration = new Date(System.currentTimeMillis() +
7200000);//milliseconds in 2 hours
PreparedStatement insert_customer_row = con.prepareStatement
(@sql.createNewCustomer@);
insert_customer_row.setString(4,cust.c_fname);
insert_customer_row.setString(5,cust.c_lname);
insert_customer_row.setString(7,cust.c_phone);
insert_customer_row.setString(8,cust.c_email);
insert_customer_row.setDate(9, new
java.sql.Date(cust.c_since.getTime()));
insert_customer_row.setDate(10, new java.sql.Date(cust.c_last_visit.getTime()));
insert_customer_row.setDate(11, new java.sql.Date(cust.c_login.getTime()));
insert_customer_row.setDate(12, new java.sql.Date(cust.c_expiration.getTime()));
insert_customer_row.setDouble(13, cust.c_discount);
insert_customer_row.setDouble(14, cust.c_balance);
insert_customer_row.setDouble(15, cust.c_ytd_pmt);
insert_customer_row.setDate(16, new java.sql.Date(cust.c_birthdate.getTime()));
insert_customer_row.setString(17, cust.c_data);
cust.addr_id = enterAddress(con,
cust.addr_street1,
cust.addr_street2,
cust.addr_city,
cust.addr_state,
cust.addr_zip,
cust.co_name);
PreparedStatement get_max_id = con.prepareStatement
(@sql.createNewCustomer.maxId@);
synchronized(Customer.class) {
// Set parameter
ResultSet rs = get_max_id.executeQuery();
// Results
rs.next();
cust.c_id = rs.getInt(1);//Is 1 the correct index?
rs.close();
cust.c_id+=1;
cust.c_uname = TPCW_Util.DigSyl(cust.c_id, 0);
cust.c_passwd = cust.c_uname.toLowerCase();
insert_customer_row.setInt(1, cust.c_id);
insert_customer_row.setString(2,cust.c_uname);
insert_customer_row.setString(3,cust.c_passwd);
insert_customer_row.setInt(6, cust.addr_id);
insert_customer_row.executeUpdate();
con.commit();
insert_customer_row.close();
}
get_max_id.close();
returnConnection(con);
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
return cust;
}
//BUY CONFIRM
public static synchronized BuyConfirmResult doBuyConfirm(int shopping_id,
int customer_id,
String cc_type,
long cc_number,
String cc_name,
Date cc_expiry,
String shipping) {
BuyConfirmResult result = new BuyConfirmResult();
try {
Connection con = getConnection();
double c_discount = getCDiscount(con, customer_id);
result.cart = getCart(con, shopping_id, c_discount);
int ship_addr_id = getCAddr(con, customer_id);
result.order_id = enterOrder(con, customer_id, result.cart, ship_addr_id, shipping, c_discount);
enterCCXact(con, result.order_id, cc_type, cc_number, cc_name, cc_expiry, result.cart.SC_TOTAL, ship_addr_id);
clearCart(con, shopping_id);
con.commit();
returnConnection(con);
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
return result;
}
public static synchronized BuyConfirmResult doBuyConfirm(int shopping_id,
int customer_id,
String cc_type,
long cc_number,
String cc_name,
Date cc_expiry,
String shipping,
String street_1, String street_2,
String city, String state,
String zip, String country) {