ConnectionPool2.class
//Creates a pool of database connections. package com; import java.sql.*; import java.util.*; public class ConnectionPool2 { static Stack st = new Stack(); static Connection connect; static Connection connection3; static int flag = 0; static int count = 20; public static void ConnectionPool2() { } public static void start() { System.out.println("connection pool started"); if( flag == 1) count = 5; while(st.size()
20) { closeConnections(); } connect = (Connection)st.pop(); } else { start(); connect = (Connection)st.pop(); //Another Method is needed here to create more connections when the stack is empty } return connect; } //After use the connection is returned to the stack for reuse public static void returnConnection(Connection c) { st.push(c); } //Method called above when the number of connections becomes to great. public static void closeConnections() { for(int i = 0; i <= 8;i++) { try { Connection con = (Connection)st.pop(); con.close(); } catch(SQLException sq) { } } } }