1 16 17 package org.springframework.jdbc.datasource; 18 19 import java.sql.Connection ; 20 import java.sql.DriverManager ; 21 import java.sql.SQLException ; 22 import java.util.Properties ; 23 24 import org.springframework.jdbc.CannotGetJdbcConnectionException; 25 import org.springframework.util.ClassUtils; 26 import org.springframework.util.StringUtils; 27 28 71 public class DriverManagerDataSource extends AbstractDataSource { 72 73 private String driverClassName; 74 75 private String url; 76 77 private String username; 78 79 private String password; 80 81 private Properties connectionProperties; 82 83 84 87 public DriverManagerDataSource() { 88 } 89 90 99 public DriverManagerDataSource(String driverClassName, String url, String username, String password) 100 throws CannotGetJdbcConnectionException { 101 setDriverClassName(driverClassName); 102 setUrl(url); 103 setUsername(username); 104 setPassword(password); 105 } 106 107 115 public DriverManagerDataSource(String url, String username, String password) 116 throws CannotGetJdbcConnectionException { 117 setUrl(url); 118 setUsername(username); 119 setPassword(password); 120 } 121 122 128 public DriverManagerDataSource(String url) 129 throws CannotGetJdbcConnectionException { 130 setUrl(url); 131 } 132 133 134 142 public void setDriverClassName(String driverClassName) throws CannotGetJdbcConnectionException { 143 if (!StringUtils.hasText(driverClassName)) { 144 throw new IllegalArgumentException ("driverClassName must not be empty"); 145 } 146 this.driverClassName = driverClassName.trim(); 147 try { 148 Class.forName(this.driverClassName, true, ClassUtils.getDefaultClassLoader()); 149 } 150 catch (ClassNotFoundException ex) { 151 throw new CannotGetJdbcConnectionException( 152 "Could not load JDBC driver class [" + this.driverClassName + "]", ex); 153 } 154 if (logger.isInfoEnabled()) { 155 logger.info("Loaded JDBC driver: " + this.driverClassName); 156 } 157 } 158 159 162 public String getDriverClassName() { 163 return driverClassName; 164 } 165 166 170 public void setUrl(String url) { 171 if (!StringUtils.hasText(url)) { 172 throw new IllegalArgumentException ("url must not be empty"); 173 } 174 this.url = url.trim(); 175 } 176 177 180 public String getUrl() { 181 return url; 182 } 183 184 188 public void setUsername(String username) { 189 this.username = username; 190 } 191 192 195 public String getUsername() { 196 return username; 197 } 198 199 203 public void setPassword(String password) { 204 this.password = password; 205 } 206 207 210 public String getPassword() { 211 return password; 212 } 213 214 222 public void setConnectionProperties(Properties connectionProperties) { 223 this.connectionProperties = connectionProperties; 224 } 225 226 229 public Properties getConnectionProperties() { 230 return connectionProperties; 231 } 232 233 234 239 public Connection getConnection() throws SQLException { 240 return getConnectionFromDriverManager(); 241 } 242 243 248 public Connection getConnection(String username, String password) throws SQLException { 249 return getConnectionFromDriverManager(username, password); 250 } 251 252 253 258 protected Connection getConnectionFromDriverManager() throws SQLException { 259 return getConnectionFromDriverManager(getUsername(), getPassword()); 260 } 261 262 267 protected Connection getConnectionFromDriverManager(String username, String password) 268 throws SQLException { 269 270 Properties props = new Properties (getConnectionProperties()); 271 if (username != null) { 272 props.setProperty("user", username); 273 } 274 if (password != null) { 275 props.setProperty("password", password); 276 } 277 return getConnectionFromDriverManager(getUrl(), props); 278 } 279 280 285 protected Connection getConnectionFromDriverManager(String url, Properties props) 286 throws SQLException { 287 288 if (logger.isDebugEnabled()) { 289 logger.debug("Creating new JDBC Connection to [" + url + "]"); 290 } 291 return DriverManager.getConnection(url, props); 292 } 293 294 } 295 | Popular Tags |