KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > echoserver > DBPoolUtil


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package echoserver;
16
17 import org.quickserver.net.*;
18 import org.quickserver.net.server.*;
19 import org.quickserver.sql.*;
20 import org.quickserver.util.xmlreader.*;
21
22 import java.io.*;
23 import java.util.logging.*;
24 import java.util.*;
25 import java.sql.*;
26
27 public class DBPoolUtil implements org.quickserver.sql.DBPoolUtil {
28     private HashMap dbPool;
29
30     public void setDatabaseConnections(Iterator iterator)
31             throws Exception JavaDoc {
32         dbPool = new HashMap();
33         while(iterator.hasNext()) {
34             DatabaseConnectionConfig dcc =
35                 (DatabaseConnectionConfig)iterator.next();
36             dbPool.put(dcc.getId(), dcc);
37         }
38     }
39
40     public boolean initPool() {
41         if(dbPool==null)
42             throw new IllegalStateException JavaDoc("Call setDatabaseConnections first.!!");
43         Iterator iterator = dbPool.keySet().iterator();
44         try {
45             while(iterator.hasNext()) {
46                 DatabaseConnectionConfig dcc = (DatabaseConnectionConfig)
47                     dbPool.get( (String JavaDoc)iterator.next() );
48                 Class.forName(dcc.getDriver());
49             }
50             return true;
51         } catch(Exception JavaDoc e) {
52             System.err.println("In DBPoolUtil.initPool : "+e);
53             return false;
54         }
55     }
56
57     public boolean clean() {
58         dbPool = null;
59         return true;
60     }
61
62     public Connection getConnection(String JavaDoc id) throws Exception JavaDoc {
63         DatabaseConnectionConfig dcc = (DatabaseConnectionConfig)
64                 dbPool.get( id );
65         return DriverManager.getConnection(dcc.getUrl(), dcc.getUsername(),
66             dcc.getPassword());
67     }
68 }
69
Popular Tags