Use DriverManager.getConnection().
This is the preferred way to obtain a JDBC connection in this millenium. No longer does one manually instantiate the JDBC driver by name. The JDBC driver jar file will have a mapping that indicates that it handles a particular type of JDBC URL and the JDBC infrastructure will handle instantiating the driver.
This commit is contained in:
@@ -25,7 +25,6 @@ import static com.samskivert.jdbc.Log.log;
|
|||||||
* <p> The configuration properties file should contain the following information:
|
* <p> The configuration properties file should contain the following information:
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* IDENT.driver=[jdbc driver class]
|
|
||||||
* IDENT.url=[jdbc driver url]
|
* IDENT.url=[jdbc driver url]
|
||||||
* IDENT.username=[jdbc username]
|
* IDENT.username=[jdbc username]
|
||||||
* IDENT.password=[jdbc password]
|
* IDENT.password=[jdbc password]
|
||||||
@@ -42,7 +41,6 @@ import static com.samskivert.jdbc.Log.log;
|
|||||||
* defaults. For example:
|
* defaults. For example:
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* default.driver=[jdbc driver class]
|
|
||||||
* default.url=[jdbc driver class]
|
* default.url=[jdbc driver class]
|
||||||
*
|
*
|
||||||
* IDENT1.username=[jdbc username]
|
* IDENT1.username=[jdbc username]
|
||||||
@@ -59,7 +57,6 @@ public class StaticConnectionProvider implements ConnectionProvider
|
|||||||
/** Creates a provider for testing, using HSQLDB. */
|
/** Creates a provider for testing, using HSQLDB. */
|
||||||
public static ConnectionProvider forTest (String dbname) {
|
public static ConnectionProvider forTest (String dbname) {
|
||||||
Properties props = new Properties();
|
Properties props = new Properties();
|
||||||
props.setProperty("default.driver", "org.hsqldb.jdbcDriver");
|
|
||||||
props.setProperty("default.username", "sa");
|
props.setProperty("default.username", "sa");
|
||||||
props.setProperty("default.password", "none");
|
props.setProperty("default.password", "none");
|
||||||
props.setProperty("default.url", "jdbc:hsqldb:mem:" + dbname);
|
props.setProperty("default.url", "jdbc:hsqldb:mem:" + dbname);
|
||||||
@@ -193,14 +190,13 @@ public class StaticConnectionProvider implements ConnectionProvider
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected static class Info {
|
protected static class Info {
|
||||||
public final String ident, driver, url, username, password;
|
public final String ident, url, username, password;
|
||||||
public final Boolean autoCommit;
|
public final Boolean autoCommit;
|
||||||
|
|
||||||
public Info (String ident, Properties props) throws PersistenceException {
|
public Info (String ident, Properties props) throws PersistenceException {
|
||||||
this.ident = ident;
|
this.ident = ident;
|
||||||
this.driver = requireProp(props, "driver", "No driver class specified");
|
this.url = requireProp(props, "url", "No database URL specified");
|
||||||
this.url = requireProp(props, "url", "No driver URL specified");
|
this.username = requireProp(props, "username", "No database username specified");
|
||||||
this.username = requireProp(props, "username", "No driver username specified");
|
|
||||||
this.password = props.getProperty("password", "");
|
this.password = props.getProperty("password", "");
|
||||||
String ac = props.getProperty("autocommit");
|
String ac = props.getProperty("autocommit");
|
||||||
this.autoCommit = (ac == null) ? null : Boolean.valueOf(ac);
|
this.autoCommit = (ac == null) ? null : Boolean.valueOf(ac);
|
||||||
@@ -240,28 +236,14 @@ public class StaticConnectionProvider implements ConnectionProvider
|
|||||||
/** Opens and returns a new connection to this mapping's database. */
|
/** Opens and returns a new connection to this mapping's database. */
|
||||||
public Connection openConnection (String ident, Boolean autoCommit)
|
public Connection openConnection (String ident, Boolean autoCommit)
|
||||||
throws PersistenceException {
|
throws PersistenceException {
|
||||||
// create an instance of the driver
|
|
||||||
Driver jdriver;
|
|
||||||
try {
|
|
||||||
jdriver = (Driver)Class.forName(_info.driver).
|
|
||||||
getDeclaredConstructor().newInstance();
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new PersistenceException(
|
|
||||||
"Error loading driver [class=" + _info.driver + "].", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
// create the connection
|
// create the connection
|
||||||
Connection conn;
|
Connection conn;
|
||||||
try {
|
try {
|
||||||
Properties props = new Properties();
|
conn = DriverManager.getConnection(_info.url, _info.username, _info.password);
|
||||||
props.put("user", _info.username);
|
|
||||||
props.put("password", _info.password);
|
|
||||||
conn = jdriver.connect(_info.url, props);
|
|
||||||
|
|
||||||
} catch (SQLException sqe) {
|
} catch (SQLException sqe) {
|
||||||
throw new PersistenceException(
|
throw new PersistenceException(
|
||||||
"Error creating database connection [driver=" + _info.driver +
|
"Error creating database connection [url=" + _info.url +
|
||||||
", url=" + _info.url + ", username=" + _info.username + "].", sqe);
|
", username=" + _info.username + "].", sqe);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if we were requested to configure auto-commit, then do so
|
// if we were requested to configure auto-commit, then do so
|
||||||
|
|||||||
Reference in New Issue
Block a user