1 7 8 package org.jdesktop.dataset.provider.sql; 9 import java.sql.Connection ; 10 import java.sql.DriverManager ; 11 import java.sql.PreparedStatement ; 12 import java.sql.ResultSet ; 13 import java.util.Properties ; 14 import javax.naming.InitialContext ; 15 import org.jdesktop.dataset.DataConnection; 16 17 25 public class JDBCDataConnection extends DataConnection { 26 29 private Connection conn; 30 36 private final Object connMutex = new String ("Connection_Mutex"); 37 41 private String jndiContext; 42 47 private String url; 48 52 private String userName; 53 57 private String password; 58 62 private Properties properties; 63 64 69 public JDBCDataConnection() { 70 } 71 72 73 81 public JDBCDataConnection(String driver, String url, String user, 82 String passwd) { 83 try { 84 Class.forName(driver); 85 } catch (Exception e) { 86 System.err.println("WARN: The driver passed to the " + 87 "JDBCDataConnection constructor could not be loaded. " + 88 "This may be due to the driver not being on the classpath"); 89 e.printStackTrace(); 90 } 91 this.setUrl(url); 92 this.setUserName(user); 93 this.setPassword(passwd); 94 } 95 96 103 public JDBCDataConnection(String driver, String url, Properties props) { 104 try { 105 Class.forName(driver); 106 } catch (Exception e) { 107 System.err.println("WARN: The driver passed to the " + 108 "JDBCDataConnection constructor could not be loaded. " + 109 "This may be due to the driver not being on the classpath"); 110 e.printStackTrace(); 111 } 112 this.setUrl(url); 113 this.setProperties(props); 114 } 115 116 123 public JDBCDataConnection(String jndiContext, String user, String passwd) { 124 this.jndiContext = jndiContext; 125 this.setUserName(user); 126 this.setPassword(passwd); 127 } 128 129 130 133 public String getUrl() { 134 return url; 135 } 136 137 140 public void setUrl(String url) { 141 this.url = url; 142 } 143 144 147 public String getUserName() { 148 return userName; 149 } 150 151 154 public void setUserName(String userName) { 155 this.userName = userName; 156 } 157 158 161 public String getPassword() { 162 return password; 163 } 164 165 168 public void setPassword(String password) { 169 this.password = password; 170 } 171 172 175 public Properties getProperties() { 176 return properties; 177 } 178 179 183 public void setProperties(Properties properties) { 184 this.properties = properties; 185 } 186 187 192 protected void connect() throws Exception { 193 if (jndiContext != null) { 196 try { 197 connectByJNDI(); 198 } catch (Exception e) { 199 try { 200 connectByDriverManager(); 201 } catch (Exception ex) { 202 throw new Exception ("Failed to connect to the database", e); 203 } 204 } 205 } else { 206 try { 207 connectByDriverManager(); 208 } catch (Exception ex) { 209 throw new Exception ("Failed to connect to the database", ex); 210 } 211 } 212 } 213 214 219 private void connectByJNDI() throws Exception { 220 InitialContext ctx = new InitialContext (); 221 javax.sql.DataSource ds = (javax.sql.DataSource )ctx.lookup(jndiContext); 222 synchronized(connMutex) { 223 conn = ds.getConnection(getUserName(), getPassword()); 224 conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); 225 } 226 } 227 228 239 private void connectByDriverManager() throws Exception { 240 synchronized(connMutex) { 241 if (getProperties() != null) { 242 try { 243 conn = DriverManager.getConnection(getUrl(), getProperties()); 244 conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); 245 } catch (Exception e) { 246 try { 247 conn = DriverManager.getConnection(getUrl(), getUserName(), getPassword()); 248 conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); 249 } catch (Exception ex) { 250 conn = DriverManager.getConnection(getUrl()); 251 conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); 252 } 253 } 254 } else { 255 try { 256 conn = DriverManager.getConnection(getUrl(), getUserName(), getPassword()); 257 258 } catch (Exception e) { 259 e.printStackTrace(); 260 conn = DriverManager.getConnection(getUrl()); 262 263 } 264 265 } 271 } 272 } 273 274 278 protected void disconnect() throws Exception { 279 synchronized(connMutex) { 280 if (conn != null) { 281 conn.close(); 282 } 283 } 284 } 285 286 public ResultSet executeQuery(PreparedStatement ps) { 287 synchronized(connMutex) { 288 if (conn != null) { 289 try { 290 return ps.executeQuery(); 291 } catch (Exception e) { 292 e.printStackTrace(); 293 } 294 } 295 } 296 return null; 297 } 298 299 public int executeUpdate(PreparedStatement ps) { 300 synchronized(connMutex) { 301 if (conn != null) { 302 try { 303 return ps.executeUpdate(); 304 } catch (Exception e) { 305 e.printStackTrace(); 306 } 307 } 308 } 309 return 0; 310 } 311 312 public PreparedStatement prepareStatement(String sql) throws Exception { 313 synchronized(connMutex) { 314 if (conn != null) { 315 return conn.prepareStatement(sql); 316 } 317 } 318 return null; 319 } 320 } 321
| Popular Tags
|