--- a/db/apichanges.xml +++ a/db/apichanges.xml @@ -105,6 +105,21 @@ + + + Add ability to get the underlying JDBC Driver instance for a JDBCDriver + + + + + + This change makes it possible to get the underlying Driver class. Without + this, the only way you can obtain the driver is to create your own classloader that + has the driver classpath URLs in its search list and then load the driver. + This makes things much easier. + + + Add ability to show the Add JDBC Driver dialog synchronously --- a/db/arch.xml +++ a/db/arch.xml @@ -206,6 +206,14 @@ This file should be registered in the Databases/JDBCDrivers folder of the module layer. To addres a bundled JAR inside the IDE the nbinst protocol can be used in the URLs: nbinst:/modules/ext/bundled-driver.jar. +

+ + +

+ You can use the JDBCDriver.getDriver() + method to obtain a reference to the underlying JDBC Driver instance. This is useful if you want to use the registered + drivers but create your own JDBC connections independent of the Database Explorer. +

--- a/db/manifest.mf +++ a/db/manifest.mf @@ -1,7 +1,7 @@ Manifest-Version: 1.0 OpenIDE-Module: org.netbeans.modules.db/1 OpenIDE-Module-Install: org/netbeans/modules/db/DatabaseModule.class -OpenIDE-Module-Implementation-Version: 27 +OpenIDE-Module-Implementation-Version: 28 OpenIDE-Module-Layer: org/netbeans/modules/db/resources/mf-layer.xml OpenIDE-Module-Requires: org.netbeans.api.javahelp.Help OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/db/resources/Bundle.properties --- a/db/src/org/netbeans/api/db/explorer/JDBCDriver.java +++ a/db/src/org/netbeans/api/db/explorer/JDBCDriver.java @@ -42,7 +42,10 @@ package org.netbeans.api.db.explorer; import java.net.URL; +import java.sql.Driver; +import java.sql.SQLException; import java.util.Arrays; +import org.netbeans.modules.db.explorer.DbDriverManager; /** * Encapsulates a JDBC driver. @@ -114,6 +117,25 @@ public String getName() { return name; } + + /** + * Get a reference to the underlying java.sql.Driver for this JDBCDriver. + * This can be useful if you want to use the registered drivers to manage + * your own JDBC connections independent of the Database Explorer + * + * @return an instance of the java.sql.Driver for this JDBCDriver + * + * @throws DatabaseException if there was an error trying to get the driver instance + * + * @since 1.28 + */ + public Driver getDriver() throws DatabaseException { + try { + return DbDriverManager.getDefault().getDriver(this); + } catch (SQLException sqle) { + throw new DatabaseException(sqle); + } + } public String toString() { return "JDBCDriver[name='" + name + // NOI18N --- a/db/src/org/netbeans/modules/db/explorer/DbDriverManager.java +++ a/db/src/org/netbeans/modules/db/explorer/DbDriverManager.java @@ -213,6 +213,21 @@ } return d; } + + /** + * Get the driver for a JDBCDriver. It only tries to load it using Class.forName() - + * there is no URL to work with + */ + public Driver getDriver(JDBCDriver jdbcDriver) throws SQLException { + ClassLoader l = getClassLoader(jdbcDriver); + try { + return (Driver)Class.forName(jdbcDriver.getClassName(), true, l).newInstance(); + } catch (Exception e) { + SQLException sqlex = createDriverNotFoundException(); + sqlex.initCause(e); + throw sqlex; + } + } /** * Gets a driver, but can skip DriverManager and doesn't throw SQLException if a driver can't be found. @@ -236,13 +251,9 @@ // didn't find it, try to load it from jdbcDriver, if any if (jdbcDriver != null) { - ClassLoader l = getClassLoader(jdbcDriver); - try { - return (Driver)Class.forName(jdbcDriver.getClassName(), true, l).newInstance(); - } catch (Exception e) { - SQLException sqlex = createDriverNotFoundException(); - sqlex.initCause(e); - throw sqlex; + Driver d = getDriver(jdbcDriver); + if (d != null) { + return d; } } --- a/db/test/unit/src/org/netbeans/api/db/explorer/JDBCDriverManagerTest.java +++ a/db/test/unit/src/org/netbeans/api/db/explorer/JDBCDriverManagerTest.java @@ -41,19 +41,18 @@ package org.netbeans.api.db.explorer; -import java.lang.ref.WeakReference; import java.net.URL; +import java.sql.Driver; import org.netbeans.modules.db.explorer.driver.JDBCDriverConvertor; -import org.netbeans.modules.db.test.TestBase; import org.netbeans.modules.db.test.Util; +import org.netbeans.modules.db.util.DBTestBase; import org.openide.loaders.DataObject; /** * * @author Andrei Badea */ -public class JDBCDriverManagerTest extends TestBase { - +public class JDBCDriverManagerTest extends DBTestBase { public JDBCDriverManagerTest(String testName) { super(testName); } @@ -98,4 +97,12 @@ assertSame(driver, JDBCDriverManager.getDefault().getDrivers("org.bar.BarDriver")[0]); */ } + + public void testGetDriver() throws Exception { + JDBCDriver jdbcDriver = getJDBCDriver(); + Driver driver = jdbcDriver.getDriver(); + + assertNotNull(driver); + + } } --- a/db/test/unit/src/org/netbeans/modules/db/util/DBTestBase.java +++ a/db/test/unit/src/org/netbeans/modules/db/util/DBTestBase.java @@ -130,14 +130,24 @@ super(name); } + protected static JDBCDriver getJDBCDriver() throws Exception{ + if (jdbcDriver == null) { + jdbcDriver = JDBCDriver.create("derbydriver", "derbydriver", driverClass, new URL[] {driverJarUrl}); + assertNotNull(jdbcDriver); + JDBCDriverManager.getDefault().addDriver(jdbcDriver); + } + + return jdbcDriver; + + } + /** * Get the DatabaseConnection for the configured Java DB database. This * method will create and register the connection the first time it is called */ protected static DatabaseConnection getDatabaseConnection() throws Exception { if (dbConnection == null) { - JDBCDriver driver = JDBCDriver.create("derbydriver", "derbydriver", driverClass, new URL[] {driverJarUrl}); - JDBCDriverManager.getDefault().addDriver(driver); + JDBCDriver driver = getJDBCDriver(); dbConnection = DatabaseConnection.create(driver, dbUrl, username, "APP", password, false); ConnectionManager.getDefault().addConnection(dbConnection); @@ -208,7 +218,7 @@ conn = DriverManager.getConnection(dbUrl, username, password); return conn; } - + protected void createSchema() throws Exception { dropSchema(); conn.createStatement().executeUpdate("CREATE SCHEMA " + SCHEMA);