如何使用JSP+MySQL创建留言本(二)
有了数据库下面就要对数据库操作读取留言了!下面列出显示留言的主要程序
<%@page import="java.sql.*"
import ="java.util.*"
import ="java.io.*"
contentType="text/html; charset=gb2312"
%>
<html>
<head>
<title>萍慧自由空间留言本</title>
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<style type="text/css"><!--
body { font-size: 9pt}
td { font-size: 9pt}-->
</style>
</head>
<body>
<p align="center"><u><font size="5" face="华文新魏">萍慧自由空间留言本</font></u></p>
<table width="75%" border="1" bgcolor="#FFCCFF" bordercolorlight="#0000FF"
bordercolordark="#6666FF" cellpadding="0" cellspacing="0" align="center">
<tr>
<td colspan="5" height="202">
<%//下面处理用户的分页请求
String string_page;
int Page,RecoderPage,RecoderRow;
try{string_page=request.getParameter("page");
}catch (NullPointerException e){string_page="";}
try{Page=Integer.parseInt(string_page);
}catch(NumberFormatException e)
{Page=0;
}
java.sql.Connection sqlConn; //数据库连接对象
java.sql.Statement sqlStmt; //语句对象
java.sql.ResultSet sqlRst; //结果集对象
//登记JDBC驱动对象
Class.forName ("org.gjt.mm.MySQL(和PHP搭配之最佳组合).Driver").newInstance ();
//连接数据库
sqlConn= java.sql.DriverManager.getConnection ("jdbc:MySQL(和PHP搭配之最佳组合)://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("易做图")%></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>
补充:Web开发 , Jsp ,