Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From the article <a href="http://www.kfu.com/~nsayer/Java/dyn-jdbc.html" rel="noreferrer">Pick your JDBC driver at runtime</a>; I am just going to post the code here for reference.</p> <p>The idea is to trick the driver manager into thinking that the driver was loaded from the system classloader. To do this we use this class:</p> <pre><code>public class DelegatingDriver implements Driver { private final Driver driver; public DelegatingDriver(Driver driver) { if (driver == null) { throw new IllegalArgumentException("Driver must not be null."); } this.driver = driver; } public Connection connect(String url, Properties info) throws SQLException { return driver.connect(url, info); } public boolean acceptsURL(String url) throws SQLException { return driver.acceptsURL(url); } public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { return driver.getPropertyInfo(url, info); } public int getMajorVersion() { return driver.getMajorVersion(); } public int getMinorVersion() { return driver.getMinorVersion(); } public boolean jdbcCompliant() { return driver.jdbcCompliant(); } } </code></pre> <p>This way the driver you register is of type <code>DelegatingDriver</code> which is loaded with the system classloader. You now just have to load the driver you really want to use using whatever classloader you want. For example:</p> <pre><code>URLClassLoader classLoader = new URLClassLoader(new URL[]{"path to my jdbc driver jar"}, this.getClass().getClassLoader()); Driver driver = (Driver) Class.forName("org.postgresql.Driver", true, classLoader).newInstance(); DriverManager.registerDriver(new DelegatingDriver(driver)); // register using the Delegating Driver DriverManager.getDriver("jdbc:postgresql://host/db"); // checks that the driver is found </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload