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>
执行结果:安装Skyeye学习ARM汇编Linux Java连接MySQL数据库相关资讯 Linux教程
- Linux教程:如何在命令行中查看目 (07/28/2014 12:22:23)
- Linux 修改root密码 (11/03/2012 07:53:38)
- su - root 与su root的区别 (06/06/2012 00:39:40)
| - Linux进程间通信:消息队列 (01/28/2013 09:43:00)
- U盘安装Linux开机无法启动解决方法 (10/07/2012 08:55:52)
- Windows 7/Linux 同步时间 (05/15/2012 06:17:55)
|
本文评论 查看全部评论 (0)