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


001:        /*
002:         *    GeoTools - OpenSource mapping toolkit
003:         *    http://geotools.org
004:         *    (C) 2002-2006, 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:        package org.geotools.data.mysql;
017:
018:        import java.sql.Connection;
019:        import java.sql.DriverManager;
020:        import java.sql.SQLException;
021:        import java.util.HashMap;
022:        import java.util.Map;
023:        import java.util.Properties;
024:        import java.util.logging.Logger;
025:
026:        import javax.sql.DataSource;
027:
028:        import org.geotools.data.jdbc.ConnectionPool;
029:        import org.geotools.data.jdbc.ConnectionPoolManager;
030:        import org.geotools.data.jdbc.datasource.DataSourceFinder;
031:        import org.geotools.data.jdbc.datasource.DataSourceUtil;
032:
033:        import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
034:
035:        /**
036:         * Creates ConnectionPool objects for a certain MySQL database instance.
037:         * @author Gary Sheppard garysheppard@psu.edu
038:         * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/mysql/src/main/java/org/geotools/data/mysql/MySQLConnectionFactory.java $
039:         * @deprecated Use {@link DataSource}, {@link DataSourceUtil} and {@link DataSourceFinder} instead
040:         */
041:        public class MySQLConnectionFactory {
042:
043:            /** Standard logging instance */
044:            private static final Logger LOGGER = org.geotools.util.logging.Logging
045:                    .getLogger("org.geotools.data.mysql");
046:
047:            /** Creates Mysql-specific JDBC driver class. */
048:            private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
049:            private static final String MYSQL_URL_PREFIX = "jdbc:mysql://";
050:            private static Map _dataSources = new HashMap();
051:            private String _dbURL;
052:            private String _username = "";
053:            private String _password = "";
054:
055:            /**
056:             * Creates a new MySQLConnectionFactory object from a MySQL database URL.  This
057:             * is normally of the following format:<br/>
058:             * <br/>
059:             * jdbc:mysql://<host>:<port>/<instance>
060:             * @param url the MySQL database URL
061:             */
062:            public MySQLConnectionFactory(String url) {
063:                _dbURL = url;
064:            }
065:
066:            /**
067:             * Creates a new MySQLConnectionFactory object from a host name, port number,
068:             * and instance name.
069:             * @param host the MySQL database host
070:             * @param port the port number for the MySQL database
071:             * @param instance the MySQL database instance name
072:             */
073:            public MySQLConnectionFactory(String host, int port, String instance) {
074:                this (MYSQL_URL_PREFIX + host + ":" + String.valueOf(port) + "/"
075:                        + instance);
076:            }
077:
078:            /**
079:             * Creates a new MySQLConnectionFactory object from a host name and an instance
080:             * name, using the normal MySQL port number of 3306.
081:             * @param host the MySQL database host
082:             * @param instance the MySQL database instance name
083:             */
084:            public MySQLConnectionFactory(String host, String instance) {
085:                this (host, 3306, instance);
086:            }
087:
088:            /**
089:             * Creates and returns a MySQL ConnectionPool, or gets an existing ConnectionPool
090:             * if one exists, based upon the username and password parameters passed to this
091:             * method.  This is shorthand for the following two calls:<br>
092:             * <br>
093:             * connPool.setLogin(username, password);<br>
094:             * connPool.getConnectionPool();<br>
095:             * @param username the MySQL username
096:             * @param password the password corresponding to <code>username</code>
097:             * @return a MySQL ConnectionPool object
098:             * @throws SQLException if an error occurs connecting to the MySQL database
099:             */
100:            public ConnectionPool getConnectionPool(String username,
101:                    String password) throws SQLException {
102:                setLogin(username, password);
103:                return getConnectionPool();
104:            }
105:
106:            /**
107:             * Creates a database connection method to initialize a given database for
108:             * feature extraction with the user and password params.
109:             *
110:             * @param user the name of the user connect to connect to the pgsql db.
111:             * @param password the password for the user.
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(String user, String password)
118:                    throws SQLException {
119:                Properties props = new Properties();
120:                props.put("user", user);
121:                props.put("password", password);
122:                return getConnection(props);
123:            }
124:
125:            /**
126:             * Creates a database connection method to initialize a given database for
127:             * feature extraction with the given Properties.
128:             *
129:             * @param props Should contain at a minimum the user and password.
130:             *        Additional properties, such as charSet, can also be added.
131:             *
132:             * @return the sql Connection object to the database.
133:             *
134:             * @throws SQLException if the postgis sql driver could not be found
135:             */
136:            public Connection getConnection(Properties props)
137:                    throws SQLException {
138:                // makes a new feature type bean to deal with incoming
139:                Connection dbConnection = null;
140:
141:                // Instantiate the driver classes
142:                try {
143:                    Class.forName(DRIVER_CLASS);
144:                    LOGGER.finest("getting connection at " + _dbURL
145:                            + "with props: " + props);
146:                    dbConnection = DriverManager.getConnection(_dbURL, props);
147:                } catch (ClassNotFoundException cnfe) {
148:                    throw new SQLException("Postgis driver was not found.");
149:                }
150:
151:                return dbConnection;
152:            }
153:
154:            /**
155:             * Creates and returns a MySQL ConnectionPool, or gets an existing ConnectionPool
156:             * if one exists, based upon the username and password set in this MySQLConnectionFactory
157:             * object.  Please call setLogin before calling this method, or use getConnectionPool(String, String)
158:             * instead.
159:             * @return a MySQL ConnectionPool object
160:             * @throws SQLException if an error occurs connecting to the DB
161:             */
162:            public ConnectionPool getConnectionPool() throws SQLException {
163:                String poolKey = _dbURL + _username + _password;
164:                MysqlConnectionPoolDataSource poolDataSource = (MysqlConnectionPoolDataSource) _dataSources
165:                        .get(poolKey);
166:
167:                if (poolDataSource == null) {
168:                    poolDataSource = new MysqlConnectionPoolDataSource();
169:
170:                    poolDataSource.setURL(_dbURL);
171:                    poolDataSource.setUser(_username);
172:                    poolDataSource.setPassword(_password);
173:
174:                    _dataSources.put(poolKey, poolDataSource);
175:                }
176:
177:                ConnectionPoolManager manager = ConnectionPoolManager
178:                        .getInstance();
179:                ConnectionPool connectionPool = manager
180:                        .getConnectionPool(poolDataSource);
181:
182:                return connectionPool;
183:            }
184:
185:            /**
186:             * Sets the MySQL database login credentials.
187:             * @param username the username
188:             * @param password the password
189:             */
190:            public void setLogin(String username, String password) {
191:                _username = username;
192:                _password = password;
193:            }
194:
195:            public void free(ConnectionPool connectionPool) {
196:                if (!connectionPool.isClosed()) {
197:                    connectionPool.close();
198:                }
199:                ConnectionPoolManager.getInstance().free(connectionPool);
200:            }
201:
202:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.