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 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
public final class DriverManagerWrapper { |
17 |
|
|
18 |
0 |
private DriverManagerWrapper() { |
19 |
0 |
} |
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
public static Connection getConnection(JDBCConnectConfigure configure) { |
29 |
0 |
Connection dbConnection = null; |
30 |
0 |
if (configure.getUser() != null && configure.getPassword() != class="keyword">null) { |
31 |
|
try { |
32 |
0 |
dbConnection = DriverManager.getConnection(configure.getUrl(), |
33 |
|
configure.getUser(), configure.getPassword()); |
34 |
0 |
} catch (SQLException e) { |
35 |
0 |
throw SQLExceptionWrapperFactory.create(e); |
36 |
0 |
} |
37 |
0 |
} else if (configure.getUser() == null |
38 |
|
&& configure.getPassword() == null) { |
39 |
|
try { |
40 |
0 |
dbConnection = DriverManager.getConnection(configure.getUrl()); |
41 |
0 |
} catch (SQLException e) { |
42 |
0 |
throw SQLExceptionWrapperFactory.create(e); |
43 |
0 |
} |
44 |
|
} else { |
45 |
0 |
throw new RuntimeException("user password config"); |
46 |
|
} |
47 |
0 |
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 |
|
} |