<%@page import="java.sql.*"
import ="java.util.*"
import ="java.io.*"
contentType="text/html; charset=gb2312"
%>
<html>
<head>
<title>使用MYsql数据库</title>
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
</head>
<body>
<p align="center"><u><font size="5" face="华文新魏">使用JSP读取Mysql数据库</font></u></p>
<%!
String host="localhost"; //数据库主机
String database="pinghui"; //数据库名
String user="test"; //用户名
String pass=""; //口令
%><% java.sql.Connection sqlConn; //数据库连接对象
java.sql.Statement sqlStmt; //语句对象
java.sql.ResultSet sqlRst; //结果集对象
//登记JDBC驱动对象
Class.forName ("org.gjt.mm.mysql.Driver").newInstance ();
//连接数据库
sqlConn= java.sql.DriverManager.getConnection ("jdbc:mysql://"+host+"/"+database,user,pass);
//创建语句对象
sqlStmt=sqlConn.createStatement (java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
//执行Sql语句
String sqlQuery="select count(*) from comment";
sqlRst=sqlStmt.executeQuery (sqlQuery);
sqlRst.next();
int count=sqlRst.getInt(1); //取得总的留言记录数
sqlQuery="select * from comment order by userid desc;";
sqlRst=sqlStmt.executeQuery (sqlQuery); //执行SQL语句
%>
总共有<%=count%>条留言
<table border="1" width="100%" bordercolorlight="#CC99FF" cellpadding="2" bordercolordark="#FFFFFF" cellspacing="0">
<tr>
<td align="center"> ID</td>
<td align="center">昵称</td>
<td align="center">性别</td>
<td align="center">地址</td>
<td align="center">IP地址</td>
<td align="center">邮编</td>
<td align="center">Oicq</td>
<td align="center">ICQ</td>
<td align="center">电话</td>
<td align="center">留言</td>
<td align="center">时间</td>
</tr>
<% while (sqlRst.next()) { //取得下一条记录 %>
<tr><!--显示记录-->
<td><%=sqlRst.getString("userid")%></td>
<td><%=sqlRst.getString("username")%></td>
<td><%=sqlRst.getString("sex")%></td>
<td><%=sqlRst.getString("address") %></td>
<td><%=sqlRst.getString("ip") %></td>
<td><%=sqlRst.getString("post")%></td>
<td><%=sqlRst.getString("oicq")%></td>
<td><%=sqlRst.getString("icq")%></td>
<td><%=sqlRst.getString("telnumber")%></td>
<td><%=sqlRst.getString("comment")%></td>
<td><%=sqlRst.getString("time") %></td>
</tr>
<% } %>
</table>
</body>
<%
//关闭结果集对象
sqlRst.close();
//关闭语句对象
sqlStmt.close ();
//关闭数据库连接
sqlConn.close();
%>