1 12 package org.openbravo.database; 13 14 import java.sql.*; 15 import java.util.Properties ; 16 import org.apache.commons.dbcp.*; 17 18 public class OpenBravoDriverManagerConnectionFactory implements ConnectionFactory { 19 protected String _connectUri; 20 protected String _uname; 21 protected String _passwd; 22 protected Properties _props; 23 protected String _dbSessionConfig; 24 25 public OpenBravoDriverManagerConnectionFactory(String connectUri, Properties props) { 26 _connectUri = null; 27 _uname = null; 28 _passwd = null; 29 _props = null; 30 _connectUri = connectUri; 31 _props = props; 32 } 33 34 public OpenBravoDriverManagerConnectionFactory(String connectUri, String uname, String passwd, String dbSessionConfig) { 35 _connectUri = null; 36 _uname = null; 37 _passwd = null; 38 _props = null; 39 _dbSessionConfig = null; 40 _connectUri = connectUri; 41 _uname = uname; 42 _passwd = passwd; 43 _dbSessionConfig = dbSessionConfig; 44 } 45 46 public Connection createConnection() throws SQLException { 47 Connection conn = null; 48 if(null == _props) { 49 if(_uname == null) 50 conn = DriverManager.getConnection(_connectUri); 51 else 52 conn = DriverManager.getConnection(_connectUri, _uname, _passwd); 53 } else { 54 conn = DriverManager.getConnection(_connectUri, _props); 55 } 56 if (conn!=null && _dbSessionConfig!=null) executeDefaultSQL(conn); 57 return conn; 58 } 59 60 private void executeDefaultSQL(Connection conn) { 61 Statement stmt = null; 62 ResultSet rset = null; 63 64 try { 65 stmt = conn.createStatement(); 66 rset = stmt.executeQuery(_dbSessionConfig); 67 } catch(SQLException e) { 68 e.printStackTrace(); 69 } finally { 70 try { rset.close(); } catch(Exception e) { } 71 try { stmt.close(); } catch(Exception e) { } 72 try { conn.commit(); } catch(Exception e) { } 73 } 74 } 75 } 76 | Popular Tags |