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 89k
  • Answers 89k
  • 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
  • Editorial Team
    Editorial Team added an answer In C#, you can't do if (MyObject) to check for… May 11, 2026 at 5:56 pm
  • Editorial Team
    Editorial Team added an answer You now have a number of options that provide this… May 11, 2026 at 5:56 pm
  • Editorial Team
    Editorial Team added an answer Generally, you only want to catch and handle exceptions you… May 11, 2026 at 5:56 pm

Related Questions

I have encountered a strange Invalid Packet Lenght (that is how the error is
I have a simple servlet running in Tomcat. Because the servlet connects to a
Does anybody know what's going on here: I run hibernate 3.2.6 against a PostgreSQL
I have a maven POM file for a web service. For one of the

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.