How to use JSP+MySQL to create a guestbook (2)
Author:Eve Cole
Update Time:2009-07-02 17:13:19
Once you have the database, you need to read the message for the database operation!
The main programs for displaying messages are listed below:
<%@page import="java.sql.*"
import="java.util.*"
import="java.io.*"
contentType="text/html; charset=gb2312"
%>
<html>
<head>
<title>Pinghui Free Space Guestbook</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="华文新伟">Pinghui Free Space Guestbook</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">
<%//The following handles the user’s paging request
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; //Database connection object
java.sql.Statement sqlStmt; //Statement object
java.sql.ResultSet sqlRst; //Result set object
//Register JDBC driver object
Class.forName ("org.gjt.mm.mysql.Driver").newInstance ();
//Connect to database
sqlConn= java.sql.DriverManager.getConnection ("jdbc:mysql://localhost/p","test","");
//Create statement object
sqlStmt=sqlConn.createStatement
(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
//Execute Sql statement
String sqlQuery="select count(*) from comment";
sqlRst=sqlStmt.executeQuery (sqlQuery);
sqlRst.next();
int count=sqlRst.getInt(1); //Get the total number of message records
if (Page>=0)RecoderPage=Page;//process page
else RecorderPage=0-Page*10;
if (RecoderPage>count/15){ RecoderPage=count/15; Page=RecoderPage; }//The page is out of bounds
RecoderRow=RecoderPage*15; //Get the message record number to be displayed
sqlQuery="select * from comment order by userid desc limit "+RecoderRow+",15;"; //Read 15 records at a time
sqlRst=sqlStmt.executeQuery (sqlQuery);
%> There are <%=count%> messages in total
<% while (sqlRst.next()) //Display messages
{ //Get the next record %> <%=sqlRst.getString("userid")%>
<table width="95%" border="1" cellspacing="1" cellpadding="1"
bordercolorlight="#6666FF" bordercolordark="#6666FF" bgcolor="#CCCCFF" align="center">
<tr>
<td width="25%">Nickname: <%=sqlRst.getString("username")%></td>
<td width="25%">Gender: <%=sqlRst.getString("sex")%></td>
<td colspan="2" width="50%">Address: <%=sqlRst.getString("address") %></td>
</tr>
<tr>
<td width="25%">Telephone: <%=sqlRst.getString("telnumber")%></td>
<td width="25%">Postcode:<%=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="Write to the person who left the message"><%=sqlRst.getString("email")%></a></ td>
<td colspan="2" width="50%">URL:
<a href target="_blank"></a><a href="mailto:<%=sqlRst.getString("url")%>" title="Write to the person who left the message">
<%=sqlRst.getString("urltitle")%></a></td>
</tr>
<tr>
<td colspan="4"><font style="line-height: 150%;color: green">
Message:<%=sqlRst.getString("comment")%><br>
-<%=sqlRst.getString("time") %>
(from <%=sqlRst.getString("ip") %>)</font></td>
</tr>
</table>
<hr align="center" noshade size="2" width="95%">
<% } %> <%
//Page the messages below
int i=count/15; //Total page, each page displays 15 records
int j=i/10; //Total large page, no 10 pages are divided into one large page
//Page displayed page
int StartPage;
//int HrefPage;
if (Page<0) Page=0-Page*10; //Divide 10 pages once
StartPage=Page/10; //Divide 10 pages at a time
out.print ("[Total "+(i+1)+"pages]");
//out.print ("total"+j+"screen");
//out.print ("Display page "+Page+"");
//out.print ("StartPage="+StartPage);
if (StartPage>0)
out.print ("|<a href="connectmysql.jsp?page=-"+Integer.toString(StartPage-1)+"">First 10 pages</a>");
for (int k=0;k<10;k++)
{ int p=StartPage*10+k;
if (p>i) break;
if (p==Page)
out.print ("|th"+Integer.toString(p+1)+"page");
else
out.print ("|<a href="connectmysql.jsp?page="+p+"">Page "+Integer.toString(p+1)+"</a>");
}
if (StartPage<j)
out.print ("|<a href="connectmysql.jsp?page=-"+Integer.toString(StartPage+1)+"">Next 10 pages</a>");
out.print ("|");
%> </td>
</tr>
</table>
</body>
<%
//Close the result set object
sqlRst.close();
//Close statement object
sqlStmt.close();
//Close database connection
sqlConn.close();
%> Deficiencies in the program end program:
Errors are not captured, but this is just to explain jsp reading the Mysql database. If you are using it as a guestbook, you must deal with the errors! Do we still want to leave a message?