# HG changeset patch # User František Kučera # Date 1431863500 -7200 # Node ID 629c9c7eab01279c89006e6419013f57f6433b7b # Parent 5a18a6adf7f9b32022dd25b9a81f7594fc7ef556 JDBC driver class can be specified in the database configuration as an optional parameter (useful especially while embedding jdbc-dk-driver into other application that does not support automatic driver discovery) diff -r 5a18a6adf7f9 -r 629c9c7eab01 java/sql-dk/src/info/globalcode/sql/dk/configuration/DatabaseDefinition.java --- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/DatabaseDefinition.java Sun May 17 13:15:21 2015 +0200 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/DatabaseDefinition.java Sun May 17 13:51:40 2015 +0200 @@ -32,10 +32,30 @@ public class DatabaseDefinition implements NameIdentified { private static final Logger log = Logger.getLogger(DatabaseDefinition.class.getName()); + /** + * database name in SQL-DK configuration + */ private String name; + /** + * JDBC URL + */ private String url; + /** + * JDBC user name + */ private String userName; + /** + * JDBC password + */ private String password; + /** + * optional JDBC driver – if empty, the DriverManager is used to lookup specific Driver for + * given URL + */ + private String driver; + /** + * JDBC properties + */ private Properties properties = new Properties(); @XmlElement(name = "name", namespace = CONFIGURATION) @@ -75,6 +95,14 @@ this.password = password; } + public String getDriver() { + return driver; + } + + public void setDriver(String driver) { + this.driver = driver; + } + @XmlElement(name = "property", namespace = CONFIGURATION) public Properties getProperties() { return properties; @@ -87,7 +115,7 @@ /** * @param properties ad-hoc properties from CLI options (for the JDBC driver) * @param jmxBean JMX management bean for progress reporting | null = disable JMX - * @return + * @return * @throws java.sql.SQLException */ public DatabaseConnection connect(Properties properties, ConnectionManagement jmxBean) throws SQLException { @@ -96,7 +124,7 @@ /** * @param properties - * @return + * @return * @throws java.sql.SQLException * @see #connect(info.globalcode.sql.dk.configuration.Properties, java.lang.String) * With disabled JMX reporting. diff -r 5a18a6adf7f9 -r 629c9c7eab01 java/sql-dk/src/info/globalcode/sql/dk/configuration/Loader.java --- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/Loader.java Sun May 17 13:15:21 2015 +0200 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/Loader.java Sun May 17 13:51:40 2015 +0200 @@ -21,6 +21,7 @@ import static info.globalcode.sql.dk.DatabaseConnection.JDBC_PROPERTY_USER; import static info.globalcode.sql.dk.DatabaseConnection.JDBC_PROPERTY_PASSWORD; import java.sql.Connection; +import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; import java.util.logging.Level; @@ -66,7 +67,22 @@ credentials.setDefaults(databaseDefinition.getProperties()); properties.setDefaults(credentials); java.util.Properties javaProperties = properties.getJavaProperties(); - return DriverManager.getConnection(databaseDefinition.getUrl(), javaProperties); + + String driverClassName = databaseDefinition.getDriver(); + final String url = databaseDefinition.getUrl(); + if (driverClassName == null) { + log.log(Level.FINE, "Using DriverManager to create connection for „{0}“", url); + return DriverManager.getConnection(url, javaProperties); + } else { + log.log(Level.FINE, "Using custom Driver „{0}“ to create connection for „{1}“", new Object[]{driverClassName, url}); + try { + Class driverClass = (Class) Class.forName(driverClassName); + Driver driver = driverClass.newInstance(); + return driver.connect(url, javaProperties); + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) { + throw new SQLException("Unable to connect usig specific driver: " + driverClassName, e); + } + } } }