?? connectionservlet_21.java
字號:
/* * @(#)ConnectionServlet_21 * * Copyright (c) 1998 Karl Moss. All Rights Reserved. * * You may study, use, modify, and distribute this software for any * purpose provided that this copyright notice appears in all copies. * * This software is provided WITHOUT WARRANTY either expressed or * implied. * * @author Karl Moss * @version 1.0 * @date 20Mar99 * */package javaservlets.db;import javax.servlet.*;import javax.servlet.http.*;/** * <p>This is a simple servlet that holds a global connection * pool. The Servlet context is used to store a named attribute * (this servlet) so that other servlets have access to the * connection pool */public class ConnectionServlet_21 extends HttpServlet{ // Our connection pool. javaservlets.jdbc.ConnectionPool m_connectionPool; // Context attribute key public static String KEY = "javaservlets.db.ConnectionServlet_21"; /** * <p>Get a JDBC connection from the pool * * @return JDBC connection */ public java.sql.Connection getConnection() throws Exception { java.sql.Connection con = null; if (m_connectionPool != null) { con = m_connectionPool.getConnection(); } return con; } /** * <p>Closes the given JDBC connection * * @param con JDBC Connection */ public void close(java.sql.Connection con) { if (m_connectionPool != null) { m_connectionPool.close(con); } } /** * <p>Initialize the servlet. This is called once when the * servlet is loaded. It is guaranteed to complete before any * requests are made to the servlet * * @param cfg Servlet configuration information */ public void init(ServletConfig cfg) throws ServletException { super.init(cfg); // Create our connection pool m_connectionPool = new javaservlets.jdbc.ConnectionPool(); // Initialize the connection pool. This will start all // of the connections as specified in the connection // pool configuration file try { m_connectionPool.initialize(); } catch (Exception ex) { // Convert the exception ex.printStackTrace(); throw new ServletException ("Unable to initialize connection pool"); } // Add this servlet to the context so that other servlets // can find us getServletContext().setAttribute(KEY, this); } /** * <p>Destroy the servlet. This is called once when the servlet * is unloaded. */ public void destroy() { // Remove the attribute from the context getServletContext().removeAttribute(KEY); // Tear down our connection pool if it was created if (m_connectionPool != null) { m_connectionPool.destroy(); } super.destroy(); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -