forked from rlawrenc/cosc_304
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorksOnDeptEmpProj.jsp
89 lines (74 loc) · 2.32 KB
/
WorksOnDeptEmpProj.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
<%@ page import="java.sql.*" %>
<html>
<head>
<title>Querying WorksOn using JSP</title>
</head>
<body>
<%
try
{ // Load driver class
Class.forName("com.mysql.jdbc.Driver");
}
catch (java.lang.ClassNotFoundException e) {
System.err.println("ClassNotFoundException: " +e);
}
String url = "jdbc:mysql://cosc304.ok.ubc.ca/workson";
String uid = "fill-in";
String pw = "fill-in";
try (Connection con = DriverManager.getConnection(url, uid, pw);)
{
// Create prepared statement to retrieve projects as going to repeat multiple times
PreparedStatement pstmt = con.prepareStatement("SELECT pno, pname, budget FROM proj WHERE dno = ?");
// Create prepared statement to retrieve employees as going to repeat multiple times
PreparedStatement pstmtEmp = con.prepareStatement("SELECT eno, ename, bdate, salary FROM emp WHERE dno = ?");
// Retrieve list of departments
Statement stmt = con.createStatement();
ResultSet rst = stmt.executeQuery("SELECT dno, dname FROM dept");
// Iterate through department list
while (rst.next())
{
String dno = rst.getString(1);
out.println("<h2>"+dno+" : "+rst.getString(2)+"</h2>");
// Retrieve projects for department
pstmt.setString(1, dno);
ResultSet rstProj = pstmt.executeQuery();
int count = 0;
if (rstProj.next())
{
out.println("<h3>Project List</h3><table><th>pno</th><th>pname</th><th>budget</th>");
do
{
out.println("<tr><td>"+rstProj.getString(1)+"</td><td>"+rstProj.getString(2)+"</td><td>"
+rstProj.getString(3)+"</td></tr>");
} while (rstProj.next());
out.println("</table>");
}
else
out.println("<h3>No projects.</h3>");
rstProj.close();
// Retrieve employees for department
pstmtEmp.setString(1, dno);
ResultSet rstEmp = pstmtEmp.executeQuery();
if (rstEmp.next())
{
out.println("<h3>Employee List</h3><table><th>eno</th><th>ename</th><th>birth date</th><th>salary</th>");
do
{
out.println("<tr><td>"+rstEmp.getString(1)+"</td><td>"+rstEmp.getString(2)+"</td><td>"
+rstEmp.getString(3)+"</td><td>"+rstEmp.getString(4)+"</td></tr>");
} while (rstEmp.next());
out.println("</table>");
}
else
{
out.println("<H3>No employees.</H3>");
}
}
out.println("</table>");
}
catch (SQLException ex)
{ out.println(ex);
}
%>
</body>
</html>