View Javadoc

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