首页 > 代码库 > jsp中读取数据库内容(一)

jsp中读取数据库内容(一)

在jsp中将数据库表格内容读出为一个表格,并在表格中添加超链接:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <%@ page import="java.io.*,java.util.*,java.sql.*"%>
 5 <head>
 6 <title>查看作业</title>
 7 <style type="text/css">
 8   a:link { color:blue; }
 9   a:visited { color:blue; }
10   a:hover { color:red; }
11 </style>
12 </head>
13 <table border="1" width="80%" align="center">
14 <tr>
15  <td width="10" valign="top"><div align="center">编号</div></td>
16  <td width="10" valign="top"><div align="center">班级</div></td>
17  <td width="10" valign="top"><div align="center">作业内容</div></td>
18  <td width="10" valign="top"><div align="center">作业附件</div></td>
19  </tr>
20 <%
21 request.setCharacterEncoding("UTF-8");
22 java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/login","root","123456");
23 String sql1="SELECT*from homework";
24 Statement statemenet = (Statement) conn.createStatement();
25 ResultSet rs1 = statemenet.executeQuery(sql1);
26 while(rs1.next()){
27 %>
28    <tr>
29     <td width="10"><div align="center"> <%=rs1.getString("编号")%></div></td>
30     <td width="10"><div align="center"> <%=rs1.getString("班级")%></div></td>
31     <td width="10"><div align="center"> <%=rs1.getString("作业内容")%></div></td>
32     <td width="5"><div align="center"><a href="http://www.mamicode.com/download_stu.jsp" style="text-decoration:none">下载</a></div></td>
33    </tr>
34                   
35 <%
36 } rs1.close(); 
37 %>
38 </table>

用Navicat Premium在数据库中建的表为:

技术分享

上述程序运行后的结果为:

技术分享

jsp中读取数据库内容(一)