1 3 package jodd.db.connection; 4 5 import jodd.db.DbSqlException; 6 7 import java.sql.Connection ; 8 import java.sql.DriverManager ; 9 import java.sql.SQLException ; 10 11 14 public class DriverManagerConnectionProvider implements ConnectionProvider { 15 16 18 private String url; 19 private String username; 20 private String password; 21 private String driverClass; 22 private Integer isolation; 23 private Boolean autoCommit; 24 25 26 public String getUrl() { 27 return url; 28 } 29 30 public void setUrl(String url) { 31 this.url = url; 32 } 33 34 public String getUsername() { 35 return username; 36 } 37 38 public void setUsername(String username) { 39 this.username = username; 40 } 41 42 public String getPassword() { 43 return password; 44 } 45 46 public void setPassword(String password) { 47 this.password = password; 48 } 49 50 public String getDriverClass() { 51 return driverClass; 52 } 53 54 public void setDriverClass(String driverClass) { 55 this.driverClass = driverClass; 56 } 57 58 public Integer getIsolation() { 59 return isolation; 60 } 61 62 public void setIsolation(Integer isolation) { 63 this.isolation = isolation; 64 } 65 66 public Boolean getAutoCommit() { 67 return autoCommit; 68 } 69 70 public void setAutoCommit(Boolean autoCommit) { 71 this.autoCommit = autoCommit; 72 } 73 74 76 public DriverManagerConnectionProvider(String driverClass, String url, String username, String password) { 77 this.driverClass = driverClass; 78 this.password = password; 79 this.username = username; 80 this.url = url; 81 } 82 public DriverManagerConnectionProvider(String driverClass, String url) { 83 this.driverClass = driverClass; 84 this.url = url; 85 } 86 87 public DriverManagerConnectionProvider() { 88 89 } 90 91 93 94 public void init() { 95 try { 96 Class.forName(driverClass); 97 } catch (ClassNotFoundException cnfex) { 98 throw new DbSqlException("Unable to find JDBC driver class: '" + driverClass + '\'', cnfex); 99 } 100 } 101 102 public Connection getConnection() { 103 Connection 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 sex) { 118 throw new DbSqlException("Unable to get connection.", sex); 119 } 120 return conn; 121 } 122 123 public void closeConnection(Connection conn) { 124 try { 125 conn.close(); 126 } catch (SQLException sex) { 127 } 129 } 130 131 public void close() { 132 } 133 } | Popular Tags |