KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > db > connection > DriverManagerConnectionProvider


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.db.connection;
4
5 import jodd.db.DbSqlException;
6
7 import java.sql.Connection JavaDoc;
8 import java.sql.DriverManager JavaDoc;
9 import java.sql.SQLException JavaDoc;
10
11 /**
12  * Driver manager connection provider;
13  */

14 public class DriverManagerConnectionProvider implements ConnectionProvider {
15
16     // ---------------------------------------------------------------- properties
17

18     private String JavaDoc url;
19     private String JavaDoc username;
20     private String JavaDoc password;
21     private String JavaDoc driverClass;
22     private Integer JavaDoc isolation;
23     private Boolean JavaDoc autoCommit;
24
25
26     public String JavaDoc getUrl() {
27         return url;
28     }
29
30     public void setUrl(String JavaDoc url) {
31         this.url = url;
32     }
33
34     public String JavaDoc getUsername() {
35         return username;
36     }
37
38     public void setUsername(String JavaDoc username) {
39         this.username = username;
40     }
41
42     public String JavaDoc getPassword() {
43         return password;
44     }
45
46     public void setPassword(String JavaDoc password) {
47         this.password = password;
48     }
49
50     public String JavaDoc getDriverClass() {
51         return driverClass;
52     }
53
54     public void setDriverClass(String JavaDoc driverClass) {
55         this.driverClass = driverClass;
56     }
57
58     public Integer JavaDoc getIsolation() {
59         return isolation;
60     }
61
62     public void setIsolation(Integer JavaDoc isolation) {
63         this.isolation = isolation;
64     }
65
66     public Boolean JavaDoc getAutoCommit() {
67         return autoCommit;
68     }
69
70     public void setAutoCommit(Boolean JavaDoc autoCommit) {
71         this.autoCommit = autoCommit;
72     }
73
74     // ---------------------------------------------------------------- ctors
75

76     public DriverManagerConnectionProvider(String JavaDoc driverClass, String JavaDoc url, String JavaDoc username, String JavaDoc password) {
77         this.driverClass = driverClass;
78         this.password = password;
79         this.username = username;
80         this.url = url;
81     }
82     public DriverManagerConnectionProvider(String JavaDoc driverClass, String JavaDoc url) {
83         this.driverClass = driverClass;
84         this.url = url;
85     }
86
87     public DriverManagerConnectionProvider() {
88         
89     }
90
91     // ---------------------------------------------------------------- provider
92

93
94     public void init() {
95         try {
96             Class.forName(driverClass);
97         } catch (ClassNotFoundException JavaDoc cnfex) {
98             throw new DbSqlException("Unable to find JDBC driver class: '" + driverClass + '\'', cnfex);
99         }
100     }
101
102     public Connection JavaDoc getConnection() {
103         Connection JavaDoc conn;
104         try {
105             if (username != null) {
106                 conn = DriverManager.getConnection(url, username, password);
107             } else {
108                 conn = DriverManager.getConnection(url);
109             }
110             if (isolation != null) {
111                 conn.setTransactionIsolation(isolation.intValue());
112             }
113             if (autoCommit != null) {
114                 conn.setAutoCommit(autoCommit.booleanValue());
115             }
116         }
117         catch (SQLException JavaDoc sex) {
118             throw new DbSqlException("Unable to get connection.", sex);
119         }
120         return conn;
121     }
122
123     public void closeConnection(Connection JavaDoc conn) {
124         try {
125             conn.close();
126         } catch (SQLException JavaDoc sex) {
127             // ignore
128
}
129     }
130
131     public void close() {
132     }
133 }
Popular Tags