如何在jsp中顯示數據庫的內容


用Eclipse tomcat新建一個JSP頁面(一)介紹了如何創建一個web程序和第一個jsp頁面,以及Eclipse需要的一些必要配置。今天,我們重點說一下如何從數據庫中查詢數據,並且在JSP頁面顯示。
首先需要注意這樣一個問題:

建的如果是Java項目,只需要引入MySQL-connector-java-5.1.10-bin.jar就可以運行java項目。建的如果是web工程,當Class.forName("com.mysql.jdbc.Driver");時,Eclipse是不會去查找字符串,不會去查找驅動。所以需要把mysql-connector-java-5.1.10-bin.jar拷貝到tomcat下lib目錄下,然后,右鍵【工程】,點擊【properties】,然后點擊【Java Build Path】,點擊【Add External Jars...】,從tomcat下lib目錄中選擇對應的mysql-connector-java-5.1.10-bin.jar,如下圖所示,然后點擊【OK】即可。

否則,控制台會報錯: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

 

顯示數據庫數據的jsp代碼如下:

 

[plain]  view plain  copy
 
  1. <span style="font-size:12px;"><span style="font-size:14px;"><%@ page language="java" import="java.sql.*,java.io.*,java.util.*"%>  
  2. <%@ page contentType="text/html;charset=utf-8"%>  
  3. <html>  
  4. <head>  
  5. <style type="text/css">  
  6. table {  
  7.     border: 2px #CCCCCC solid;  
  8.     width: 360px;  
  9. }  
  10.   
  11. td,th {  
  12.     height: 30px;  
  13.     border: #CCCCCC 1px solid;  
  14. }  
  15. </style>  
  16. </head>  
  17. <body>  
  18.     <%  
  19.         //驅動程序名   
  20.         String driverName = "com.mysql.jdbc.Driver";  
  21.         //數據庫用戶名   
  22.         String userName = "root";  
  23.         //密碼   
  24.         String userPasswd = "szy";  
  25.         //數據庫名   
  26.         String dbName = "studentmanage";  
  27.         //表名   
  28.         String tableName = "student";  
  29.         //聯結字符串   
  30.         String url = "jdbc:mysql://localhost:3306/" + dbName + "?user="  
  31.                 + userName + "&password=" + userPasswd;  
  32.         Class.forName("com.mysql.jdbc.Driver").newInstance();  
  33.         Connection connection = DriverManager.getConnection(url);  
  34.         Statement statement = connection.createStatement();  
  35.         String sql = "SELECT * FROM " + tableName;  
  36.         ResultSet rs = statement.executeQuery(sql);  
  37.     %>  
  38.     <br>  
  39.     <br>  
  40.     <table align="center">  
  41.         <tr>  
  42.             <th>  
  43.                 <%  
  44.                     out.print("學號");  
  45.                 %>  
  46.             </th>  
  47.             <th>  
  48.                 <%  
  49.                     out.print("姓名");  
  50.                 %>  
  51.             </th>  
  52.             <th>  
  53.                 <%  
  54.                     out.print("專業");  
  55.                 %>  
  56.             </th>  
  57.             <th>  
  58.                 <%  
  59.                     out.print("班級");  
  60.                 %>  
  61.             </th>  
  62.         </tr>  
  63.   
  64.         <%  
  65.             while (rs.next()) {  
  66.         %>  
  67.         <tr>  
  68.             <td>  
  69.                 <%  
  70.                     out.print(rs.getString(1));  
  71.                 %>  
  72.             </td>  
  73.             <td>  
  74.                 <%  
  75.                     out.print(rs.getString(2));  
  76.                 %>  
  77.             </td>  
  78.             <td>  
  79.                 <%  
  80.                     out.print(rs.getString(3));  
  81.                 %>  
  82.             </td>  
  83.             <td>  
  84.                 <%  
  85.                     out.print(rs.getString(4));  
  86.                 %>  
  87.             </td>  
  88.         </tr>  
  89.         <%  
  90.             }  
  91.         %>  
  92.     </table>  
  93.     <div align="center">  
  94.         <br> <br> <br>  
  95.         <%  
  96.             out.print("數據查詢成功,恭喜你");  
  97.         %>  
  98.     </div>  
  99.     <%  
  100.         rs.close();  
  101.         statement.close();  
  102.         connection.close();  
  103.     %>  
  104. </body>  
  105. </html></span><span style="font-size:24px;color: rgb(255, 0, 0);">  
  106. </span></span>  


顯示結果如下所示:


 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM