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 main program for displaying messages is listed below
<% @ 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?

Related Article

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.