本文介绍如何读取留言。
显示留言信息的主程序。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://localhost/p","test","");
//创建语句对象
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); //取得总的留言记录数
if (Page>=0)RecoderPage=Page;//处理页面
else RecoderPage=0-Page*10;
if (RecoderPage>count/15){ RecoderPage=count/15; Page=RecoderPage; }//页面越界
RecoderRow=RecoderPage*15; //取得要显示的留言记录号
sqlQuery="select * from comment order by userid desc limit "+RecoderRow+",15;"; //一次读取15条记录
sqlRst=sqlStmt.executeQuery (sqlQuery);
%> 总共有<%=count%>条留言
<% while (sqlRst.next()) //显示留言
{ //取得下一条记录 %> 第<%=sqlRst.getString("userid")%>条
<table width="95%" border="1" cellspacing="1" cellpadding="1"
bordercolorlight="#6666FF" bordercolordark="#6666FF" bgcolor="#CCCCFF" align="center">
<tr>
<td width="25%">昵称:<%=sqlRst.getString("username")%></td>
<td width="25%">性别:<%=sqlRst.getString("sex")%></td>
<td colspan="2" width="50%">地址:<%=sqlRst.getString("address") %></td>
</tr>
<tr>
<td width="25%">电话:<%=sqlRst.getString("telnumber")%></td>
<td width="25%">邮编:<%=sqlRst.getString("post")%></td>
<td width="25%">OICQ:<%=sqlRst.getString("oicq")%></td>
<td width="25%">ICQ:<%=sqlRst.getString("icq")%></td>
</tr>
<tr>
<td colspan="2" width="50%">Email:
<a href="mailto:<%=sqlRst.getString("email")%>" title="写信给留言者"><%=sqlRst.getString("email")%></a></td>
<td colspan="2" width="50%">网址:
<a href target="_blank"></a><a href="mailto:<%=sqlRst.getString("url")%>" title="写信给留言者">
<%=sqlRst.getString("urltitle")%></a></td>
</tr>
<tr>
<td colspan="4"><font style="line-height: 150%;color: green">
留言:<%=sqlRst.getString("comment")%><br>
-<%=sqlRst.getString("time") %>
(来自<%=sqlRst.getString("ip") %>)</font></td>
</tr>
</table>
<hr align="center" noshade size="2" width="95%">
<% } %> <%
//下面对留言进行分页
int i=count/15; //总页,每页显示15条记录
int j=i/10; //总的大页 ,没10个页面分一大页
//Page 显示的页面
int StartPage;
//int HrefPage;
if (Page<0) Page=0-Page*10; //10页分一次
StartPage=Page/10; //10页分一次
out.print ("[共有"+(i+1)+"页]");
//out.print ("共有"+j+"屏");
//out.print ("显示第"+Page+"页");
//out.print ("StartPage="+StartPage);
if (StartPage>0)
out.print ("|<a href="connectmysql.jsp?page=-"+Integer.toString(StartPage-1)+"">前10页</a>");
for (int k=0;k<10;k++)
{ int p=StartPage*10+k;
if (p>i) break;
if (p==Page)
out.print ("|第"+Integer.toString(p+1)+"页");
else
out.print ("|<a href="connectmysql.jsp?page="+p+"">第"+Integer.toString(p+1)+"页</a>");
}
if (StartPage<j)
out.print ("|<a href="connectmysql.jsp?page=-"+Integer.toString(StartPage+1)+"">后10页</a>");
out.print ("|");
%> </td>
</tr>
</table>
</body>
<%
//关闭结果集对象
sqlRst.close();
//关闭语句对象
sqlStmt.close ();
//关闭数据库连接
sqlConn.close();
%>