繁体   English   中英

SQLException:没有为url = jdbc:derby找到合适的驱动程序

[英]SQLException: No suitable driver found for url=jdbc:derby

我在建立与本地Apache Derby(Java DB)数据库的连接时遇到了问题,这是我一直在研究的小游戏。 抛出异常的代码如下所示:

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添加到库中(并检查它是否位于Project> Properties> Libraries下的类路径中)
  • 将Java DB驱动程序添加到库中(并检查它是否位于“项目”>“属性”>“库”下的类路径中)
  • 使用Class.forName("org.apache.derby.jdbc.ClientDriver")注册驱动程序
  • 使用Class.forName("org.apache.derby.jdbc.EmbeddedDriver")注册驱动程序
  • 添加虚拟驱动程序

以及上述的组合。 没有一个有效,我认为我的头发开始脱落。 任何人都可以向我提供为什么它不会注册驱动程序的答案?

谢谢! 我正在使用Netbeans和JDK 1.8。

您的连接网址不应以字符“url =”开头。 代替

private final String url = "url=jdbc:derby://localhost:1527/GameDB;create=true";

您的网址应以“jdbc:”开头,例如,

private final String url = "jdbc:derby://localhost:1527/GameDB;create=true";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM