-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdo_purchase.jsp
96 lines (83 loc) · 3.45 KB
/
do_purchase.jsp
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
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" import="database.*" import="java.util.*" errorPage="" %>
<%@include file="welcome.jsp" %>
<%
if(session.getAttribute("name")!=null)
{
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CSE135</title>
</head>
<body>
<div style="width:20%; position:absolute; top:50px; left:0px; height:90%; border-bottom:1px; border-bottom-style:solid;border-left:1px; border-left-style:solid;border-right:1px; border-right-style:solid;border-top:1px; border-top-style:solid;">
<table width="100%">
<tr><td><a href="products_browsing.jsp" target="_self">Show Produts</a></td></tr>
<tr><td><a href="buyShoppingCart.jsp" target="_self">Buy Shopping Cart</a></td></tr>
</table>
</div>
<div style="width:79%; position:absolute; top:50px; right:0px; height:90%; border-bottom:1px; border-bottom-style:solid;border-left:1px; border-left-style:solid;border-right:1px; border-right-style:solid;border-top:1px; border-top-style:solid;">
<p><table align="center" width="80%" style="border-bottom-width:2px; border-top-width:2px; border-bottom-style:solid; border-top-style:solid">
<tr><td align="left"><font size="+3">
<%
String uName=(String)session.getAttribute("name");
int userID = (Integer)session.getAttribute("userID");
String role = (String)session.getAttribute("role");
String card=null;
int card_num=0;
try {card=request.getParameter("card"); }catch(Exception e){card=null;}
try
{
card_num = Integer.parseInt(card);
if(card_num>0)
{
Connection conn=null;
Statement stmt=null;
try
{
String SQL_copy="INSERT INTO sales (uid, pid, quantity, price) select c.uid, c.pid, c.quantity, c.price from carts c where c.uid="+userID+";";
String SQL="delete from carts where uid="+userID+";";
try{Class.forName("org.postgresql.Driver");}catch(Exception e){System.out.println("Driver error");}
String url="jdbc:postgresql://127.0.0.1:5432/P1";
String user="postgres";
String password="880210";
conn =DriverManager.getConnection(url, user, password);
stmt =conn.createStatement();
try{
conn.setAutoCommit(false);
/**record log,i.e., sales table**/
stmt.execute(SQL_copy);
stmt.execute(SQL);
conn.commit();
conn.setAutoCommit(true);
out.println("Dear customer '"+uName+"', Thanks for your purchasing.<br> Your card '"+card+"' has been successfully proved. <br>We will ship the products soon.");
out.println("<br><font size=\"+2\" color=\"#990033\"> <a href=\"products_browsing.jsp\" target=\"_self\">Continue purchasing</a></font>");
}
catch(Exception e)
{
out.println("Fail! Please try again <a href=\"purchase.jsp\" target=\"_self\">Purchase page</a>.<br><br>");
}
conn.close();
}
catch(Exception e)
{
out.println("<font color='#ff0000'>Error.<br><a href=\"purchase.jsp\" target=\"_self\"><i>Go Back to Purchase Page.</i></a></font><br>");
}
}
else
{
out.println("Fail! Please input valid credit card numnber. <br> Please <a href=\"purchase.jsp\" target=\"_self\">buy it</a> again.");
}
}
catch(Exception e)
{
out.println("Fail! Please input valid credit card numnber. <br> Please <a href=\"purchase.jsp\" target=\"_self\">buy it</a> again.");
}
%>
</font><br>
</td></tr>
</table>
</div>
</body>
</html>
<%}%>