SQLException:找不到适合 url=jdbc:derby 的驱动程序
SQLException: No suitable driver found for url=jdbc:derby
我在为我一直在开发的小游戏建立与 本地 Apache Derby(Java 数据库) 数据库的连接时遇到问题。抛出异常的代码如下所示:
public class DatabaseController {
private static Connection conn;
private final String url = "url=jdbc:derby://localhost:1527/GameDB;create=true";
private final String username = "pdc";
private final String password = "123";
/**
* Connects to the database.
*/
public void initialize() {
try{
//Open a connection
conn = DriverManager.getConnection(url, username, password);
} catch(SQLException e){
//Handle errors
Logger.getLogger(DatabaseController.class.getName()).log(Level.SEVERE, null, e);
}
}
}
当我 运行 代码时,我得到以下异常:
Oct 08, 2015 2:27:40 PM pdc.project.Controller.DatabaseController initialize
SEVERE: null
java.sql.SQLException: No suitable driver found for url=jdbc:derby://localhost:1527/GameDB;create=true
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at pdc.project.Controller.DatabaseController.initialize(DatabaseController.java:30)
at pdc.project.Controller.Main.main(Main.java:35)
Exception in thread "main" java.lang.NullPointerException
at pdc.project.Controller.DatabaseController.createTable(DatabaseController.java:63)
at pdc.project.Controller.Main.main(Main.java:36)
我尝试了以下方法:
- 将 derbyclient.jar 添加到库(并检查它是否在项目 > 属性 > 库下的类路径中)
- 将 Java 数据库驱动程序添加到库(并检查它是否在项目 > 属性 > 库下的类路径中)
- 使用
Class.forName("org.apache.derby.jdbc.ClientDriver")
注册驱动程序
- 使用
Class.forName("org.apache.derby.jdbc.EmbeddedDriver")
注册驱动程序
- 添加虚拟驱动程序
以及上述的组合。 None 有效,我想我的头发开始脱落了。谁能告诉我为什么它不注册驱动程序?
谢谢!
我正在使用 Netbeans 和 JDK 1.8.
您的连接 URL 不应以字符 "url=" 开头。而不是
private final String url = "url=jdbc:derby://localhost:1527/GameDB;create=true";
您的 URL 应该以 "jdbc:" 开头,例如
private final String url = "jdbc:derby://localhost:1527/GameDB;create=true";
我在为我一直在开发的小游戏建立与 本地 Apache Derby(Java 数据库) 数据库的连接时遇到问题。抛出异常的代码如下所示:
public class DatabaseController {
private static Connection conn;
private final String url = "url=jdbc:derby://localhost:1527/GameDB;create=true";
private final String username = "pdc";
private final String password = "123";
/**
* Connects to the database.
*/
public void initialize() {
try{
//Open a connection
conn = DriverManager.getConnection(url, username, password);
} catch(SQLException e){
//Handle errors
Logger.getLogger(DatabaseController.class.getName()).log(Level.SEVERE, null, e);
}
}
}
当我 运行 代码时,我得到以下异常:
Oct 08, 2015 2:27:40 PM pdc.project.Controller.DatabaseController initialize
SEVERE: null
java.sql.SQLException: No suitable driver found for url=jdbc:derby://localhost:1527/GameDB;create=true
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at pdc.project.Controller.DatabaseController.initialize(DatabaseController.java:30)
at pdc.project.Controller.Main.main(Main.java:35)
Exception in thread "main" java.lang.NullPointerException
at pdc.project.Controller.DatabaseController.createTable(DatabaseController.java:63)
at pdc.project.Controller.Main.main(Main.java:36)
我尝试了以下方法:
- 将 derbyclient.jar 添加到库(并检查它是否在项目 > 属性 > 库下的类路径中)
- 将 Java 数据库驱动程序添加到库(并检查它是否在项目 > 属性 > 库下的类路径中)
- 使用
Class.forName("org.apache.derby.jdbc.ClientDriver")
注册驱动程序 - 使用
Class.forName("org.apache.derby.jdbc.EmbeddedDriver")
注册驱动程序 - 添加虚拟驱动程序
以及上述的组合。 None 有效,我想我的头发开始脱落了。谁能告诉我为什么它不注册驱动程序?
谢谢! 我正在使用 Netbeans 和 JDK 1.8.
您的连接 URL 不应以字符 "url=" 开头。而不是
private final String url = "url=jdbc:derby://localhost:1527/GameDB;create=true";
您的 URL 应该以 "jdbc:" 开头,例如
private final String url = "jdbc:derby://localhost:1527/GameDB;create=true";