Linux JSP连接MySQL数据库

Linux(Ubuntu平台)JSP通过JDBC连接MySQL数据库,与Windows平台类似,步骤如下:

下载 jdbc: mysql-connector-java-5.1.18.tar.gz

解压 jdbc: tar -zxvf mysql-connector-java-5.1.18.tar.gz

配置 jdbc:cp mysql-connector-java-5.1.18-bin.jar /usr/local/jdk1.6.0_22/jre/lib/ext/

配置JSP:

JSP示例:

  1. <%@ page language="java" import="java.sql.*"%>  
  2. <%@ page language="java" import="java.io.*" %>  
  3.   
  4. <html>  
  5. <head>  
  6. <title>Read from mySQL Database</title>  
  7. </head>  
  8.   
  9. <body>  
  10.   
  11. <p align="center"><b>Following records are selected from table "gametop800"</b><br>  
  12.  </p>  
  13.   
  14. <div align="center" width="85%">  
  15. <center>  
  16. <table border="1" borderColor="#ffe9bf" cellPadding="0" cellSpacing="0"  
  17.     width="658" height="63">  
  18.     <tbody>  
  19.   
  20.         <td bgColor="#008080" width="47" align="center" height="19">  
  21.             <font color="#ffffff"><b>top</b></font></td>  
  22.         <td bgColor="#008080" width="107" height="19">  
  23.             <font color="#ffffff"><b>id</b></font></td>  
  24.         <td bgColor="#008080" width="224" height="19">  
  25.             <font color="#ffffff"><b>name</b></font></td>  
  26.         <td bgColor="#008080" width="270" height="19">  
  27.             <font color="#ffffff"><b>country</b></font></td>  
  28.         <td bgColor="#008080" width="270" height="19">  
  29.             <font color="#ffffff"><b>dtime</b></font></td>  
  30.   
  31.         <%  
  32.             String DRIVER = "com.mysql.jdbc.Driver";  
  33.             String url = "jdbc:mysql://localhost:3306/top800";  
  34.   
  35.             Connection con = null;  
  36.             ResultSet rst = null;  
  37.             Statement stmt = null;  
  38.               
  39.             int i = 1;  
  40.   
  41.             try {  
  42.                 Class.forName(DRIVER).newInstance();  
  43.                   
  44.                 con = DriverManager.getConnection(url, "root", "");  
  45.                 stmt = con.createStatement();  
  46.                 rst = stmt.executeQuery("select top, id, name, country, dtime from gametop800 where top=1");  
  47.                 while (rst.next()) {  
  48.                     if (i == (i / 2) * 2) {  
  49.         %>  
  50.         <tr>  
  51.             <td bgColor="#ffff98" vAlign="top" width="47" align="center" height="19"><%=rst.getInt(1)%>.</td>  
  52.             <td bgColor="#ffff98" vAlign="top" width="107" height="19"><%=rst.getString(2)%></td>  
  53.             <td bgColor="#ffff98" vAlign="top" width="224" height="19"><a href="<%=rst.getString(3)%>"><%=rst.getString(3)%></a></td>  
  54.             <td bgColor="#ffff98" vAlign="top" width="270" height="19"><%=rst.getString(4)%></td>  
  55.             <td bgColor="#ffff98" vAlign="top" width="270" height="19"><%=rst.getString(5)%></td>  
  56.         </tr>  
  57.         <% } else {  
  58.         %>  
  59.         <tr>  
  60.             <td bgColor="#ffcc68" vAlign="top" width="47" align="center" height="19"><%=rst.getInt(1)%>.</td>  
  61.             <td bgColor="#ffcc68" vAlign="top" width="107" height="19"><%=rst.getString(2)%></td>  
  62.             <td bgColor="#ffcc68" vAlign="top" width="224" height="19"><a href="<%=rst.getString(3)%>"><%=rst.getString(3)%></a> </td>  
  63.             <td bgColor="#ffcc68" vAlign="top" width="270" height="19"><%=rst.getString(4)%></td>  
  64.             <td bgColor="#ffff98" vAlign="top" width="270" height="19"><%=rst.getString(5)%></td>  
  65.         </tr>  
  66.         <% }  
  67.             i++;  
  68.                 }  
  69.                 rst.close();  
  70.                 stmt.close();  
  71.                 con.close();  
  72.             } catch (Exception e) {  
  73.                 System.out.println(e.getMessage());  
  74.             }  
  75.         %>  
  76.   
  77.     </tbody>  
  78. </table>  
  79. </center>  
  80. </div>  
  81.   
  82. </body>  
  83. </html>  

执行结果:

Linux JSP连接MySQL数据库

相关推荐