Snonnam cat 5 database connection pool + javabean + paging technology + JDBC3.0

xiaoxiao2021-03-06  89

The day did not look, finally developed "Database Connection Pool JavaBean JSP SQL Server JDBC3.0 database paging technology", which is now published, and there is a bad point of mind. Main features: 1. Use the latest JDBC3.0 database driver. 2. A large reduction in JSP repeated call JavaBean, you can write SQL directly, no need to use the connection database connection pool. 3. Give a lot of work to JavaBean, JSP is responsible for page control. 4. The biggest feature is extremely simple, and the program is also extremely simple, and it is very suitable for beginners. 5. Used is the "Snon Dafa Cat" database connection pool, which is convenient and fast.

"Page.jsp" *************************************************** ********************** File name "page.jsp" <% @ page language = "java" import = "java.sql. *, My. * "%> <% @ page contenttype =" text / html; charset = GB2312 "%> < Table align = "center" border = 1> <% string countQuery = "SELECT COUNT (*) from product information"; string query = "select * from product information"; ResultSet RS = PAGI.QuerySQL (countQuery, query, request) String footer = pagi.pagefooter ();%> Product number Product Name <% ing (PAGI .intPageCount> 0) {INT i = 0; while (rs.next ()) {i ; IF ((pagi.intpage-1) * Pagi.intpagesize && (i <= pagi.intpage * PAGI. INTPAGESIZE)) {%> <% = r.gs (1)%> <% = rs.getstring (2)%> < %}}} out.println (" Footer " ); rs.close (); pagi.close_all ();%>

**************************************** File name "pagi.java" *** *********************************************************** ****

File name "pagi.java" package my;

Import java.util. *; import java.sql. *; import java.io. *; import javax.servlet. *; import javax.servlet.http. *; import my.db. *; public class pagi {Resultset Counttopicrs = NULL; // Initialization Total Record RS Variable ResultSet Pagirs = NULL; // Initialization Paging When rs Variable Public Int IntCountTopic = 0; // Total Public Int INT INT INT INT INT INT INT INT INT INT INTPAGECOUNT; // Total page PUBLIC INT INTPAGE = 1; // Current page number PUBLIC STRING NOWPAGE; // INT I; public string httpfile; // Initialize the current page INTPAGE variable to accurately obtain the current page. / / The file DB DB of the current address bar; // Defines an object of the LinkDB class.

Public PAGI () // Defines the constructor, initializes the number of topics and databases of each page. {INTPAGESIZE = 4; // The number of records displayed per page DB = New db ();}

// countsql: The total recorded query string. [Form is SELECT Count (*) from Tablename] // Pagisql: The Query string to be paid. [Form is SELECT * from Tablename where ...] // request: Variables during the parameter pass. [Pages variable used to control the page] Public ResultSet QuerySQL (String CountSQL, String Pagisql, httpservletRequest Request) throws exception {httpfile = request.getRequesturi (); // Get the current file name. NOWPAGE = Request.getParameter ("Pages"); // Get the current page and give the value to the INTPAGE variable. [PageS parameters must be posted in the paging bar] if (nowPage == NULL) {INTPAGE = 1;} else {INTPAGE = Integer.Parseint (now); IF (INTPAGE <1) INTPAGE = 1;}

COUNTTOPICRS = db.executeQuery (countsql); @@@@@@@ 获取 总 集 集 集.

IF (counttopicrs.next ()) {INTCOUNTTOPIC = COUNTTOPICRS.GETINT (1);} intPageCount = (intcounttopic intPagesize-1) / intPageSize; // Get the total number of pages.

IF (INTPAGE> INTPAGECOUNT) // If the current page is greater than the total number of pages, the current page is equal to the total number of pages. {INTPAGE = INTPAGENCOUNT;} counttopicrs.close (); // Turns the data set of the total number of topics. Db.close_all (); pagirs = db.executeQuery (PAGISQL); @@@@@@@@@@jucked to perform the result set of paging. Return Pagirs;} // end querysql function.

Public int getCountTopic () // Get the total number of records. {RETURN INTCOUNTTTTOPIC;} public int getPageCount () // Get the total number of pages. {RETURN INTPAGECOUNT;

Public int getintpage () // Gets the current number of pages. {Return INTPAGE;

Public string Pagefooter () {string str = ""; int next, prev; prev = INTPAGE-1; Next = INTPAGE 1; STR = "Query getCounttopic () " Strip Record " " Total " getPageCount () " page "; str =" B "Font Color = Red> GetInetPage () " Page "; if (INTPAGE> 1) STR =" Home "; Else Str =" Home "; if (INTPAGE> 1 ) Str = " Previous "; ElseStr = "Previous"; if (INTPAGE " Next page "; Else Str =" Next "; if (INTPAGECUNT> 1 && INTPAGE! = INTPAGECOUNT) STR = "" 百家; "; ElseStr =" Last ";

Return str;} public void close_all () {db.close_all ();}}

************************************************file name "Db.java" *************************************************** *********** File name "db.java" package my;

Import java.sql. *; import javax.naming. *; import javax.sql.datasource; // an tool class for finding data sources. public class DB {private Connection con = null; private Statement stmt = null; ResultSet rs = null; public ResultSet executeQuery (String sql) throws Exception {rs = null; try {Context initCtx = new javax.naming.InitialContext (); Context Envctx = (context) INITCTX.LOOKUP ("Java: Comp / ENV"); DataSource DS = (Datasource) Envctx.lookup ("JDBC / BN"); con = ds.getConnection (); stmt = con.createstatement (Resultset) .TYPE_SCROLL_SENSITIVE, RESULTSET.CONCUR_READ_ONLY); RS = stmt.executeQuery (SQL);} catch (sqlexception e) {throw e;} catch (namingexception e) {throw E;}

Return RS;

// perform Insert, Update statement public void executeUpdate (String sql) throws Exception {stmt = null; rs = null; try {Context initCtx = new javax.naming.InitialContext (); Context envCtx = (Context) initCtx.lookup ( " java: comp / env "); DataSource ds = (DataSource) envCtx.lookup (" jdbc / bn "); con = ds.getConnection (); stmt = con.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); stmt. ExecuteQuery (SQL); stmt.close (); con.close ();} catch (sqlexception ex) {system.err.println ("Execute SQL statement error:" ex.getMessage ());}}

// Close Stmt and close the connection public void close_all () {Try {stmt.close (); con.close ();} catch (sqlexception e) {E.PrintStackTrace ();}}}

**************************** "Database connection pool in the soup mother cat "******************************************************** ****************************** ................... factory org.apache.commons.dbcp.basicDataSourceFactory driverclassname COM .microsoft.jdbc.sqlser.sqlserverdriver

URL JDBC: Microsoft: SQLServer: //127.0.0.1: 1433; DatabaseName = JSPDEV

Username cyg

Password 325345353

maxactive 20

maxidle 20

maxWait -1


New Post(0)