forked from rlawrenc/cosc_304
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmpQuery.jsp
74 lines (66 loc) · 1.91 KB
/
EmpQuery.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
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<title>Query Results Using JSP with PreparedStatements</title>
</head>
<body>
<%
String url = "jdbc:mysql://cosc304.ok.ubc.ca/workson";
String uid = "rlawrenc";
String pw = "todo";
try
{ // Load driver class
Class.forName("com.mysql.jdbc.Driver");
}
catch (java.lang.ClassNotFoundException e) {
System.err.println("ClassNotFoundException: " +e);
}
// Get parameters from request
String empName = request.getParameter("empname");
String deptNum = request.getParameter("deptnum");
try ( Connection con = DriverManager.getConnection(url, uid, pw);
Statement stmt = con.createStatement();)
{
String sql = "SELECT ename, salary, dno FROM emp";
boolean hasEmp = empName != null && !empName.equals("");
boolean hasDept = deptNum != null && !deptNum.equals("");
PreparedStatement pstmt = null;
ResultSet rst = null;
if (!hasEmp && !hasDept) {
pstmt = con.prepareStatement(sql);
rst = pstmt.executeQuery();
} else if (hasEmp) {
empName = "%" + empName + "%";
sql += " WHERE ename LIKE ?";
if (hasDept)
sql += " AND dno = ?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, empName);
if (hasDept)
pstmt.setString(2, deptNum);
rst = pstmt.executeQuery();
} else if (hasDept) {
sql += " WHERE dno = ?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, deptNum);
rst = pstmt.executeQuery();
}
// Note: Asking driver to return actual SQL executed
sql = pstmt.toString();
out.println("<h2>SQL Query: " + sql + "</h2>");
out.print("<table><tr><th>Name</th><th>Salary</th><th>dno</th></tr>");
while (rst.next()) {
out.println("<tr><td>" + rst.getString(1) + "</td>" + "<td>" + rst.getDouble(2) + "</td>" + "<td>"
+ rst.getString(3) + "</td></tr>");
}
out.println("</table></body></html>");
out.close();
}
catch (SQLException ex)
{
out.println(ex);
}
%>
</body>
</html>