1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.dbcp;
19 import java.sql.Connection;
20 import java.sql.DriverManager;
21 import java.sql.SQLException;
22 import java.util.Properties;
23
24 /**
25 * A {@link DriverManager}-based implementation of {@link ConnectionFactory}.
26 *
27 * @author Rodney Waldhoff
28 * @author Ignacio J. Ortega
29 * @author Dirk Verbeeck
30 * @version $Revision: 479137 $ $Date: 2006-11-25 08:51:48 -0700 (Sat, 25 Nov 2006) $
31 */
32 public class DriverManagerConnectionFactory implements ConnectionFactory {
33
34 /**
35 * Constructor for DriverManagerConnectionFactory.
36 * @param connectUri a database url of the form
37 * <code> jdbc:<em>subprotocol</em>:<em>subname</em></code>
38 * @param props a list of arbitrary string tag/value pairs as
39 * connection arguments; normally at least a "user" and "password"
40 * property should be included.
41 */
42 public DriverManagerConnectionFactory(String connectUri, Properties props) {
43 _connectUri = connectUri;
44 _props = props;
45 }
46
47 /**
48 * Constructor for DriverManagerConnectionFactory.
49 * @param connectUri a database url of the form
50 * <code>jdbc:<em>subprotocol</em>:<em>subname</em></code>
51 * @param uname the database user
52 * @param passwd the user's password
53 */
54 public DriverManagerConnectionFactory(String connectUri, String uname, String passwd) {
55 _connectUri = connectUri;
56 _uname = uname;
57 _passwd = passwd;
58 }
59
60 public Connection createConnection() throws SQLException {
61 if(null == _props) {
62 if((_uname == null) && (_passwd == null)) {
63 return DriverManager.getConnection(_connectUri);
64 } else {
65 return DriverManager.getConnection(_connectUri,_uname,_passwd);
66 }
67 } else {
68 return DriverManager.getConnection(_connectUri,_props);
69 }
70 }
71
72 protected String _connectUri = null;
73 protected String _uname = null;
74 protected String _passwd = null;
75 protected Properties _props = null;
76 }