Source Code Cross Referenced for MysqlDataSource.java in  » Database-JDBC-Connection-Pool » mysql-connector-java-5.1.3 » com » mysql » jdbc » jdbc2 » optional » 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 » Database JDBC Connection Pool » mysql connector java 5.1.3 » com.mysql.jdbc.jdbc2.optional 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         Copyright (C) 2002-2007 MySQL AB
003:
004:         This program is free software; you can redistribute it and/or modify
005:         it under the terms of version 2 of the GNU General Public License as 
006:         published by the Free Software Foundation.
007:
008:         There are special exceptions to the terms and conditions of the GPL 
009:         as it is applied to this software. View the full text of the 
010:         exception in file EXCEPTIONS-CONNECTOR-J in the directory of this 
011:         software distribution.
012:
013:         This program is distributed in the hope that it will be useful,
014:         but WITHOUT ANY WARRANTY; without even the implied warranty of
015:         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016:         GNU General Public License for more details.
017:
018:         You should have received a copy of the GNU General Public License
019:         along with this program; if not, write to the Free Software
020:         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
021:
022:
023:
024:         */
025:        package com.mysql.jdbc.jdbc2.optional;
026:
027:        import com.mysql.jdbc.ConnectionPropertiesImpl;
028:        import com.mysql.jdbc.NonRegisteringDriver;
029:        import com.mysql.jdbc.exceptions.NotYetImplementedException;
030:
031:        import java.io.PrintWriter;
032:        import java.io.Serializable;
033:
034:        import java.sql.SQLException;
035:
036:        import java.util.Properties;
037:
038:        import javax.naming.NamingException;
039:        import javax.naming.Reference;
040:        import javax.naming.Referenceable;
041:        import javax.naming.StringRefAddr;
042:
043:        import javax.sql.DataSource;
044:
045:        /**
046:         * A JNDI DataSource for a Mysql JDBC connection
047:         * 
048:         * @author Mark Matthews
049:         */
050:        public class MysqlDataSource extends ConnectionPropertiesImpl implements 
051:                DataSource, Referenceable, Serializable {
052:            /** The driver to create connections with */
053:            protected static com.mysql.jdbc.Driver mysqlDriver = null;
054:
055:            static {
056:                try {
057:                    mysqlDriver = (com.mysql.jdbc.Driver) Class.forName(
058:                            "com.mysql.jdbc.Driver").newInstance();
059:                } catch (Exception E) {
060:                    throw new RuntimeException(
061:                            "Can not load Driver class com.mysql.jdbc.Driver");
062:                }
063:            }
064:
065:            /** Log stream */
066:            protected PrintWriter logWriter = null;
067:
068:            /** Database Name */
069:            protected String databaseName = null;
070:
071:            /** Character Encoding */
072:            protected String encoding = null;
073:
074:            /** Hostname */
075:            protected String hostName = null;
076:
077:            /** Password */
078:            protected String password = null;
079:
080:            /** The profileSql property */
081:            protected String profileSql = "false";
082:
083:            /** The JDBC URL */
084:            protected String url = null;
085:
086:            /** User name */
087:            protected String user = null;
088:
089:            /** Should we construct the URL, or has it been set explicitly */
090:            protected boolean explicitUrl = false;
091:
092:            /** Port number */
093:            protected int port = 3306;
094:
095:            /**
096:             * Default no-arg constructor for Serialization
097:             */
098:            public MysqlDataSource() {
099:            }
100:
101:            /**
102:             * Creates a new connection using the already configured username and
103:             * password.
104:             * 
105:             * @return a connection to the database
106:             * 
107:             * @throws SQLException
108:             *             if an error occurs
109:             */
110:            public java.sql.Connection getConnection() throws SQLException {
111:                return getConnection(this .user, this .password);
112:            }
113:
114:            /**
115:             * Creates a new connection with the given username and password
116:             * 
117:             * @param userID
118:             *            the user id to connect with
119:             * @param password
120:             *            the password to connect with
121:             * 
122:             * @return a connection to the database
123:             * 
124:             * @throws SQLException
125:             *             if an error occurs
126:             */
127:            public java.sql.Connection getConnection(String userID, String pass)
128:                    throws SQLException {
129:                Properties props = new Properties();
130:
131:                if (userID != null) {
132:                    props.setProperty(NonRegisteringDriver.USER_PROPERTY_KEY,
133:                            userID);
134:                }
135:
136:                if (pass != null) {
137:                    props.setProperty(
138:                            NonRegisteringDriver.PASSWORD_PROPERTY_KEY, pass);
139:                }
140:
141:                exposeAsProperties(props);
142:
143:                return getConnection(props);
144:            }
145:
146:            /**
147:             * Sets the database name.
148:             * 
149:             * @param dbName
150:             *            the name of the database
151:             */
152:            public void setDatabaseName(String dbName) {
153:                this .databaseName = dbName;
154:            }
155:
156:            /**
157:             * Gets the name of the database
158:             * 
159:             * @return the name of the database for this data source
160:             */
161:            public String getDatabaseName() {
162:                return (this .databaseName != null) ? this .databaseName : "";
163:            }
164:
165:            /**
166:             * Sets the log writer for this data source.
167:             * 
168:             * @see javax.sql.DataSource#setLogWriter(PrintWriter)
169:             */
170:            public void setLogWriter(PrintWriter output) throws SQLException {
171:                this .logWriter = output;
172:            }
173:
174:            /**
175:             * Returns the log writer for this data source
176:             * 
177:             * @return the log writer for this data source
178:             */
179:            public java.io.PrintWriter getLogWriter() {
180:                return this .logWriter;
181:            }
182:
183:            /**
184:             * DOCUMENT ME!
185:             * 
186:             * @param seconds
187:             *            DOCUMENT ME!
188:             * 
189:             * @throws SQLException
190:             *             DOCUMENT ME!
191:             */
192:            public void setLoginTimeout(int seconds) throws SQLException {
193:            }
194:
195:            /**
196:             * Returns the login timeout
197:             * 
198:             * @return the login timeout
199:             */
200:            public int getLoginTimeout() {
201:                return 0;
202:            }
203:
204:            /**
205:             * Sets the password
206:             * 
207:             * @param pass
208:             *            the password
209:             */
210:            public void setPassword(String pass) {
211:                this .password = pass;
212:            }
213:
214:            /**
215:             * Sets the database port.
216:             * 
217:             * @param p
218:             *            the port
219:             */
220:            public void setPort(int p) {
221:                this .port = p;
222:            }
223:
224:            /**
225:             * Returns the port number
226:             * 
227:             * @return the port number
228:             */
229:            public int getPort() {
230:                return this .port;
231:            }
232:
233:            /**
234:             * Sets the port number
235:             * 
236:             * @param p
237:             *            the port
238:             * 
239:             * @see #setPort
240:             */
241:            public void setPortNumber(int p) {
242:                setPort(p);
243:            }
244:
245:            /**
246:             * Returns the port number
247:             * 
248:             * @return the port number
249:             */
250:            public int getPortNumber() {
251:                return getPort();
252:            }
253:
254:            /**
255:             * DOCUMENT ME!
256:             * 
257:             * @param ref
258:             *            DOCUMENT ME!
259:             * 
260:             * @throws SQLException
261:             *             DOCUMENT ME!
262:             */
263:            public void setPropertiesViaRef(Reference ref) throws SQLException {
264:                super .initializeFromRef(ref);
265:            }
266:
267:            /**
268:             * Required method to support this class as a <CODE>Referenceable</CODE>.
269:             * 
270:             * @return a Reference to this data source
271:             * 
272:             * @throws NamingException
273:             *             if a JNDI error occurs
274:             */
275:            public Reference getReference() throws NamingException {
276:                String factoryName = "com.mysql.jdbc.jdbc2.optional.MysqlDataSourceFactory";
277:                Reference ref = new Reference(getClass().getName(),
278:                        factoryName, null);
279:                ref.add(new StringRefAddr(
280:                        NonRegisteringDriver.USER_PROPERTY_KEY, getUser()));
281:                ref.add(new StringRefAddr(
282:                        NonRegisteringDriver.PASSWORD_PROPERTY_KEY,
283:                        this .password));
284:                ref.add(new StringRefAddr("serverName", getServerName()));
285:                ref.add(new StringRefAddr("port", "" + getPort()));
286:                ref.add(new StringRefAddr("databaseName", getDatabaseName()));
287:                ref.add(new StringRefAddr("url", getUrl()));
288:                ref.add(new StringRefAddr("explicitUrl", String
289:                        .valueOf(this .explicitUrl)));
290:
291:                //
292:                // Now store all of the 'non-standard' properties...
293:                //
294:                try {
295:                    storeToRef(ref);
296:                } catch (SQLException sqlEx) {
297:                    throw new NamingException(sqlEx.getMessage());
298:                }
299:
300:                return ref;
301:            }
302:
303:            /**
304:             * Sets the server name.
305:             * 
306:             * @param serverName
307:             *            the server name
308:             */
309:            public void setServerName(String serverName) {
310:                this .hostName = serverName;
311:            }
312:
313:            /**
314:             * Returns the name of the database server
315:             * 
316:             * @return the name of the database server
317:             */
318:            public String getServerName() {
319:                return (this .hostName != null) ? this .hostName : "";
320:            }
321:
322:            //
323:            // I've seen application servers use both formats
324:            // URL or url (doh)
325:            //
326:
327:            /**
328:             * Sets the URL for this connection
329:             * 
330:             * @param url
331:             *            the URL for this connection
332:             */
333:            public void setURL(String url) {
334:                setUrl(url);
335:            }
336:
337:            /**
338:             * Returns the URL for this connection
339:             * 
340:             * @return the URL for this connection
341:             */
342:            public String getURL() {
343:                return getUrl();
344:            }
345:
346:            /**
347:             * This method is used by the app server to set the url string specified
348:             * within the datasource deployment descriptor. It is discovered using
349:             * introspection and matches if property name in descriptor is "url".
350:             * 
351:             * @param url
352:             *            url to be used within driver.connect
353:             */
354:            public void setUrl(String url) {
355:                this .url = url;
356:                this .explicitUrl = true;
357:            }
358:
359:            /**
360:             * Returns the JDBC URL that will be used to create the database connection.
361:             * 
362:             * @return the URL for this connection
363:             */
364:            public String getUrl() {
365:                if (!this .explicitUrl) {
366:                    String builtUrl = "jdbc:mysql://";
367:                    builtUrl = builtUrl + getServerName() + ":" + getPort()
368:                            + "/" + getDatabaseName();
369:
370:                    return builtUrl;
371:                }
372:
373:                return this .url;
374:            }
375:
376:            /**
377:             * Sets the user ID.
378:             * 
379:             * @param userID
380:             *            the User ID
381:             */
382:            public void setUser(String userID) {
383:                this .user = userID;
384:            }
385:
386:            /**
387:             * Returns the configured user for this connection
388:             * 
389:             * @return the user for this connection
390:             */
391:            public String getUser() {
392:                return this .user;
393:            }
394:
395:            /**
396:             * Creates a connection using the specified properties.
397:             * 
398:             * @param props
399:             *            the properties to connect with
400:             * 
401:             * @return a connection to the database
402:             * 
403:             * @throws SQLException
404:             *             if an error occurs
405:             */
406:            protected java.sql.Connection getConnection(Properties props)
407:                    throws SQLException {
408:                String jdbcUrlToUse = null;
409:
410:                if (!this .explicitUrl) {
411:                    StringBuffer jdbcUrl = new StringBuffer("jdbc:mysql://");
412:
413:                    if (this .hostName != null) {
414:                        jdbcUrl.append(this .hostName);
415:                    }
416:
417:                    jdbcUrl.append(":");
418:                    jdbcUrl.append(this .port);
419:                    jdbcUrl.append("/");
420:
421:                    if (this .databaseName != null) {
422:                        jdbcUrl.append(this .databaseName);
423:                    }
424:
425:                    jdbcUrlToUse = jdbcUrl.toString();
426:                } else {
427:                    jdbcUrlToUse = this .url;
428:                }
429:
430:                return mysqlDriver.connect(jdbcUrlToUse, props);
431:            }
432:            //
433:            //	public boolean isWrapperFor(Class<?> iface) throws SQLException {
434:            //		throw new NotYetImplementedException();
435:            //	}
436:            //
437:            //	public <T> T unwrap(Class<T> iface) throws SQLException {
438:            //		throw new NotYetImplementedException();
439:            //	}
440:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.