【JSP开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了JSP开源代码-EX8-20.jsp的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>JSP通用范例大全</title>
</head>
<body>
<%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); //加载JDBC驱动
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=school"; //school为你的数据库
String user="sa"; //连接数据库的用户名
String password="123"; //连接数据库的密码
Connection conn= DriverManager.getConnection(url,user,pass
word); //数据库连接对象
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); //SQL语句对象
String
sql="select C_no,count(S_no) from SC group by C_no"; //创建SQL语句
ResultSet rs=stmt.executeQuery(sql); //执行SQL语句并取得结果集
%>
<table border width=50%>
<caption>各个课程号及相应的选课人数</caption>
<tr>
<th>课程号</th>
<th>学生数</th>
</tr>
<%
while(rs.next()) {%>
<tr align=center>
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
</tr>
<%}%>
</table>
<br>
<%
sql="select S_no from SC group by S_no having count(*)>2"; //创建SQL语句
rs=stmt.executeQuery(sql); //执行SQL语句并取得结果集
%>
<table border width=50%>
<caption>选修了3门以上
课程的学生号</caption>
<tr>
<th>课程号</th>
</tr>
<%
while(rs.next()) {%>
<tr align=center>
<td><%=rs.getString(1)%></td>
</tr>
<%}%>
</table>
<%rs.close(); //关闭结果集
stmt.close(); //关闭SQL语句对象
conn.close(); //关闭数据库
%>
</body>
</html>