作者:k78283381 | 2021-09-05 07:39
Linux(Ubuntu平台)JSP通过JDBC连接MySQL数据库,与Windows平台类似,步骤如下:下载jdbc:mysql-connector-java-5.1.18.tar.gz解压jdbc:tar-zxvfmysql-connector-java-5.1.18.tar.gz配置jdbc:c
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示例:
- <%@ page language="java" import="java.sql.*"%>
- <%@ page language="java" import="java.io.*" %>
-
- <html>
- <head>
- <title>Read from mySQL Databasetitle>
- head>
-
- <body>
-
- <p align="center"><b>Following records are selected from table "gametop800"b><br>
- p>
-
- <div align="center" width="85%">
- <center>
- <table border="1" borderColor="#ffe9bf" cellPadding="0" cellSpacing="0"
- width="658" height="63">
- <tbody>
-
- <td bgColor="#008080" width="47" align="center" height="19">
- <font color="#ffffff"><b>topb>font>td>
- <td bgColor="#008080" width="107" height="19">
- <font color="#ffffff"><b>idb>font>td>
- <td bgColor="#008080" width="224" height="19">
- <font color="#ffffff"><b>nameb>font>td>
- <td bgColor="#008080" width="270" height="19">
- <font color="#ffffff"><b>countryb>font>td>
- <td bgColor="#008080" width="270" height="19">
- <font color="#ffffff"><b>dtimeb>font>td>
-
- <%
- String DRIVER = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://localhost:3306/top800";
-
- Connection con = null;
- ResultSet rst = null;
- Statement stmt = null;
-
- int i = 1;
-
- try {
- Class.forName(DRIVER).newInstance();
-
- con = DriverManager.getConnection(url, "root", "");
- stmt = con.createStatement();
- rst = stmt.executeQuery("select top, id, name, country, dtime from gametop800 where top=1");
- while (rst.next()) {
- if (i == (i / 2) * 2) {
- %>
- <tr>
- <td bgColor="#ffff98" vAlign="top" width="47" align="center" height="19"><%=rst.getInt(1)%>.td>
- <td bgColor="#ffff98" vAlign="top" width="107" height="19"><%=rst.getString(2)%>td>
- <td bgColor="#ffff98" vAlign="top" width="224" height="19"><a href="<%=rst.getString(3)%>"><%=rst.getString(3)%>a>td>
- <td bgColor="#ffff98" vAlign="top" width="270" height="19"><%=rst.getString(4)%>td>
- <td bgColor="#ffff98" vAlign="top" width="270" height="19"><%=rst.getString(5)%>td>
- tr>
- <% } else {
- %>
- <tr>
- <td bgColor="#ffcc68" vAlign="top" width="47" align="center" height="19"><%=rst.getInt(1)%>.td>
- <td bgColor="#ffcc68" vAlign="top" width="107" height="19"><%=rst.getString(2)%>td>
- <td bgColor="#ffcc68" vAlign="top" width="224" height="19"><a href="<%=rst.getString(3)%>"><%=rst.getString(3)%>a> td>
- <td bgColor="#ffcc68" vAlign="top" width="270" height="19"><%=rst.getString(4)%>td>
- <td bgColor="#ffff98" vAlign="top" width="270" height="19"><%=rst.getString(5)%>td>
- tr>
- <% }
- i++;
- }
- rst.close();
- stmt.close();
- con.close();
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- %>
-
- tbody>
- table>
- center>
- div>
-
- body>
- html>
执行结果: