Source Code Cross Referenced for JDBCConnectionFactory.java in  » GIS » GeoTools-2.4.1 » org » geotools » data » geometryless » 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 » GIS » GeoTools 2.4.1 » org.geotools.data.geometryless 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *    Geotools2 - OpenSource mapping toolkit
003:         *    http://geotools.org
004:         *    (C) 2002, Geotools Project Managment Committee (PMC)
005:         *
006:         *    This library is free software; you can redistribute it and/or
007:         *    modify it under the terms of the GNU Lesser General Public
008:         *    License as published by the Free Software Foundation;
009:         *    version 2.1 of the License.
010:         *
011:         *    This library is distributed in the hope that it will be useful,
012:         *    but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:         *    Lesser General Public License for more details.
015:         *
016:         */
017:        package org.geotools.data.geometryless;
018:
019:        import java.sql.Connection;
020:        import java.sql.DriverManager;
021:        import java.sql.SQLException;
022:        import java.util.HashMap;
023:        import java.util.Map;
024:        import java.util.Properties;
025:        import java.util.logging.Logger;
026:
027:        import org.geotools.data.jdbc.ConnectionPool;
028:        import org.geotools.data.jdbc.ConnectionPoolManager;
029:
030:        /**
031:         * Creates ConnectionPool objects for a certain JDBC database instance.
032:         * @author Rob Atkinson rob@socialchange.net.NOSPAM.au
033:         * @author Gary Sheppard garysheppard@psu.edu
034:         * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/geometryless/src/main/java/org/geotools/data/geometryless/JDBCConnectionFactory.java $
035:         */
036:        public class JDBCConnectionFactory {
037:
038:            /** Standard logging instance */
039:            private static final Logger LOGGER = org.geotools.util.logging.Logging
040:                    .getLogger("org.geotools.data.geometryless");
041:
042:            /** Creates configuration-driven JDBC driver class. */
043:            private String _driver = "";
044:            private static Map _dataSources = new HashMap();
045:            private String _dbURL;
046:            private String _username = "";
047:            private String _password = "";
048:            /** An alternate character set to use. */
049:            private String charSet;
050:
051:            /**
052:             * Creates a new JDBCConnectionFactory object from a specified database URL.  
053:            This is the only constructor supported since there is significant variability in implementation syntax
054:            <br/>
055:             * <br/>
056:             * jdbc:mysql://<host>:<port>/<instance>
057:             * @param url the JDBC database URL
058:             */
059:            public JDBCConnectionFactory(String url, String driver) {
060:                _driver = driver;
061:                _dbURL = url;
062:            }
063:
064:            /**
065:             * Creates and returns a JDBC ConnectionPool, or gets an existing ConnectionPool
066:             * if one exists, based upon the username and password parameters passed to this
067:             * method.  This is shorthand for the following two calls:<br>
068:             * <br>
069:             * connPool.setLogin(username, password);<br>
070:             * connPool.getConnectionPool();<br>
071:             * @param username the JDBC username
072:             * @param password the password corresponding to <code>username</code>
073:             * @return a JDBC ConnectionPool object
074:             * @throws SQLException if an error occurs connecting to the JDBC database
075:             */
076:            public ConnectionPool getConnectionPool(String username,
077:                    String password) throws SQLException {
078:                setLogin(username, password);
079:                return getConnectionPool();
080:            }
081:
082:            /**
083:             * Creates a database connection method to initialize a given database for
084:             * feature extraction with the user and password params.
085:             *
086:             * @param user the name of the user connect to connect to the db.
087:             * @param password the password for the user.
088:             *
089:             * @return the sql Connection object to the database.
090:             *
091:             * @throws SQLException if the configured sql driver could not be found
092:             */
093:            public Connection getConnection(String user, String password)
094:                    throws SQLException {
095:                Properties props = new Properties();
096:                props.put("user", user);
097:                props.put("password", password);
098:
099:                if (charSet != null) {
100:                    props.put("charSet", charSet);
101:                }
102:
103:                return getConnection(props);
104:            }
105:
106:            /**
107:             * Creates a database connection method to initialize a given database for
108:             * feature extraction with the given Properties.
109:             *
110:             * @param props Should contain at a minimum the user and password.
111:             *        Additional properties, such as charSet, can also be added.
112:             *
113:             * @return the sql Connection object to the database.
114:             *
115:             * @throws SQLException if the postgis sql driver could not be found
116:             */
117:            public Connection getConnection(Properties props)
118:                    throws SQLException {
119:                // makes a new feature type bean to deal with incoming
120:                Connection dbConnection = null;
121:
122:                //       dbConnection = getConnectionPool().getConnection();
123:
124:                // Instantiate the driver classes
125:                try {
126:                    Class.forName("com.mysql.jdbc.Driver");
127:                    LOGGER.fine("getting connection at " + _dbURL + " using "
128:                            + _driver + " with props: " + props);
129:                    dbConnection = DriverManager.getConnection(_dbURL, props);
130:                } catch (ClassNotFoundException cnfe) {
131:                    throw new SQLException("JDBC driver (" + _driver
132:                            + " was not found.");
133:                }
134:
135:                return dbConnection;
136:            }
137:
138:            /**
139:             * Creates and returns a  ConnectionPool, or gets an existing ConnectionPool
140:             * if one exists, based upon the username and password set in this JDBCConnectionFactory
141:             * object.  Please call setLogin before calling this method, or use getConnectionPool(String, String)
142:             * instead.
143:             * @return a  ConnectionPool object
144:             * @throws SQLException if an error occurs connecting to the DB
145:             */
146:            public ConnectionPool getConnectionPool() throws SQLException {
147:                String poolKey = _dbURL + _username + _password;
148:
149:                ConnectionPoolFacade poolDataSource = (ConnectionPoolFacade) _dataSources
150:                        .get(poolKey);
151:                if (poolDataSource == null) {
152:
153:                    poolDataSource = new ConnectionPoolFacade(poolKey, _driver);
154:
155:                    poolDataSource.setURL(_dbURL);
156:                    poolDataSource.setUser(_username);
157:                    poolDataSource.setPassword(_password);
158:
159:                    _dataSources.put(poolKey, poolDataSource);
160:
161:                }
162:
163:                ConnectionPoolManager manager = ConnectionPoolManager
164:                        .getInstance();
165:                ConnectionPool connectionPool = manager
166:                        .getConnectionPool(poolDataSource);
167:
168:                return connectionPool;
169:            }
170:
171:            /**
172:             * Sets the JDBC database login credentials.
173:             * @param username the username
174:             * @param password the password
175:             */
176:            public void setLogin(String username, String password) {
177:                _username = username;
178:                _password = password;
179:            }
180:
181:            public void free(ConnectionPool connectionPool) {
182:                if (!connectionPool.isClosed()) {
183:                    connectionPool.close();
184:                }
185:                ConnectionPoolManager.getInstance().free(connectionPool);
186:            }
187:
188:            /**
189:             * Sets a different character set for the postgis driver to use.
190:             *
191:             * @param charSet the string of a valid charset name.
192:             */
193:            public void setCharSet(String charSet) {
194:                this.charSet = charSet;
195:            }
196:
197:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.