在jsp中将数据库表格内容读出为一个表格,并在表格中添加超链接:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<head>
<title>查看作业</title>
<style type="text/css">
a:link { color:blue; }
a:visited { color:blue; }
a:hover { color:red; }
</style>
</head>
<table border="1" width="80%" align="center">
<tr>
<td width="10" valign="top"><div align="center">编号</div></td>
<td width="10" valign="top"><div align="center">班级</div></td>
<td width="10" valign="top"><div align="center">作业内容</div></td>
<td width="10" valign="top"><div align="center">作业附件</div></td>
</tr>
<%
request.setCharacterEncoding("UTF-8");
java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/login","root","123456");
String sql1="SELECT*from homework";
Statement statemenet = (Statement) conn.createStatement();
ResultSet rs1 = statemenet.executeQuery(sql1);
while(rs1.next()){
%>
<tr>
<td width="10"><div align="center"> <%=rs1.getString("编号")%></div></td>
<td width="10"><div align="center"> <%=rs1.getString("班级")%></div></td>
<td width="10"><div align="center"> <%=rs1.getString("作业内容")%></div></td>
<td width="5"><div align="center"><a href="download_stu.jsp" style="text-decoration:none">下载</a></div></td>
</tr> <%
} rs1.close();
%>
</table>
用Navicat Premium在数据库中建的表为:
上述程序运行后的结果为: