How to use JSP + MYSQL to create a message book (2)

Source: Internet
Author: User
Tags microsoft frontpage

If you have a database, you need to read the message for database operations!
The following lists the main messages Program
<% @ Page import = "Java. SQL .*"
Import = "Java. util .*"
Import = "Java. Io .*"
Contenttype = "text/html; charset = gb2312"
%>
<HTML>
<Head>
<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>
</Head>
<Body>
<P align = "center"> <u> <font size = "5" face = "文 "> 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>
<TD colspan = "4" Height = "202">
<% // The following process 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 the JDBC driver object
Class. forname ("org. gjt. Mm. MySQL. Driver"). newinstance ();
// Connect to the database
Sqlconn = java. SQL. drivermanager. getconnection ("JDBC: mysql: // localhost/P", "test ","");
// Create a statement object
Sqlstmt = sqlconn. createstatement
(Java. SQL. resultset. type_scroll_insensitive, java. SQL. resultset. concur_read_only );
// Execute the SQL statement
String sqlquery = "select count (*) from comment ";
Sqlrst=sqlstmt.exe cutequery (sqlquery );
Sqlrst. Next ();
Int COUNT = sqlrst. getint (1); // obtain the total number of message records
If (page> = 0) recoderpage = page; // process the page
Else recoderpage = 0-page * 10;
If (recoderpage> count/15) {recoderpage = count/15; page = recoderpage;} // The page is out of bounds.

Recoderrow = recoderpage * 15; // obtain 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.exe cutequery (sqlquery );
%> A total of <% = count %> messages
<% While (sqlrst. Next () // display the message
{// Obtain the <% = sqlrst. getstring ("userid") %> next record
<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%"> Tel: <% = sqlrst. getstring ("telnumber") %> </TD>
<TD width = "25%"> zip code: <% = sqlrst. getstring ("Post") %> </TD>
<TD width = "25%"> OICQ: <% = sqlrst. getstring ("OICQ") %> </TD>
<TD width = "25%"> ICQ: <% = sqlrst. getstring ("ICQ") %> </TD>
</Tr>
<Tr>
& Lt; TD colspan = "2" width = "50%" & gt; Email:
<A href = "mailto: <% = sqlrst. getstring ("email") %> "Title =" "> <% = sqlrst. getstring ("email") %> </a> </TD>
<TD colspan = "2" width = "50%"> URL:
<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">
Message: <% = sqlrst. getstring ("comment") %> <br>
-<% = Sqlrst. getstring ("time") %>
(From <% = sqlrst. getstring ("ip") %>) </font> </TD>
</Tr>
</Table>
<HR Align = "center" noshade size = "2" width = "95%">
<% }%> <%
// Pagination of the following messages
Int I = count/15; // total page, 15 records per page
Int J = I/10; // The total size of the page. If there are no 10 pages, a large page is displayed.
// Page
Int startpage;
// Int hrefpage;
If (page <0) page = 0-page * 10; // every 10 pages
Startpage = page/10; // every 10 pages
Out. Print ("[total" + (I + 1) + "Page]");
// Out. Print ("Total" + J + "screen ");
// Out. Print ("display page" + Page + "page ");
// Out. Print ("startpage =" + startpage );
If (startpage> 0)
Out. Print ("| <a href =" connectmysql. jsp? Page =-"+ integer. tostring (StartPage-1) +" "> Top 10 pages </a> ");
For (int K = 0; k <10; k ++)
{Int P = startpage * 10 + K;
If (P> I) break;
If (P = page)
Out. Print ("| No." + 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 the statement object
Sqlstmt. Close ();
// Close the database connection
Sqlconn. Close ();
%> Program ended
Program limitations:
The error is not captured, but here we only want to explain how to read the MySQL database from JSP. If it is used as a message book, you must handle the error! Do we have 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.