標籤:request .sql localhost html driver ansi character tchar tco
在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="download_stu.jsp" style="text-decoration:none">下載</a></div></td>33 </tr>34 35 <%36 } rs1.close(); 37 %>38 </table>
用Navicat Premium在資料庫中建的表為:
上述程式運行後的結果為:
jsp中讀取資料庫內容(一)