⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connectionpoolservlet.java~17~

📁 Describes basic connections from java to database.
💻 JAVA~17~
字号:
package com.jsc.database;

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.jsc.utils.*;

public class ConnectionPoolServlet extends HttpServlet {

    private Vector m_v = null;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        try {
            Configurator configurator = new Configurator(ConfData.CONFIG_FILE);
            Configuration configuration = configurator.getConfiguration();
            Vector v = configuration.getDBConfig();
            m_v = v;
            for (int i=0; i<v.size(); i++) {
                DBConfig dbc = (DBConfig)v.elementAt(i);
                createPool(dbc);
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("This Servlet does not service requests!");
        out.close();
    }

    public void destroy() {
        destoryPool("mysql");
    }

    public String getServletInfo() {
        return "ConnectionPoolServlet Information";
    }

    public void createPool(DBConfig dbc) {
        String dbname = dbc.m_name;
        ConnectionPool pool = new ConnectionPool();
        try {
            String DBDriver = configuration.get(nodeName, "DBDriver");
            String DBURL = configuration.get(nodeName, "DBURL");
            String DBConnectSize = configuration.get(nodeName, "DBConnectSize");
            String DBUsername = configuration.get(nodeName, "DBUsername");
            String DBPassword = configuration.get(nodeName, "DBPassword");

            System.out.println("########################################");
            System.out.println("# start " + nodeName + " connection pool");
            pool.setDriver(DBDriver);
            pool.setURL(DBURL);
            pool.setUsername(DBUsername);
            pool.setSize(new Integer(DBConnectSize).intValue());
            pool.setPassword(DBPassword);

            // Initialize the pool
            pool.initializePool();

            // Once the pool is Initailized, add it to the
            // Global ServletContext.  This makes it available
            // To other servlets using the same ServletContext.
            ServletContext context = getServletContext();
            context.setAttribute(nodeName.toUpperCase() + "_POOL", pool);
            System.err.println("# CONTEXT \"" + nodeName.toUpperCase() + "_POOL\" ADDED");
            System.out.println("########################################");
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }

    public void destoryPool(String nodeName) {
        ServletContext context = getServletContext();
        ConnectionPool pool = (ConnectionPool)context.getAttribute(nodeName.toUpperCase() + "_POOL");
        if ( pool != null ) {
            pool.emptyPool();
            context.removeAttribute(nodeName.toUpperCase() + "_POOL");
        } else {
            System.err.println("Could not get a reference to Pool!");
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -