JSP technology page

  Abstract: JSP technology page 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="722" border="0"> <tr> <td> </ td> </ tr> </ table> 

  The current widespread use of the paging option is to cache query results in the HttpSession or state bean, the time from turning the pages out of a cache data.    Such an approach has two major drawbacks: First, users may see is measured data; Second, if the volume of data is very large for the first time spent traversing the results of assembly for a long time, and cache data will take up much of memory, efficiency has declined markedly. 
  Other common methods are also on each page-a database, only removed from a ResultSet data (using rs.last (); rs.getRow (), and was recorded of the total number, use rs.absolute () positioning Go to Start records).    In some ways this database (such as oracle) JDBC is the need to achieve almost all records traversing experimental proof in the record number of large, very slow speed. 
  As for the cache result sets ResultSet methods are is a completely wrong approach.    ResultSet because Connection closed or in the Statement will also be closed, if prolonged occupation will ResultSet effective database connection. 

  Pagination therefore relatively good practice should be the time to turn the pages, each from a database search page size of the data block.    Although each page-this need to query the database, but for a very small number of records, not data transmission network, the use of connection pool can be the most time-consuming little more than the establishment of a database linking process.    In the database and a variety of mature-optimization techniques used to enhance the speed of enquiries than in the application server cache done more effectively. 

  In oracle database query results, the Bank said that the use of pseudo-out ROWNUM (from the beginning).    For example, select * from employee where rownum <10 return to the previous record of 10.    But since rownum in enquiries after the sort of assignment before, so for employee birthday by the sort 100-120 record should write: 
  [Pre] select * from ( 
  Select my_table .*, rownum as my_rownum from ( 
  Select name, birthday from employee order by birthday 
  ) My_table where rownum <120 
  ) Where my_rownum> = 100 
  [/ Pre] 
  MySQL can use the LIMIT clause: 
  Select name, birthday from employee order by birthday LIMIT 99,20 
  DB2 has rownumber () function to access the current line number. 
  SQL Server not studied, can refer to the article: http://www.csdn.net/develop/article/18/18627.shtm 

  In the process of Web page will be frequently used, but the realization of the details page programming is trouble in the course of things.    Most of the paging display query operation at the same time to deal with complex multiple search criteria, sql statement needs dynamic mosaic composition, coupled with the need for paging record positioning, the total number of records, as well as the result of a query on Ergodic, Packaging, and that process will become very complex and difficult to understand.    Some of the tools needed to streamline category paging code, programmers focus on the business logic part.    Below are the two I design tools categories: 
  PagedStatement Packaging, a database connection, the record number of enquiries, paging enquiries, outcome data package and the closure of operations such as database connectivity, support and the use of dynamic setting parameters PreparedStatement. 
  PetStore RowSetPage reference to the page by page iterator pattern for Packaging Design RowSetPage query results (using OracleCachedRowSet from a cache for data on the use of database query results CachedRowSet Packaging, please refer to JSP pages enquiries revealed that commonly used models) as well as the current paginated, and the total number of records, the current record number, and other information, and can generate simple HTML page code. 
  PagedStatement the results of inquiries into RowsetPage package. 

  Below is the use of simple examples: 


  / / DAO data for some of the code: 
…
  Public RowSetPage getEmployee (String gender, int pageNo) throws Exception ( 
  String sql = "select emp_id, emp_code, user_name, real_name from employee where gender =?"; 
  / / Oracle Database tab for realization per page 5 
  PagedStatement pst = new PagedStatementOracleImpl (sql, pageNo, 5); 
  Pst.setString (1, gender); 
  Return pst.executeQuery (); 
  ) 


  / / Servlet handle enquiries request of the code: 

…
  Int pageNo; 
  Try ( 
  / / Pageno parameters can be obtained through the user's selection of a page 
  PageNo = Integer.parseInt (request.getParameter ( "pageno")); 
  ) Catch (Exception ex) ( 
  / / The default is the first page 
  PageNo = 1; 
  ) 
  String gender = request.getParameter ( "gender"); 
  Request.setAttribute ( "empPage" myBean.getEmployee (gender, pageNo)); 
…

  / / JSP showed some code 
  <% @ Page import = "page.RowSetPage"%> 
…
  <script Language="javascript"> 
  Function doQuery () ( 
  Form1.actionType.value = "doQuery"; 
  Form1.submit (); 
  ) 
  </ Script> 
…
  <form Name=form1 method=get> 
  <input Type=hidden name=actionType> 
  Sex: 
  <Input type = text name = gender size = 1 value ="<%= request.getParameter ( "gender ")%>"> 
  <input Type=button value="查询" onclick="doQuery()"> 
<%
  RowSetPage empPage = (RowSetPage) request.getAttribute ( "empPage"); 
  If (empPage == null) = RowSetPage.EMPTY_PAGE empPage; 
%>
…
  <table Cellspacing="0" width="90%"> 
  <tr> <td> ID </ td> <td> code </ td> <td> user name </ td> <td> name </ td> </ tr> 
<%
  Javax.sql.RowSet empRS = (javax.sql.RowSet) empPage.getRowSet (); 
  If (empRS! = Null) while (empRS.next ()) ( 
%>
<tr>
  <td> <% = EmpRS.getString ( "EMP_ID ")%></ td> 
  <td> <% = EmpRS.getString ( "EMP_CODE ")%></ td> 
  <td> <% = EmpRS.getString ( "USER_NAME ")%></ td> 
  <td> <% = EmpRS.getString ( "REAL_NAME ")%></ td> 
  </ Tr> 
<%
  ) / / End while 
%>
<tr>
<%
  / / Show the total number of pages and the current page (pageno), as well as paging code. 
  / / Here doQuery pages submitted for action on the javascript function name, logo pageno for the parameters of the current page 
%>
  <td Colspan=4> <% = empPage. GetHTML ( "doQuery", "pageno ")%></ td> 
  </ Tr> 
  </ Table> 
  </ Form> 

  Figure effect: 


  Pagination will show that because of the general conditions and with enquiries for action, the page should have a check on the conditions and methods for the javascript (as in the above doQuery), the RowSetPage.getHTML () generated paging code on the user choice new page directly in front of the callback to deal with enquiries javascript methods.    Attention to the outcome of the enquiries revealed that the last time on the need to maintain conditions, such as <input type = text name = gender size = 1 value ="<%= request.getParameter ( "gender ")%>">.    At the same time as the parameters of a page can be designated, and therefore support in the same pages in a number of paging zones. 
  Another paging code is generated for each page URL, page inquiry parameters and attached to the URL as QueryString behind.    This method is the deficiencies in the conditions for dealing with complex, difficult, and the need for action designated to deal with the servlet, and may not be suitable for certain custom query operation. 
  If the RowSetPage.getHTML () generation is not satisfied with the default code page can be dealt with the preparation of their own paging code RowSetPage provide many getter methods are used to get the relevant information (such as the current page, total pages, the total number of records and the current record number, etc.). 
  In practical application can be made and shown on page jsp taglib to further streamline the JSP code, shielding Java Code. 

  Attachment: Paging Tool of the source code, the Notes should be very easy to understand. 

1.Page.java
  2.RowSetPage.java (RowSetPage succession Page) 
3.PagedStatement.java
  4.PagedStatementOracleImpl.java (PagedStatementOracleImpl succession PagedStatement) 

  You can use any source code, but the word must be preserved author evan_zhao@hotmail.com 


///////////////////////////////////
  / / 
  / / Page.java 
  / / Author: evan_zhao@hotmail.com 
  / / 
///////////////////////////////////

  Package page; 

  Import java.util.List; 
  Import java.util.ArrayList; 
  Import java.util.Collection; 
  Import java.util.Collections; 


  / ** 
  * Title: paging object 

  * Description: Information contained data and paging targets 

  * Page class implements used to display basic information page, but not specified the type of data contained, 
  * According to the need to achieve a particular way of organizing the data-type, 

  * RowSetPage to RowSet package data, the data package ListPage to List 

  * Copyright: Copyright (c) 2002 

  * @ Author evan_zhao@hotmail.com 

  * @ Version 1.0 
  * / 
  Public class Page implements java.io.Serializable ( 
  Public static final Page EMPTY_PAGE = new Page (); 
  Public static final int DEFAULT_PAGE_SIZE = 20; 
  Public static final int MAX_PAGE_SIZE = 9999; 

  Private int myPageSize = DEFAULT_PAGE_SIZE; 

  Private int start; 
  Private int avaCount, totalSize; 
  Private Object data; 

  Private int currentPageno; 
  Private int totalPageCount; 

  / ** 
  * The default constructor, the only empty page structure 
  * / 
  Protected Page () ( 
  This.init (0,0,0, DEFAULT_PAGE_SIZE, new Object ()); 
  ) 

  / ** 
  * Initial data paging, call by the subclass 
  * @ Param start page data in the database in the initial position 
  * @ Param avaCount page contains data of the number of 
  * @ Param totalSize database records of the total number of 
  * @ Param pageSize page capacity 
  * @ Param data page contains data 
  * / 
  Protected void init (int start, int avaCount, int totalSize, int pageSize, Object data) ( 

  This.avaCount = avaCount; 
  This.myPageSize = pageSize; 

  This.start = start; 
  This.totalSize = totalSize; 

  This.data = data; 

  / / System.out.println ( "avaCount:" + avaCount); 
  / / System.out.println ( "totalSize:" + totalSize); 
  If (avaCount> totalSize) ( 
  / / Throw new RuntimeException ( "record of the total number of more than a few ?!"); 
  ) 

  This.currentPageno = (start -1) / pageSize +1; 
  This.totalPageCount = (totalSize pageSize + -1) / pageSize; 

  If (totalSize == 0 & & avaCount == 0) ( 
  This.currentPageno = 1; 
  This.totalPageCount = 1; 
  ) 
  / / System.out.println ( "Start Index to Page No:" + start + "-" + currentPageno); 
  ) 

  Public Object getData () ( 
  Return this.data; 
  ) 

  / ** 
  * Data from this page capacity (this page will contain the record number) 
  * @ Return the records page can include several 
  * / 
  Public int getPageSize () ( 
  Return this.myPageSize; 
  ) 

  / ** 
  * Whether there is a Next 
  * @ Return whether there is a Next 
  * / 
  Public boolean hasNextPage () ( 
  / * 
  If (avaCount == 0 & & totalSize == 0) ( 
  Return false; 
  ) 
  Return (+ start avaCount -1) <totalSize; 
  * / 
  Return (this.getCurrentPageNo () <this.getTotalPageCount ()); 

}

  / ** 
  * Whether there is a previous 
  * @ Return whether there is a previous 
  * / 
  Public boolean hasPreviousPage () ( 
  / * 
  Return start> 1; 
  * / 
  Return (this.getCurrentPageNo ()> 1); 
  ) 

  / ** 
  * Access to the first page of the data in the database location 
  * @ Return 
  * / 
  Public int getStart () ( 
  Return start; 
  ) 

  / ** 
  * Get the current page final data in the database in a position 
  * @ Return 
  * / 
  Public int getEnd () ( 
  Int end = this.getStart () + this.getSize () -1; 
  If (end <0) ( 
  End = 0; 
  ) 
  Return end; 
  ) 

  / ** 
  * Previous first access data in the database of the location of 
  * @ Return records corresponding rownum 
  * / 
  Public int getStartOfPreviousPage () ( 
  Return Math.max (start-myPageSize, 1); 
  ) 


  / ** 
  * Get Next first data in the database of the location of 
  * @ Return records corresponding rownum 
  * / 
  Public int getStartOfNextPage () ( 
  + AvaCount return start; 
  ) 

  / ** 
  * Access to the first page of the data in the database location, number each page of the use of default values 
  * @ Param pageNo page, 
  * @ Return records corresponding rownum 
  * / 
  Public static int getStartOfAnyPage (int pageNo) ( 
  Return getStartOfAnyPage (pageNo, DEFAULT_PAGE_SIZE); 
  ) 

  / ** 
  * Access to the first page of the data in the database location 
  * @ Param pageNo page, 
  * @ Param pageSize few records per page 
  * @ Return records corresponding rownum 
  * / 
  Public static int getStartOfAnyPage (int pageNo, int pageSize) ( 
  Int startIndex = (pageNo-1) * pageSize + 1; 
  If (startIndex <1) startIndex = 1; 
  / / System.out.println ( "Page No to Start Index:" pageNo + + "-" + startIndex); 
  Return startIndex; 
  ) 

  / ** 
  * Page from the records contain few 
  * @ Return page contains a record number of 
  * / 
  Public int getSize () ( 
  Return avaCount; 
  ) 

  / ** 
  * From the database contains records of the total number of 
  * @ Return the database contains records of the total number of 
  * / 
  Public int getTotalSize () ( 
  Return this.totalSize; 
  ) 

  / ** 
  * From the current page 
  * @ Return the current page 
  * / 
  Public int getCurrentPageNo () ( 
  Return this.currentPageno; 
  ) 

  / ** 
  * From the page 
  * @ Return the page 
  * / 
  Public int getTotalPageCount () ( 
  Return this.totalPageCount; 
  ) 


  / ** 
*
  * @ Param queryJSFunctionName achieve paging JS script name, page will automatically change the method of correction 
  * @ Param pageNoParamName page parameter name 
  * @ Return 
  * / 
  Public String getHTML (String queryJSFunctionName, String pageNoParamName) ( 
  If (getTotalPageCount () <1) ( 
  Return "<input type='hidden' name='"+pageNoParamName+"' value='1'>"; 
  ) 
  If (queryJSFunctionName == null | | queryJSFunctionName.trim (). Length () <1) ( 
  QueryJSFunctionName = "gotoPage"; 
  ) 
  If (pageNoParamName == null | | pageNoParamName.trim (). Length () <1) ( 
  PageNoParamName = "pageno"; 
  ) 

  String gotoPage = "_" + queryJSFunctionName; 

  StringBuffer html = new StringBuffer ( "\ n"); 
  Html.append ( "<script language=\"Javascript1.2\"> \ n") 
  . Append ( "function"). Append (gotoPage). Append ( "(pageNo) (\ n") 
  . Append ( "var curPage = 1 \ n") 
  . Append ( "try (curPage = document.all [\" ") 
  . Append (pageNoParamName). Append ( "\"]. Value; \ n ") 
  . Append ( "document.all [\" "). Append (pageNoParamName) 
  . Append ( "\"]. Value = pageNo; \ n ") 
  . Append ( ""). Append (queryJSFunctionName). Append ( "(pageNo) \ n") 
  . Append ( "return true; \ n") 
  . Append ( ") catch (e) (\ n") 
  / / Append ( "try (\ n") 
  / / Append ( "document.forms [0]. Submit () \ n") 
  / / Append ( ") catch (e) (\ n") 
  . Append ( "alert ( 'not yet defined query methods: function") 
  . Append (queryJSFunctionName). Append ("()'); \ n ") 
  . Append ( "document.all [\" "). Append (pageNoParamName) 
  . Append ( "\"]. Value = curPage; \ n ") 
  . Append ( "return false; \ n") 
  / / Append ( ") \ n") 
  . Append ( ") \ n") 
  . Append ( ")") 
  . Append ( "</ script> \ n") 
  . Append ( ""); 
  Html.append ( "<table border=0 cellspacing=0 cellpadding=0 align=center width=80%> \ n") 

.append( ” <tr> \n”)
  . Append ( "<td align=left> 
  \ N "); 
  Html.append ( "total"). Append (getTotalPageCount ()). Append ( "pages") 
  . Append ( "["). Append (getStart ()). Append (".."). append (getEnd ()) 
  . Append ("/"). append (this.getTotalSize ()). Append ( "] \ n") 
  . Append ( "</ td> \ n") 
  . Append ( "<td align=right> \ n"); 
  If (hasPreviousPage ()) ( 
  Html.append ( "[. Append ("("). Append (getCurrentPageNo () -1) 
  . Append ( ") '> previous] \ n"); 
  ) 
  Html.append ( "") 
  . Append ( "<select name = '") 

.append(pageNoParamName).append(”‘ onChange=’javascript:”)

.append(gotoPage).append(”(this.value)’>   \ N "); 
  String selected = "selected"; 
  For (int i = 1; i <= getTotalPageCount (); i + +) ( 
  If (i == getCurrentPageNo ()) 
  Selected = "selected"; 
  Else selected = ""; 
  Html.append ( "<='"). append option value (i). Append (" ' ") 

.append(selected).append(”>   "). Append (i). Append (" </ option> \ n "); 
  ) 
  If (getCurrentPageNo ()> getTotalPageCount ()) ( 
  Html.append ( "<='"). append option value (getCurrentPageNo ()) 

.append(”‘ selected>   "). Append (getCurrentPageNo ()) 
  . Append ( "</ option> \ n"); 
  ) 
  Html.append ( "</ select> page \ n"); 
  If (hasNextPage ()) ( 
  Html.append ( "[. Append ("("). append ((getCurrentPageNo () +1)) 
  . Append ( ") '> Next] \ n"); 
  ) 
  Html.append ( "</ td> </ tr> </ table> \ n"); 

  Return html.toString (); 

  ) 
  ) 


///////////////////////////////////
  / / 
  / / RowSetPage.java 
  / / Author: evan_zhao@hotmail.com 
  / / 
///////////////////////////////////
  Package page; 

  Import javax.sql.RowSet; 


  / ** 
*

  Title: RowSetPage 


*

  Description: Use data paging RowSet objects Packaging 


*

  Copyright: Copyright (c) 2003 


  * @ Author evan_zhao@hotmail.com 
  * @ Version 1.0 
  * / 

  Public class RowSetPage extends Page ( 
  Private javax.sql.RowSet rs; 

  / ** 
  * Empty page 
  * / 
  Public static final RowSetPage EMPTY_PAGE = new RowSetPage (); 

  / ** 
  * The default constructor, creating an empty page 
  * / 
  Public RowSetPage () ( 
  This (null, 0,0); 
  ) 

  / ** 
  * Constructors paging object 
  * @ Param crs contains a data OracleCachedRowSet 
  * @ Param start page data in the database in the initial position 
  * @ Param totalSize database contains the records of the total number of 
  * / 
  Public RowSetPage (RowSet crs, int start, int totalSize) ( 
  This (crs, start, totalSize, Page.DEFAULT_PAGE_SIZE); 
  ) 

  / ** 
  * Constructors paging object 
  * @ Param crs contains a data OracleCachedRowSet 
  * @ Param start page data in the database in the initial position 
  * @ Param totalSize database contains the records of the total number of 
  * @ PageSize page to accommodate the record number 
  * / 
  Public RowSetPage (RowSet crs, int start, int totalSize, int pageSize) ( 
  Try ( 
  Int avaCount = 0; 
  If (crs! = Null) ( 
  Crs.beforeFirst (); 
  If (crs.next ()) ( 
  Crs.last (); 
  AvaCount = crs.getRow (); 
  ) 
  Crs.beforeFirst (); 
  ) 
  Rs = crs; 
  Super.init (start, avaCount, totalSize, pageSize, rs); 
  ) Catch (java.sql.SQLException sqle) ( 
  Throw new RuntimeException (sqle.toString ()); 
  ) 
  ) 

  / ** 
  * Paging object from the data records 
  * / 
  Public javax.sql.RowSet getRowSet () ( 
  Return rs; 
  ) 


  ) 


///////////////////////////////////
  / / 
  / / PagedStatement.java 
  / / Author: evan_zhao@hotmail.com 
  / / 
///////////////////////////////////

  Package page; 

  Import foo.DBUtil; 

  Import java.math.BigDecimal; 
  Import java.util.List; 
  Import java.util.Iterator; 
  Import java.util.Collections; 

  Import java.sql.Connection; 
  Import java.sql.SQLException; 
  Import java.sql.ResultSet; 
  Import java.sql.Statement; 
  Import java.sql.PreparedStatement; 
  Import java.sql.Timestamp; 
  Import javax.sql.RowSet; 

  / ** 
*

  Title: Paging enquiries 


*

  Description: According to a query on the page and the page data 


*

  Copyright: Copyright (c) 2002 


  * @ Author evan_zhao@hotmail.com 
  * @ Version 1.0 
  * / 
  (Public abstract class PagedStatement 
  Public final static int MAX_PAGE_SIZE = Page.MAX_PAGE_SIZE; 

  Protected String countSQL, querySQL; 
  Protected int pageNo, pageSize, startIndex, totalCount; 
  Protected javax.sql.RowSet rowSet; 
  Protected RowSetPage rowSetPage; 

  Private List boundParams; 

  / ** 
  * Constructors enquiries from all data PageStatement 
  * @ Param sql query sql 
  * / 
  Public PagedStatement (String sql) ( 
  This (sql, 1, MAX_PAGE_SIZE); 
  ) 


  / ** 
  * When a structure for a page of data PageStatement 
  * @ Param sql query sql 
  * @ Param pageNo page 
  * / 
  Public PagedStatement (String sql, int pageNo) ( 
  This (sql, pageNo, Page.DEFAULT_PAGE_SIZE); 
  ) 

  / ** 
  * When a structure for a page PageStatement data, and specify the number of records per page 
  * @ Param sql query sql 
  * @ Param pageNo page 
  * @ Param pageSize page capacity 
  * / 
  Public PagedStatement (String sql, int pageNo, int pageSize) ( 
  This.pageNo = pageNo; 
  This.pageSize = pageSize; 
  This.startIndex = Page.getStartOfAnyPage (pageNo, pageSize); 
  Collections.synchronizedList this.boundParams = (new java.util.LinkedList ()); 

  This.countSQL = "select count (*) from (" sql + + ")"; 
  This.querySQL = intiQuerySQL (sql, this.startIndex, pageSize); 
  ) 


  / ** 
  * Generation for a data sql statement 
  * @ Param sql original query 
  * @ StartIndex begin recording location 
  * @ Size needed to get the record number 
  * / 
  Protected abstract String intiQuerySQL (String sql, int startIndex, int size); 


  / ** 
  * Use the object is the value of parameter settings specified 
  * @ Param index for a first parameter, and the second for 2.    .    . 
  * @ Param obj the object contains parameters 
  * / 
  Public void setObject (int index, Object obj) (throws SQLException 
  BoundParam bp = new BoundParam (index, obj); 
  BoundParams.remove (bp); 
  BoundParams.add (bp); 
  ) 

  / ** 
  * Use the object is the value of parameter settings specified 
  * @ Param index for a first parameter, and the second for 2.    .    . 
  * @ Param obj the object contains parameters 
  * @ Param targetSqlType parameters of the database type 
  * / 
  Public void setObject (int index, Object obj, int targetSqlType) throws SQLException ( 
  BoundParam bp = new BoundParam (index, obj, targetSqlType); 
  BoundParams.remove (bp); 
  BoundParams.add (bp); 
  ) 

  / ** 
  * Use the object is the value of parameter settings specified 
  * @ Param index for a first parameter, and the second for 2.    .    . 
  * @ Param obj the object contains parameters 
  * @ Param targetSqlType parameters of the database type (constants defined in the java.sql.Types) 
  * @ Param scale accuracy, the median after the decimal point 
  * (Only targetSqlType Types.NUMBER or Types.DECIMAL is effective, while ignoring other types) 
  * / 
  Public void setObject (int index, Object obj, int targetSqlType, int scale) throws SQLException ( 
  BoundParam bp = new BoundParam (index, obj, targetSqlType, scale); 
  BoundParams.remove (bp); 
  BoundParams.add (bp); 
  ) 

  / ** 
  * Use the string is the value of parameter settings specified 
  * @ Param index for a first parameter, and the second for 2.    .    . 
  * @ Param str parameter contains the string value 
  * / 
  Public void setString (int index, String str) (throws SQLException 
  BoundParam bp = new BoundParam (index, str); 
  BoundParams.remove (bp); 
  BoundParams.add (bp); 
  ) 

  / ** 
  * Use the string is the value of parameter settings specified 
  * @ Param index for a first parameter, and the second for 2.    .    . 
  * @ Param timestamp value of the parameters contain timestamps 
  * / 
  Public void setTimestamp (int index, Timestamp timestamp) throws SQLException ( 
  BoundParam bp = new BoundParam (index, timestamp); 
  BoundParams.remove (bp); 
  BoundParams.add (bp); 
  ) 

  / ** 
  * Use given integer parameter settings specified value 
  * @ Param index for a first parameter, and the second for 2.    .    . 
  * @ Param value contains integer parameter values 
  * / 
  Public void setInt (int index, int value) throws SQLException ( 
  BoundParam bp = new BoundParam (index, new Integer (value)); 
  BoundParams.remove (bp); 
  BoundParams.add (bp); 
  ) 

  / ** 
  * Use is a long integer value of the parameter settings specified 
  * @ Param index for a first parameter, and the second for 2.    .    . 
  * @ Param value includes the value of long integer parameters 
  * / 
  Public void setLong (int index, long value) (throws SQLException 
  BoundParam bp = new BoundParam (index, the new Long (value)); 
  BoundParams.remove (bp); 
  BoundParams.add (bp); 
  ) 

  / ** 
  * Use double-precision floating point numbers are designated parameter values set 
  * @ Param index for a first parameter, and the second for 2.    .    . 
  * @ Param value of the parameter values include double-precision floating-point 
  * / 
  Public void setDouble (int index, the double value) throws SQLException ( 
  BoundParam bp = new BoundParam (index, the new Double (value)); 
  BoundParams.remove (bp); 
  BoundParams.add (bp); 
  ) 

  / ** 
  * Use BigDecimal given set of parameters specified value 
  * @ Param index for a first parameter, and the second for 2.    .    . 
  * @ Param bd value of the BigDecimal contains parameters 
  * / 
  Public void setBigDecimal (int index, BigDecimal bd) throws SQLException ( 
  BoundParam bp = new BoundParam (index, bd); 
  BoundParams.remove (bp); 
  BoundParams.add (bp); 
  ) 

  Private void setParams (PreparedStatement pst) throws SQLException ( 
  If (pst == null | | this.boundParams == null | | this.boundParams.size () == 0) return; 
  BoundParam param; 
  For (Iterator itr = this.boundParams.iterator (); itr.hasNext ();){ 
  Param = (BoundParam) itr.next (); 
  If (param == null) continue; 
  If (param.sqlType == java.sql.Types.OTHER) ( 
  Pst.setObject (param.index, param.value); 
  Else () 
  Pst.setObject (param.index, param.value, param.sqlType, param.scale); 
  ) 
  ) 
  ) 

  / ** 
  * Implementation of a data queries, after the closure of the implementation of the database connection 
  * @ Return RowSetPage 
  * @ Throws SQLException 
  * / 
  Public RowSetPage executeQuery () (throws SQLException 
  System.out.println ( "executeQueryUsingPreparedStatement"); 
  Connection conn = DBUtil.getConnection (); 
  PreparedStatement pst = null; 
  ResultSet rs = null; 
  Try ( 
  Pst = conn.prepareStatement (this.countSQL); 
  SetParams (pst); 
  Rs = pst.executeQuery (); 
  If (rs.next ()) ( 
  Rs.getInt totalCount = (1); 
  Else () 
  TotalCount = 0; 
  ) 

  Rs.close (); 
  Pst.close (); 

  If (totalCount <1) return RowSetPage.EMPTY_PAGE; 

  Pst = conn.prepareStatement (this.querySQL); 
  System.out.println (querySQL); 
  Pst.setFetchSize (this.pageSize); 
  SetParams (pst); 
  Rs = pst.executeQuery (); 
  / / Rs.setFetchSize (pageSize); 

  This.rowSet = populate (rs); 

  Rs.close (); 
  Rs = null; 
  Pst.close (); 
  Pst = null; 

  This.rowSetPage = new RowSetPage (this.rowSet, startIndex, totalCount, pageSize); 
  Return this.rowSetPage; 
  ) Catch (SQLException sqle) ( 
  / / System.out.println ( "executeQuery SQLException"); 
  Sqle.printStackTrace (); 
  Throw sqle; 
  ) Catch (Exception e) ( 
  E.printStackTrace (); 
  Throw new RuntimeException (e.toString ()); 
  Finally () 
  / / System.out.println ( "executeQuery finally"); 
  DBUtil.close (rs, pst, conn); 
  ) 
  ) 

  / ** 
  * ResultSet data will be filled into CachedRowSet 
  * / 
  Protected abstract RowSet populate (ResultSet rs) throws SQLException; 

  / ** 
  * Packaging from query results into RowSet 
  * @ Return RowSet 
  * / 
  Public javax.sql.RowSet getRowSet () ( 
  Return this.rowSet; 
  ) 


  / ** 
  * Packaging from the query results into RowSetPage 
  * @ Return RowSetPage 
  * / 
  Public RowSetPage getRowSetPage () ( 
  Return this.rowSetPage; 
  ) 

  / ** 
  * Close Database Connection 
  * / 
  Public void close () ( 
  / / Database connection because in the end enquiries or anomalies that closed here do nothing 
  / / Left to expand. 
  ) 

  (Private class BoundParam 
  Int index; 
  Object value; 
  Int sqlType; 
  Int scale; 

  Public BoundParam (int index, Object value) ( 
  This (index, value, java.sql.Types.OTHER); 
  ) 

  Public BoundParam (int index, Object value, int sqlType) ( 
  This (index, value, sqlType, 0); 
  ) 

  Public BoundParam (int index, Object value, int sqlType, int scale) ( 
  This.index = index; 
  This.value = value; 
  This.sqlType = sqlType; 
  This.scale = scale; 
  ) 

  Public boolean equals (Object obj) ( 
  If (obj! = Null & this.getClass (). IsInstance (obj)) ( 
  BoundParam bp = (BoundParam) obj; 
  If (this.index == bp.index) return true; 
  ) 
  Return false; 
  ) 
  ) 

  ) 


///////////////////////////////////
  / / 
  / / PagedStatementOracleImpl.java 
  / / Author: evan_zhao@hotmail.com 
  / / 
///////////////////////////////////
  Package page; 
  Import java.sql.ResultSet; 
  Import java.sql.SQLException; 
  Import javax.sql.RowSet; 
  Import oracle.jdbc.rowset.OracleCachedRowSet; 

  / ** 
*

  Title: tabbed for Oracle Database 


*

  Copyright: Copyright (c) 2002 


  * @ Author evan_zhao@hotmail.com 
  * @ Version 1.0 
  * / 
  Public class PagedStatementOracleImpl extends PagedStatement ( 

  / ** 
  * Constructors enquiries from all data PageStatement 
  * @ Param sql query sql 
  * / 
  Public PagedStatementOracleImpl (String sql) ( 
  Super (sql); 
  ) 


  / ** 
  * When a structure for a page of data PageStatement 
  * @ Param sql query sql 
  * @ Param pageNo page 
  * / 
  Public PagedStatementOracleImpl (String sql, int pageNo) ( 
  Super (sql, pageNo); 
  ) 

  / ** 
  * When a structure for a page PageStatement data, and specify the number of records per page 
  * @ Param sql query sql 
  * @ Param pageNo page 
  * @ Param pageSize page capacity 
  * / 
  Public PagedStatementOracleImpl (String sql, int pageNo, int pageSize) ( 
  Super (sql, pageNo, pageSize); 
  ) 


  / ** 
  * Generation for a data sql statement 
  * @ Param sql original query 
  * @ StartIndex begin recording location 
  * @ Size needed to get the record number 
  * / 
  Protected String intiQuerySQL (String sql, int startIndex, int size) ( 
  StringBuffer querySQL = new StringBuffer (); 
  If (size! = Super.MAX_PAGE_SIZE) ( 
  QuerySQL.append ( "select * from (select my_table .*, rownum as my_rownum from (") 
  . Append (sql) 
  . Append ( ") my_table where rownum <"). Append (startIndex + size) 
  . Append ( ") where my_rownum >="). append (startIndex); 
  Else () 
  QuerySQL.append ( "select * from (select my_table .*, rownum as my_rownum from (") 
  . Append (sql) 
  . Append ( ") my_table") 
  . Append ( ") where my_rownum >="). append (startIndex); 
  ) 
  Return querySQL.toString (); 
  ) 

  / ** 
  * ResultSet data will be filled into CachedRowSet 
  * / 
  Protected RowSet populate (ResultSet rs) throws SQLException ( 
  OracleCachedRowSet ocrs = new OracleCachedRowSet (); 
  Ocrs.populate (rs); 
  Return ocrs; 
  ) 

  ) 



  This paper quoted a circular Address: http://blog.csdn.net/wanchao2001/services/trackbacks/460066.aspx 
  Function TempSave (ElementID) (CommentsPersistDiv.setAttribute ( "CommentContent" document.getElementById (ElementID). Value); CommentsPersistDiv.save ( "CommentXMLStore");) function Restore (ElementID) (CommentsPersistDiv.load ( "CommentXMLStore"); document . getElementById (ElementID). CommentsPersistDiv.getAttribute value = ( "CommentContent");) </ td> </ tr> <tr> 

  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: ,

Releated Java Articles

Comments

Leave a Reply