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) v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 17 May 2015 13:51:40 +0200
branchv_0
changeset 194629c9c7eab01
parent 193 5a18a6adf7f9
child 195 aed11d9107bf
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)
java/sql-dk/src/info/globalcode/sql/dk/configuration/DatabaseDefinition.java
java/sql-dk/src/info/globalcode/sql/dk/configuration/Loader.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/DatabaseDefinition.java	Sun May 17 13:15:21 2015 +0200
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/DatabaseDefinition.java	Sun May 17 13:51:40 2015 +0200
     1.3 @@ -32,10 +32,30 @@
     1.4  public class DatabaseDefinition implements NameIdentified {
     1.5  
     1.6  	private static final Logger log = Logger.getLogger(DatabaseDefinition.class.getName());
     1.7 +	/**
     1.8 +	 * database name in SQL-DK configuration
     1.9 +	 */
    1.10  	private String name;
    1.11 +	/**
    1.12 +	 * JDBC URL
    1.13 +	 */
    1.14  	private String url;
    1.15 +	/**
    1.16 +	 * JDBC user name
    1.17 +	 */
    1.18  	private String userName;
    1.19 +	/**
    1.20 +	 * JDBC password
    1.21 +	 */
    1.22  	private String password;
    1.23 +	/**
    1.24 +	 * optional JDBC driver – if empty, the DriverManager is used to lookup specific Driver for
    1.25 +	 * given URL
    1.26 +	 */
    1.27 +	private String driver;
    1.28 +	/**
    1.29 +	 * JDBC properties
    1.30 +	 */
    1.31  	private Properties properties = new Properties();
    1.32  
    1.33  	@XmlElement(name = "name", namespace = CONFIGURATION)
    1.34 @@ -75,6 +95,14 @@
    1.35  		this.password = password;
    1.36  	}
    1.37  
    1.38 +	public String getDriver() {
    1.39 +		return driver;
    1.40 +	}
    1.41 +
    1.42 +	public void setDriver(String driver) {
    1.43 +		this.driver = driver;
    1.44 +	}
    1.45 +
    1.46  	@XmlElement(name = "property", namespace = CONFIGURATION)
    1.47  	public Properties getProperties() {
    1.48  		return properties;
    1.49 @@ -87,7 +115,7 @@
    1.50  	/**
    1.51  	 * @param properties ad-hoc properties from CLI options (for the JDBC driver)
    1.52  	 * @param jmxBean JMX management bean for progress reporting | null = disable JMX
    1.53 -	 * @return 
    1.54 +	 * @return
    1.55  	 * @throws java.sql.SQLException
    1.56  	 */
    1.57  	public DatabaseConnection connect(Properties properties, ConnectionManagement jmxBean) throws SQLException {
    1.58 @@ -96,7 +124,7 @@
    1.59  
    1.60  	/**
    1.61  	 * @param properties
    1.62 -	 * @return 
    1.63 +	 * @return
    1.64  	 * @throws java.sql.SQLException
    1.65  	 * @see #connect(info.globalcode.sql.dk.configuration.Properties, java.lang.String)
    1.66  	 * With disabled JMX reporting.
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/Loader.java	Sun May 17 13:15:21 2015 +0200
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/Loader.java	Sun May 17 13:51:40 2015 +0200
     2.3 @@ -21,6 +21,7 @@
     2.4  import static info.globalcode.sql.dk.DatabaseConnection.JDBC_PROPERTY_USER;
     2.5  import static info.globalcode.sql.dk.DatabaseConnection.JDBC_PROPERTY_PASSWORD;
     2.6  import java.sql.Connection;
     2.7 +import java.sql.Driver;
     2.8  import java.sql.DriverManager;
     2.9  import java.sql.SQLException;
    2.10  import java.util.logging.Level;
    2.11 @@ -66,7 +67,22 @@
    2.12  		credentials.setDefaults(databaseDefinition.getProperties());
    2.13  		properties.setDefaults(credentials);
    2.14  		java.util.Properties javaProperties = properties.getJavaProperties();
    2.15 -		return DriverManager.getConnection(databaseDefinition.getUrl(), javaProperties);
    2.16 +
    2.17 +		String driverClassName = databaseDefinition.getDriver();
    2.18 +		final String url = databaseDefinition.getUrl();
    2.19 +		if (driverClassName == null) {
    2.20 +			log.log(Level.FINE, "Using DriverManager to create connection for „{0}“", url);
    2.21 +			return DriverManager.getConnection(url, javaProperties);
    2.22 +		} else {
    2.23 +			log.log(Level.FINE, "Using custom Driver „{0}“ to create connection for „{1}“", new Object[]{driverClassName, url});
    2.24 +			try {
    2.25 +				Class<Driver> driverClass = (Class<Driver>) Class.forName(driverClassName);
    2.26 +				Driver driver = driverClass.newInstance();
    2.27 +				return driver.connect(url, javaProperties);
    2.28 +			} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) {
    2.29 +				throw new SQLException("Unable to connect usig specific driver: " + driverClassName, e);
    2.30 +			}
    2.31 +		}
    2.32  	}
    2.33  
    2.34  }