How to use Jsp+mysql to create a message (ii) _JSP programming

Source: Internet
Author: User
With the database under the database operation read the message!
The main procedures for displaying messages are listed below
<% @page import= "java.sql.*"
import = "Java.util.*"
import = "Java.io.*"
Contenttype= "text/html; charset=gb2312 "
%>
<title> Ping hui free Space message book </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>
<body>
<p align= "center" ><u><font size= "5" face= "Chinese New Wei" > Ping hui free space message book </font></u></p>
<table width= "75%" border= "1" bgcolor= "#FFCCFF" bordercolorlight= "#0000FF"
bordercolordark= "#6666FF" cellpadding= "0" cellspacing= "0" align= "center" >
<tr>
&LT;TD colspan= "5" height= ">"
<%//The following user paging requests
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 objects
Java.sql.Statement sqlstmt; Statement Object
Java.sql.ResultSet Sqlrst; Result set Object
Registering JDBC Driver objects
Class.forName ("Org.gjt.mm.mysql.Driver"). newinstance ();
Connecting to a database
sqlconn= java.sql.DriverManager.getConnection ("jdbc:mysql://localhost/p", "Test", "");
To create a 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;//processing page
else recoderpage=0-page*10;
if (RECODERPAGE&GT;COUNT/15) {RECODERPAGE=COUNT/15; Page=recoderpage; }//page out of bounds

recoderrow=recoderpage*15; Get the message record number to display
sqlquery= "SELECT * from comment order BY userid desc LIMIT" +recoderrow+ ", 15;"; Read 15 records at a time
Sqlrst=sqlstmt.executequery (sqlquery);
%> A total of <%=count%> message
<% while (Sqlrst.next ())//Show message
{//Get the next record%> <%=sqlrst.getstring ("userid")%> Strip
<table width= "1" border= "cellspacing=" 1 "cellpadding=" 1 "
bordercolorlight= "#6666FF" bordercolordark= "#6666FF" bgcolor= "#CCCCFF" align= "Center" >
<tr>
&LT;TD width= "25%" > Nickname: <%=sqlrst.getstring ("username")%></td>
&LT;TD width= "25%" > Sex: <%=sqlrst.getstring ("Sex")%></td>
&LT;TD colspan= "2" width= "50%" > Add: <%=sqlrst.getstring ("Address")%></td>
</tr>
<tr>
&LT;TD width= "25%" > Tel: <%=sqlrst.getstring ("Telnumber")%></td>
&LT;TD width= "25%" > Zip Code: <%=sqlrst.getstring ("post")%></td>
&LT;TD width= "25%" >oicq:<%=sqlrst.getstring ("OICQ")%></td>
&LT;TD width= "25%" >icq:<%=sqlrst.getstring ("ICQ")%></td>
</tr>
<tr>
&LT;TD colspan= "2" width= "50%" >email:
<a href= "mailto:<%=sqlrst.getstring (" email ")%>" title= "write to the Message" ><%=sqlrst.getstring "(" email ")%> </a></td>
&LT;TD colspan= "2" width= "50%" > website:
<a href target= "_blank" ></a><a href= "mailto:<%=sqlrst.getstring" ("url")%> "title=" write to Message >
<%=sqlrst.getstring ("Urltitle")%></a></td>
</tr>
<tr>
&LT;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>
&LT;HR align= "center" NoShade size= "2" width= ">"
<%}%> <%
The following message to the paging
int I=COUNT/15; Total page, 15 records per page
int J=I/10; Total Big page, No 10 pages divided into one big page
Pages displayed by page
int startpage;
int hrefpage;
if (page<0) page=0-page*10; 10-page Time
STARTPAGE=PAGE/10; 10-page Time
Out.print ("[Total" + (i+1) + "page]");
Out.print ("Total" +j+ "screen");
Out.print ("Show the First" +page+ "page");
Out.print ("startpage=" +startpage);
if (startpage>0)
Out.print ("|<a href=" connectmysql.jsp?page=-"+integer.tostring (StartPage-1) +" "> Top 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) + "page");
Else
Out.print ("|<a href=" connectmysql.jsp?page= "+p+" ">" +integer.tostring (p+1) + "page </a>");
}
if (STARTPAGE&LT;J)
Out.print ("|<a href=" connectmysql.jsp?page=-"+integer.tostring (startpage+1) +" "> after 10 pages </a>");
Out.print ("|");
%> </td>
</tr>
</table>
</body>
<%
Close result Set Object
Sqlrst.close ();
Close Statement Object
Sqlstmt.close ();
To close a database connection
Sqlconn.close ();
%> Program End
Deficiencies in the program:
The error is not captured, but only to read the MySQL database to the JSP to explain. If you are using and make a message, be sure to deal with the error! Are we going to leave a message?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.