jsp中讀取資料庫內容(一)

來源:互聯網
上載者:User

標籤: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中讀取資料庫內容(一)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.