01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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:
20: import java.sql.Connection;
21: import java.sql.DriverManager;
22: import java.sql.SQLException;
23: import java.util.Properties;
24:
25: /**
26: * A {@link DriverManager}-based implementation of {@link ConnectionFactory}.
27: *
28: * @author Rodney Waldhoff
29: * @author Ignacio J. Ortega
30: * @author Dirk Verbeeck
31: * @version $Revision: 479137 $ $Date: 2006-11-25 08:51:48 -0700 (Sat, 25 Nov 2006) $
32: */
33: public class DriverManagerConnectionFactory implements
34: ConnectionFactory {
35:
36: /**
37: * Constructor for DriverManagerConnectionFactory.
38: * @param connectUri a database url of the form
39: * <code> jdbc:<em>subprotocol</em>:<em>subname</em></code>
40: * @param props a list of arbitrary string tag/value pairs as
41: * connection arguments; normally at least a "user" and "password"
42: * property should be included.
43: */
44: public DriverManagerConnectionFactory(String connectUri,
45: Properties props) {
46: _connectUri = connectUri;
47: _props = props;
48: }
49:
50: /**
51: * Constructor for DriverManagerConnectionFactory.
52: * @param connectUri a database url of the form
53: * <code>jdbc:<em>subprotocol</em>:<em>subname</em></code>
54: * @param uname the database user
55: * @param passwd the user's password
56: */
57: public DriverManagerConnectionFactory(String connectUri,
58: String uname, String passwd) {
59: _connectUri = connectUri;
60: _uname = uname;
61: _passwd = passwd;
62: }
63:
64: public Connection createConnection() throws SQLException {
65: if (null == _props) {
66: if ((_uname == null) && (_passwd == null)) {
67: return DriverManager.getConnection(_connectUri);
68: } else {
69: return DriverManager.getConnection(_connectUri, _uname,
70: _passwd);
71: }
72: } else {
73: return DriverManager.getConnection(_connectUri, _props);
74: }
75: }
76:
77: protected String _connectUri = null;
78: protected String _uname = null;
79: protected String _passwd = null;
80: protected Properties _props = null;
81: }
|