View Javadoc

1   package com.wikihouse.wildcats0201.jdbcchart.jdbcutil;
2   
3   import java.sql.Connection;
4   import java.sql.DriverManager;
5   import java.sql.SQLException;
6   
7   import com.wikihouse.wildcats0201.jdbcchart.jdbcutil.exception.SQLExceptionWrapper;
8   
9   /***
10   * <B>DriverManagerWrapper </B>. DriverManagerをラッピングしたクラスです。
11   * 
12   * @see java.sql.DriverManager
13   * @author $Author: wildcats $
14   * @version $Revision: 5 $
15   */
16  public final class DriverManagerWrapper {
17  
18      private DriverManagerWrapper() {
19      }
20  
21      /***
22       * コネクション取得
23       * 
24       * @param configure
25       *            JDBCConnectConfigure
26       * @return Connection
27       */
28      public static Connection getConnection(JDBCConnectConfigure configure) {
29          Connection dbConnection = null;
30          if (configure.getUser() != null && configure.getPassword() != null) {
31              try {
32                  dbConnection = DriverManager.getConnection(configure.getUrl(),
33                          configure.getUser(), configure.getPassword());
34              } catch (SQLException e) {
35                  throw SQLExceptionWrapperFactory.create(e);
36              }
37          } else if (configure.getUser() == null
38                  && configure.getPassword() == null) {
39              try {
40                  dbConnection = DriverManager.getConnection(configure.getUrl());
41              } catch (SQLException e) {
42                  throw SQLExceptionWrapperFactory.create(e);
43              }
44          } else {
45              throw new RuntimeException("user password config");
46          }
47          return dbConnection;
48      }
49  
50      private static class SQLExceptionWrapperFactory {
51          private SQLExceptionWrapperFactory() {
52          }
53  
54          public static SQLExceptionWrapper create(SQLException e) {
55              return new SQLExceptionWrapper(e);
56          }
57      }
58  
59  }