gpt4 book ai didi

java - 自定义 JDBC 驱动程序

转载 作者:行者123 更新时间:2023-11-30 07:17:34 25 4
gpt4 key购买 nike

我正在编写自己的自定义 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com