forked from rlawrenc/cosc_304
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JdbcQuery.jsp
39 lines (34 loc) · 854 Bytes
/
JdbcQuery.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
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<title>Query Results Using JSP</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);
}
try ( Connection con = DriverManager.getConnection(url, uid, pw);
Statement stmt = con.createStatement();)
{
ResultSet rst = stmt.executeQuery("SELECT ename,salary FROM emp");
out.println("<table><tr><th>Name</th><th>Salary</th></tr>");
while (rst.next())
{ out.println("<tr><td>"+rst.getString(1)+"</td>"+"<td>"+rst.getDouble(2)+"</td></tr>");
}
out.println("</table>");
}
catch (SQLException ex)
{ out.println(ex);
}
%>
</body>
</html>