-
Notifications
You must be signed in to change notification settings - Fork 1
/
listprod.jsp
79 lines (70 loc) · 2.38 KB
/
listprod.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
<%@ page import="java.sql.*,java.util.Locale" %>
<%@ page import="java.text.NumberFormat" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF8"%>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link href="style.css" rel="stylesheet" type="text/css">
<title>Ray's Grocery</title>
</head>
<body>
<h1>Collection</h1>
<form method="get" action="listprod.jsp">
<input type="text" name="productName" size="50">
<input type="submit" value="Submit"><input type="reset" value="Reset"> (Leave blank for all products)
</form>
<%
// Get product name to search for
String name = request.getParameter("productName");
boolean hasParameter = false;
String sql = "";
if (name == null)
name = "";
if (name.equals(""))
{
out.println("<h2>All Products</h2>");
sql = "SELECT productId, productName, productPrice FROM Product";
}
else
{
out.println("<h2>Products containing '" + name + "'</h2>");
hasParameter = true;
sql = "SELECT productId, productName, productPrice FROM Product WHERE productName LIKE ?";
name = '%' + name + '%';
}
String url = "jdbc:sqlserver://db:1433;DatabaseName=tempdb;";
String uid = "SA";
String pw = "YourStrong@Passw0rd";
NumberFormat currFormat = NumberFormat.getCurrencyInstance(Locale.CANADA);
try
{ // Load driver class
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch (java.lang.ClassNotFoundException e)
{
out.println("ClassNotFoundException: " +e);
}
try ( Connection con = DriverManager.getConnection(url, uid, pw);)
{
PreparedStatement pstmt = con.prepareStatement(sql);
if (hasParameter)
pstmt.setString(1, name);
ResultSet rst = pstmt.executeQuery();
out.println("<table class=table border = 1><tr><th></th><th>Product Name</th><th>Price</th></tr>");
while (rst.next())
{
out.print("<tr><td><a href=\"addcart.jsp?id=" + rst.getInt(1) + "&name=" + rst.getString(2)
+ "&price=" + rst.getDouble(3) + "\">Add to Cart</a></td>");
out.println("<td><a href=\"product.jsp?id=" + rst.getInt(1) + "\">" + rst.getString(2) + "</a></td>" + "<td>" + currFormat.format(rst.getDouble(3))
+ "</td></tr>");
}
out.println("</table>");
}
catch (SQLException ex)
{
out.println(ex);
}
%>
</body>
</html>