Source Code Cross Referenced for NamedConnectionManager.java in  » XML-UI » XUI » net » xoetrope » optional » data » sql » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » XML UI » XUI » net.xoetrope.optional.data.sql 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package net.xoetrope.optional.data.sql;
002:
003:        import java.sql.Connection;
004:        import java.sql.DriverManager;
005:        import java.sql.SQLException;
006:        import java.util.Vector;
007:
008:        import net.xoetrope.optional.pool.PoolObject;
009:
010:        /**
011:         * <p>An extension of the ConnectionManager to manage connections to multiple
012:         * databases</p>
013:         * <p>Copyright: Copyright (c) 2003<br>
014:         * License: see license.txt</p>
015:         * $Revision: 1.4 $
016:         */
017:        public class NamedConnectionManager extends ConnectionManager {
018:            /**
019:             * Storage for the various connection parameters
020:             */
021:            protected Vector connectionParameters;
022:
023:            /**
024:             * Constructs a new ConnectionManager, called by getInstance
025:             * @param aurl the database connection string
026:             * @param auser the database user
027:             * @param apassword the database password
028:             * @param size the initial pool size
029:             */
030:            protected NamedConnectionManager(String aDriver, String aurl,
031:                    String auser, String apassword, int size) {
032:                super (aDriver, aurl, auser, apassword, size);
033:
034:                connectionParameters = new Vector();
035:                addConnection("default", aDriver, aurl, auser, apassword);
036:            }
037:
038:            /**
039:             * Get a new object from the pool. A lease is granted to the new object
040:             * @param connParamName the name of the connection parameters
041:             * @return a new PoolObject
042:             * @throws Exception
043:             */
044:            public PoolObject getObject(String connParamName) throws Exception {
045:                NamedConnectionObject c;
046:
047:                String connectionName = "default";
048:                if (connParamName != null)
049:                    connectionName = connParamName;
050:
051:                for (int i = 0; i < objects.size(); i++) {
052:                    c = (NamedConnectionObject) objects.elementAt(i);
053:                    if (c.lease()
054:                            && (c.getName().compareTo(connectionName) == 0))
055:                        return c;
056:                }
057:
058:                c = (NamedConnectionObject) getNewObject(connectionName);
059:                c.lease();
060:                objects.addElement(c);
061:
062:                return c;
063:            }
064:
065:            /**
066:             * Creates a new ConnectionObject, loading the JDBC driver in the process if necessary
067:             * @return
068:             * @throws SQLException
069:             */
070:            public PoolObject getNewObject(String connParamName)
071:                    throws SQLException {
072:                ConnectionParameters connectionParams = (ConnectionParameters) findConnectionParams(connParamName);
073:                try {
074:                    Class.forName(connectionParams.driverName);
075:                } catch (ClassNotFoundException e) {
076:                    e.printStackTrace();
077:                }
078:
079:                Connection conn = DriverManager.getConnection(
080:                        connectionParams.url, connectionParams.user,
081:                        connectionParams.password);
082:                return new NamedConnectionObject(connectionParams.name, conn,
083:                        this );
084:            }
085:
086:            /**
087:             * Gets an instance of the ConnectionManager
088:             * @return
089:             */
090:            public static ConnectionManager getInstance() {
091:                if (connMgr != null)
092:                    return connMgr;
093:
094:                return connMgr = new NamedConnectionManager(defaultDriverName,
095:                        defaultDatabaseUrl, "sa", "", 10);
096:            }
097:
098:            /**
099:             * Gets a connection.
100:             * @param connectionName the name of the connection object
101:             * @return the ConnectionObject
102:             * @throws SQLException
103:             */
104:            public ConnectionObject getConnection(String connectionName)
105:                    throws SQLException {
106:                try {
107:                    return (ConnectionObject) ConnectionManager.getInstance()
108:                            .getObject(connectionName);
109:                } catch (Exception ex) {
110:                    throw new SQLException(ex.getMessage());
111:                }
112:            }
113:
114:            /**
115:             * Gets an instance of the ConnectionManager. Any open connections are closed
116:             * prior to attempting the new connection.
117:             * @param name the name by which the connection paramenters will be referenced
118:             * @param driver the driver class name
119:             * @param url the database URL
120:             * @param userName the user name
121:             * @param password the password
122:             */
123:            public void addConnection(String name, String driver, String url,
124:                    String userName, String password) {
125:                // Check for the existance of a connection
126:                ConnectionParameters connectionParams = (ConnectionParameters) findConnectionParams(name);
127:                if (connectionParams != null)
128:                    return;
129:
130:                try {
131:                    // Try to load the driver.
132:                    Class.forName(driver);
133:
134:                    // Attemp to add the connection details
135:                    connectionParams = new ConnectionParameters();
136:                    connectionParams.name = name;
137:                    connectionParams.driverName = driver;
138:                    connectionParams.url = url;
139:                    connectionParams.user = userName;
140:                    connectionParams.password = password;
141:                    connectionParameters.addElement(connectionParams);
142:                } catch (ClassNotFoundException e) {
143:                    e.printStackTrace();
144:                }
145:            }
146:
147:            /**
148:             * Find a nameds set of connection parameters
149:             * @param connParamName the name of the set of connection parameters
150:             * @return the set or null if nothing is found
151:             */
152:            protected Object findConnectionParams(String connParamName) {
153:                int numElements = connectionParameters.size();
154:                for (int i = 0; i < numElements; i++) {
155:                    // Find the named element
156:                    if (((ConnectionParameters) connectionParameters
157:                            .elementAt(i)).name.compareTo(connParamName) == 0)
158:                        return connectionParameters.elementAt(i);
159:                }
160:
161:                return null;
162:            }
163:
164:            /**
165:             * Gets an instance of the ConnectionManager. Any open connections are closed
166:             * prior to attempting the new connection.
167:             * @return
168:             */
169:            public ConnectionManager reset(String name, String driver,
170:                    String url, String userName, String password) {
171:                if (connMgr != null)
172:                    connMgr.closeObjects();
173:
174:                defaultDriverName = driver;
175:
176:                try {
177:                    // Try to load the driver.
178:                    Class.forName(defaultDriverName);
179:
180:                    // Attempt to make the connection
181:                    return connMgr = new NamedConnectionManager(driver, url,
182:                            userName, password, 10);
183:                } catch (ClassNotFoundException e) {
184:                    e.printStackTrace();
185:                }
186:
187:                return null;
188:            }
189:        }
190:
191:        /**
192:         * An internal class for storing the connection parameters
193:         */
194:        class ConnectionParameters {
195:            public String name;
196:            public String url;
197:            public String user;
198:            public String password;
199:            public String driverName;
200:            public String databaseUrl;
201:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.