1 16 package scriptella.jdbc; 17 18 import scriptella.spi.AbstractScriptellaDriver; 19 import scriptella.spi.ConnectionParameters; 20 21 import java.io.PrintWriter ; 22 import java.sql.Connection ; 23 import java.sql.DriverManager ; 24 import java.sql.SQLException ; 25 import java.util.Properties ; 26 import java.util.logging.Level ; 27 import java.util.logging.Logger ; 28 29 35 public class GenericDriver extends AbstractScriptellaDriver { 36 37 private static final Logger LOG = Logger.getLogger(GenericDriver.class.getName()); 38 39 static { 40 final Logger LOG = Logger.getLogger("scriptella.DriverManagerLog"); 42 if (LOG.isLoggable(Level.FINE)) { 43 if (DriverManager.getLogWriter() == null) { 44 DriverManager.setLogWriter(new PrintWriter (System.out) { 45 public void println(String s) { 46 LOG.fine(s); 47 } 48 }); 49 50 } 51 } 52 } 53 54 60 protected void loadDrivers(String ... drivers) { 61 if (drivers.length > 0) { 62 Throwable throwable = null; 63 for (String name : drivers) { 64 if (LOG.isLoggable(Level.FINE)) { 65 LOG.fine("Trying to load driver class " + name); 66 } 67 try { 68 Class.forName(name); 69 break; 70 } catch (Throwable t) { 71 if (throwable == null) { 72 throwable = t; 73 } else { 74 if (LOG.isLoggable(Level.FINE)) { 75 LOG.log(Level.FINE, "Failed to load driver class " + name, t); 76 } 77 } 78 } 79 } 80 if (throwable != null) { 81 throw new JdbcException("Couldn't find appropriate jdbc driver : " + drivers[0] + 82 ". Please check class path settings", throwable); 83 } 84 } 85 } 86 87 public JdbcConnection connect(ConnectionParameters params) { 88 try { 89 Properties props = new Properties (); 90 props.putAll(params.getProperties()); 91 if (params.getUser() != null) { 93 props.put("user", params.getUser()); 94 } 95 if (params.getPassword() != null) { 96 props.put("password", params.getPassword()); 97 } 98 return connect(params, props); 99 100 } catch (SQLException e) { 101 throw new JdbcException("Unable to obtain connection for URL " + params.getUrl(), e); 102 } 103 } 104 105 106 114 protected JdbcConnection connect(ConnectionParameters parameters, Properties props) throws SQLException { 115 return new JdbcConnection(getConnection(parameters.getUrl(), props), parameters); 116 } 117 118 122 protected Connection getConnection(String url, Properties props) throws SQLException { 123 return DriverManager.getConnection(url, props); 124 } 125 126 127 } 128 | Popular Tags |