歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux JSP連接MySQL數據庫

Linux JSP連接MySQL數據庫

日期:2017/2/28 16:05:02   编辑:Linux教程

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

執行結果:

Copyright © Linux教程網 All Rights Reserved