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樣本:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %><br /><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"></p><p><%@ page language="java" import="java.sql.*"%><br /><%@ page language="java" import="java.io.*" %></p><p><html><br /><head><br /><title>Read from mySQL Database</title><br /></head></p><p><body></p><p><p align="center"><b>Following records are selected from table "gametop800"</b><br></p></p><p><center><br /><table border="1" borderColor="#ffe9bf" cellPadding="0" cellSpacing="0" width="60%" ><br /><tbody><br /><tr><br /><td bgColor="#008080" width="10%" height="28" align="center" valign="middle"><br /><font color="#ffffff"><b>top</b></font></td><br /><td bgColor="#008080" width="30%" height="28" align="center" valign="middle"><br /><font color="#ffffff"><b>id</b></font></td><br /><td bgColor="#008080" width="20%" height="28" align="center" valign="middle"><br /><font color="#ffffff"><b>name</b></font></td><br /><td bgColor="#008080" width="20%" height="28" align="center" valign="middle"><br /><font color="#ffffff"><b>country</b></font></td><br /><td bgColor="#008080" width="20%" height="28" align="center" valign="middle"><br /><font color="#ffffff"><b>dtime</b></font></td><br /></tr><br /><%<br />String DRIVER = "com.mysql.jdbc.Driver";<br />String url = "jdbc:mysql://localhost:3306/top800";</p><p>Connection con = null;<br />ResultSet rst = null;<br />Statement stmt = null;</p><p>int i = 1;</p><p>try {<br />Class.forName(DRIVER).newInstance();</p><p>con = DriverManager.getConnection(url, "root", "");<br />stmt = con.createStatement();<br />rst = stmt.executeQuery("select top, id, name, country, dtime from gametop800 where top=1");<br />while (rst.next()) {<br />if (0 == i%2) {<br />%><br /><tr><br /><td bgColor="#ffff98" width="10%" height="18" vAlign="middle" align="center"><%=rst.getInt(1)%>.</td><br /><td bgColor="#ffff98" width="30%" height="18" valign="middle"><%=rst.getString(2)%></td><br /><td bgColor="#ffff98" width="20%" height="18" valign="middle"><a href="<%=rst.getString(3)%>"><%=rst.getString(3)%></a></td><br /><td bgColor="#ffff98" width="20%" height="18" valign="middle"><%=rst.getString(4)%></td><br /><td bgColor="#ffff98" width="20%" height="18" valign="middle"><%=rst.getString(5)%></td><br /></tr><br /><% } else {<br />%><br /><tr><br /><td bgColor="#ffcc68" width="10%" height="18" valign="middle" align="center"><%=rst.getInt(1)%>.</td><br /><td bgColor="#ffcc68" width="30%" height="18" valign="middle"><%=rst.getString(2)%></td><br /><td bgColor="#ffcc68" width="20%" height="18" valign="middle"><a href="<%=rst.getString(3)%>"><%=rst.getString(3)%></a> </td><br /><td bgColor="#ffcc68" width="20%" height="18" valign="middle"><%=rst.getString(4)%></td><br /><td bgColor="#ffcc68" width="20%" height="18" valign="middle"><%=rst.getString(5)%></td><br /></tr><br /><% }<br />i++;<br />}<br />rst.close();<br />stmt.close();<br />con.close();<br />} catch (Exception e) {<br />System.out.println(e.getMessage());<br />}<br />%></p><p></tbody><br /></table><br /></center></p><p></body><br /></html>
執行結果: