DBListDao.java file:
Here, a DAO class is defined to implement functions such as data plus, delete, update, add, and generate zone strings. The getPagestr (int ipage) function is used to generate a pages string, and the int ipage parameter is used to transfer the current page number. The FindSQL (STRING SQL, INT iPAGE) function is used to find database data, and the String SQL parameter is used to deliver the SQL query statement string, and the int ipage parameter is used to transfer the current page number.
. . . . . . . . . . (Omit)
Public class dblistdao {
PRIVATE CONNECTION CON;
PRIVATE INT ROWCOUNT;
PRIVATE INT pageCOUNT;
PRIVATE INT Length;
PRIVATE STRING PAGESTR;
Public int getLength () {return (this.Length);
Public void setlength (int length) {this.Length = Length;}
Public String getpageStr (int ipage)
{
String strpage = ""
IF (getLength ()> 0)
{
STRPAGE = "Total";
StrPage = String.Valueof (Rowcount);
STRPAGE = "Strip record, total";
StrPage = String.Valueof (pagecount);
STRPAGE = "page, currently the first";
StrPage = String.Valueof (ipage);
STRPAGE = "page,";
INT iStart, IEND;
iStart = ipage-5;
IF (iStart <0) {iStart = 0;}
IEND = ISTART 10;
Ie (IEND> PageCount) {IEND = PageCount;}
iStart = IEND-10;
IF (iStart <0) {iStart = 0;}
For (INT i = iStart; i { STRPAGE = ""; STRPAGE = String.Valueof (i 1); STRPAGE = ""; STRPAGE = "" } } this.pageStr = STRPAGE; Return strpage; } The intermediate code is independent of the paging, and it is omitted. Public Collection Findsql (String SQL, INT iPAG) { PreparedStatement PS = NULL; ResultSet RS = NULL; ArrayList List = New ArrayList (); Try { IF (con?isclosed ()) { Throw new IllegalStateException ("Error.unexpected"); PS = con.preparestatement (SQL, ResultSet.Type_Scroll_Sensitive, ResultSet.concur_Updata); RS = ps.executeQuery (); Rs.absolute (-1); Rowcount = rs.getrow (); INT offset = 1; INT PAGESIZE = GetLength (); IF (getLength () <1) { PageSize = rowcount; PageCount = 1; } Else { PageCount = rowcount / getLength () ((RowCount% getLength ())> 0? 1: 0); OFFSET = (ipage-1) * getLength () 1; IF (Offset <1) OFFSET = 1; IF (offset> rowcount) OFFSET = RowCount; } rs.absolute (offset); For (int i = 0; i DBLIST M_DBLIST = New DBLIST (); // m_dblist.set serial number (Rs.Getint (1)); m_dblist.set name (rs.getstring (2)); M_dblist.set Remarks (rs.getstring (3)); // find> rs.next (); List.add (m_dblist); } Return List; } catch (sqlexception e) { //e.printstacktrace (); // throw new runtimeException ("Error.unexpected"); Return List; } finally { Try { IF (PS! = NULL) ps.close (); IF (rs! = null) Rs.close (); } catch (sqlexception e) { E.PrintStackTrace (); Throw new runtimeException ("error.unexpected"); } } } } DBListAction.java file: The code between . . . . . . . . . . (Omit) Public Final Class DBListAction Extends Action { // Variable definitions: Private log log = logfactory.getlog ("org.apache.struts.Webapp.example"); PRIVATE CONNECTIONPOOL POOL; Public dblistression () { Pool = connectionPool.getInstance (); } // Function definition: Public ActionForward Execute (ActionMapping Mapping, Actionform Form, HTTPSERVLETREQUEST REQUEST, Httpservletresponse response Throws Exception { // Extract Attributes and Parameters We will Needlocale Locale = GetLocale (Request); MessageResources Messages = GetResources (Request); HttpSession session = request.getations (); DBListForm m_dblistform = (dblistform) form; Connection con = NULL; Try { (Omit) // m_dblistdao.setlength (10); // Set the number of records per page INT ipage; // current page Try { String Page = Request.getParameter ("Page"); // Pedestrian Parameters, read the current page of the request ipage = java.lang.integer.parseint (page, 10); } Catch (Exception E) { ipage = m_dblistform.getpage (); } IPAGE = 1; Collection col = m_dblistdao.findsql (SQL, IPAGE); // Query data via DAO object Request.setattribute ("dblist", col); // Save data String PageStr = m_dblistdao.getpagestr (ipage); // Paging string, paging content String s_find, str; IF (! "Search" .Equals (search) // lookup and all display two different operations, which are different, which is handled here. If all it is displayed, remove "Search = Search &" { s_find = "Search = SEARCH &"; While (PageStr.Indexof (s_find)! = - 1) { Str = pageStr.Substring (0, PageStr.Indexof (s_find)); Str = pageStr.substring (PageStr.Indexof (s_find) s_find.length (), pageStr.Length ()); PageStr = STR; } } m_dblistform.setpageStr (PageStr); m_dblistform.setAction; Request.setattribute ("dblistform", m_dblistform); // page> Return mapping.findforward ("Success"); } Catch (SQLException E) { (Omit) } } }