我正在编写自己的自定义 JDBC 驱动程序。我想知道如何在客户端代码中配置要传递给 DriverManager.getConnection
的 URL 前缀(即,使用 mysql 连接器时相当于 jdbc:mysql)?我似乎一直收到 java.sql.SQLException: No suitable driver found
。我的代码目前如下所示:
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;
}
测试代码:
Class.forName("CustomDriver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection("customDriver://localhost/testdb");
// throws SQLException
最佳答案
你需要实现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;
关于java - 自定义 JDBC 驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16027641/