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示例:
- <%@ page language="java" import="java.sql.*"%>
- <%@ page language="java" import="java.io.*" %>
- <html>
- <head>
- <title>Read from mySQL Database</title>
- </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>top</b></font></td>
- <td bgColor="#008080" width="107" height="19">
- <font color="#ffffff"><b>id</b></font></td>
- <td bgColor="#008080" width="224" height="19">
- <font color="#ffffff"><b>name</b></font></td>
- <td bgColor="#008080" width="270" height="19">
- <font color="#ffffff"><b>country</b></font></td>
- <td bgColor="#008080" width="270" height="19">
- <font color="#ffffff"><b>dtime</b></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>
执行结果:
相关推荐
羽化大刀Chrome 2013-05-31
淡风wisdon大大 2020-06-06
黎豆子 2020-05-07
chenshuixian 2013-06-01
quanhaoH 2013-06-17
IT之家 2020-03-11
graseed 2020-10-28
zbkyumlei 2020-10-12
SXIAOYI 2020-09-16
jinhao 2020-09-07
impress 2020-08-26
liuqipao 2020-07-07
yoohsummer 2020-06-01
chenjia00 2020-05-29
baike 2020-05-19
扭来不叫牛奶 2020-05-08
hxmilyy 2020-05-11
xiongweiwei00 2020-04-29