Reputation: 6152
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
Upvotes: 3
Views: 4813
Reputation: 136152
Create a text file java.sql.Driver
with 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.
Upvotes: 2
Reputation: 2800
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;
Upvotes: 5