View Javadoc
1   package com.github.davidmoten.rx.jdbc;
2   
3   import java.sql.Connection;
4   import java.sql.DriverManager;
5   import java.sql.SQLException;
6   
7   import com.github.davidmoten.rx.jdbc.exceptions.SQLRuntimeException;
8   
9   /**
10   * Provides {@link Connection}s from a url (using
11   * DriverManager.getConnection()).
12   */
13  public final class ConnectionProviderFromUrl implements ConnectionProvider {
14  
15      /**
16       * JDBC url
17       */
18      private final String url;
19  
20      private final String username;
21  
22      private final String password;
23  
24      /**
25       * Constructor.
26       * 
27       * @param url
28       *            the jdbc url
29       */
30      public ConnectionProviderFromUrl(String url) {
31          this(url, null, null);
32      }
33  
34      /**
35       * Constructor.
36       * 
37       * @param url jdbc url
38       * @param username login username
39       * @param password login password
40       */
41      public ConnectionProviderFromUrl(String url, String username, String password) {
42          this.url = url;
43          this.username = username;
44          this.password = password;
45      }
46  
47      @Override
48      public Connection get() {
49          try {
50              if (username != null || password != null)
51                  return DriverManager.getConnection(url, username, password);
52              else
53                  return DriverManager.getConnection(url);
54          } catch (SQLException e) {
55              throw new SQLRuntimeException(e);
56          }
57      }
58  
59      @Override
60      public void close() {
61          // nothing to do
62      }
63  }