簡體   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