3

I am writing my own custom JDBC driver. I am wondering how I can configure the URL prefix to be passed to DriverManager.getConnection in client code (i.e., the equivalent of jdbc:mysql when using mysql connector)? I seem to keep getting java.sql.SQLException: No suitable driver found. My code currently looks like the following:

static
{
    try
    {
        CustomDriver driverInst = new CustomDriver();
        DriverManager.registerDriver(driverInst);
    }
    catch (Exception e) { e.printStackTrace(); }
}

public CustomDriver () throws SQLException 
{
    super();
}

@Override
public Connection connect (String url, Properties info) throws SQLException
{
    // this is never called
    return null;
}

test code:

      Class.forName("CustomDriver");

      System.out.println("Connecting to database...");
      conn = DriverManager.getConnection("customDriver://localhost/testdb"); 
      // throws SQLException
JRR
  • 6,014
  • 6
  • 39
  • 59
  • 1
    duplicate of http://stackoverflow.com/questions/861500/url-to-load-resources-from-the-classpath-in-java and http://stackoverflow.com/questions/6278299/java-registering-custom-url-protocol-handlers – Vitaly Apr 16 '13 at 02:18
  • Vitaly: I am asking very specifically for JDBC, not a custom URL handler. It is not clear to me how the links you mentioned can solve the exception thrown by DriverManager. – JRR Apr 16 '13 at 02:21
  • btw, is it working now? – Vitaly Apr 16 '13 at 02:48
  • 3
    @JRR Hi, did you successfully implemented your own JDBC driver ? Can you please share your code for that? I am also trying to implement, but miserably stuck in it - or if it's on your github - share the repo link please. I'll be grateful. Thanks a lot in advance. – Shumail Mar 07 '15 at 14:47
  • 1
    @JRR can you please advise me on how to write a custom jdbc driver? If there is a good reference somewhere, please help me with its link – KCK Aug 14 '18 at 12:32

2 Answers2

4

You need to implement Driver.boolean acceptsURL(String url)

/**
 * Retrieves whether the driver thinks that it can open a connection
 * to the given URL.  Typically drivers will return <code>true</code> if they
 * understand the subprotocol specified in the URL and <code>false</code> if
 * they do not.
 *
 * @param url the URL of the database
 * @return <code>true</code> if this driver understands the given URL;
 *         <code>false</code> otherwise
 * @exception SQLException if a database access error occurs
 */
boolean acceptsURL(String url) throws SQLException;
Vitaly
  • 2,760
  • 2
  • 19
  • 26
  • 1
    This worked. Thanks. I posted an answer earlier but it was deleted and I am not sure why. Here is the original text: Turns out the way it works is that DriverManager will poll each registered driver with the URL and see which one will accept it (by called acceptsURL), and will call connect on the first driver that returns true on acceptsURL. – JRR Apr 18 '13 at 13:26
  • Great! Thanks for making this question-answer complete with your confirmation. – Vitaly Apr 18 '13 at 13:33
2

Create a text file java.sql.Driverwith one line in it - fully qualified name of your driver. Put it in META-INF/services folder. In this case DriverManager will find and instatiate your driver and call acceptsURL(String url) on it.

This is one of the ways to let DriverManager know about your driver, read more in DriverManager API.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275