1 /**
2 * Logback: the reliable, generic, fast and flexible logging framework.
3 * Copyright (C) 1999-2011, QOS.ch. All rights reserved.
4 *
5 * This program and the accompanying materials are dual-licensed under
6 * either the terms of the Eclipse Public License v1.0 as published by
7 * the Eclipse Foundation
8 *
9 * or (per the licensee's choosing)
10 *
11 * under the terms of the GNU Lesser General Public License version 2.1
12 * as published by the Free Software Foundation.
13 */
14 package ch.qos.logback.core.db;
15
16 import java.sql.Connection;
17 import java.sql.DriverManager;
18 import java.sql.SQLException;
19
20 /**
21 * The DriverManagerConnectionSource is an implementation of
22 * {@link ConnectionSource} that obtains the Connection in the traditional JDBC
23 * manner based on the connection URL.
24 * <p>
25 * For more information about this component, please refer to the online manual at
26 * http://logback.qos.ch/manual/appenders.html#DBAppender
27 *
28 * @author <a href="mailto:rdecampo@twcny.rr.com">Ray DeCampo</a>
29 */
30 public class DriverManagerConnectionSource extends ConnectionSourceBase {
31 private String driverClass = null;
32 private String url = null;
33
34 public void start() {
35 try {
36 if (driverClass != null) {
37 Class.forName(driverClass);
38 discoverConnnectionProperties();
39 } else {
40 addError("WARNING: No JDBC driver specified for logback DriverManagerConnectionSource.");
41 }
42 } catch (final ClassNotFoundException cnfe) {
43 addError("Could not load JDBC driver class: " + driverClass, cnfe);
44 }
45 }
46
47 /**
48 * @see ch.qos.logback.classic.db.ConnectionSource#getConnection()
49 */
50 public Connection getConnection() throws SQLException {
51 if (getUser() == null) {
52 return DriverManager.getConnection(url);
53 } else {
54 return DriverManager.getConnection(url, getUser(), getPassword());
55 }
56 }
57
58 /**
59 * Returns the url.
60 *
61 * @return String
62 */
63 public String getUrl() {
64 return url;
65 }
66
67 /**
68 * Sets the url.
69 *
70 * @param url
71 * The url to set
72 */
73 public void setUrl(String url) {
74 this.url = url;
75 }
76
77 /**
78 * Returns the name of the driver class.
79 *
80 * @return String
81 */
82 public String getDriverClass() {
83 return driverClass;
84 }
85
86 /**
87 * Sets the driver class.
88 *
89 * @param driverClass
90 * The driver class to set
91 */
92 public void setDriverClass(String driverClass) {
93 this.driverClass = driverClass;
94 }
95 }