Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 76627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:42:11+00:00 2026-05-10T20:42:11+00:00

I need to test a JDBC connection to a database. The java code to

  • 0

I need to test a JDBC connection to a database. The java code to do that should be as simple as:

DriverManager.getConnection('jdbc connection URL', 'username', 'password'); 

The driver manager will lookup the appropriate the driver for the given connection URL. However I need to be able to load the JDBC driver (jar) at runtime. I.e I don’t have the JDBC driver on the classpath of the java application that runs the snippet of code above.

So I can load the driver using this code, for example:

URLClassLoader classLoader = new URLClassLoader(new URL[]{'jar URL'}, this.getClass().getClassLoader()); Driver driver = (Driver) Class.forName('jdbc driver class name', true, classLoader).newInstance(); 

But then the driver manager still won’t pick it up as I can’t tell it which classloader to use. I tried setting the current thread’s context classloader and it still doesn’t work.

Anyone has any idea on the best way to achieve that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:42:12+00:00Added an answer on May 10, 2026 at 8:42 pm

    From the article Pick your JDBC driver at runtime; I am just going to post the code here for reference.

    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:

    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();     } } 

    This way the driver you register is of type DelegatingDriver 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:

    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 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 63k
  • Answers 63k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Use tee for windows. May 11, 2026 at 10:28 am
  • added an answer MediaWiki's API is running on Wikipedia (docs). You can also… May 11, 2026 at 10:28 am
  • added an answer It's not possible per the Rss Spec, and if you… May 11, 2026 at 10:28 am

Related Questions

I need to test a JDBC connection to a database. The java code to
I need to test a serial port application on Linux, however, my test machine
I need to test a site with a dynamic menu in Mac Firefox, but
I need to test a function that needs to query a page on an
I need to test a url that it does not end with .asp So
I need to test a web form that takes a file upload. The filesize
I need to test if a file is a shortcut. I'm still trying to
I understand the need to test a class that has logic (for instance, one
I need to test whether various types of database objects exist in a given
I am writing a small app which I need to test with utf-8 characters

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.