Linux JSP串連MySQL資料庫

來源:互聯網
上載者:User

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>

執行結果:


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.