【问题标题】:JDBC for SQLite in Netbeans: No suitable driver foundNetbeans 中 SQLite 的 JDBC:找不到合适的驱动程序
【发布时间】:2014-08-12 18:22:33
【问题描述】:

我需要将 SQLite 文件中的数据加载到我在 Netbeans 中开发的 java 程序中。 该文件将通过摆动菜单项加载。我使用sqlitejdbc 作为驱动程序。

以下是我认为很重要的代码块:

// header stuff
package aufgabe_9;

import java.sql.*;

//...

// menu item opening the file
private void mitemOpenFileActionPerformed(java.awt.event.ActionEvent evt)  
{                                              

    /**
     * Handles the dialogue for selecting and loading file.
     */
    JFileChooser fileChoose = new JFileChooser();
    fileChoose.showOpenDialog(this); //'this' calls the current object

     //Load the sql file
     try {
        String filePath = fileChoose.getSelectedFile().toString();
        Connection conn = DriverManager.getConnection("jdbc:sqlite:" +  
                    filePath);

        //Close the connection
        if (conn != null)
            conn.close();

    }


    catch (SQLException e){System.err.println("Database problem: " + e);}
    }                                  
}

//...

运行程序并通过菜单加载文件时,出现以下错误:

java.sql.SQLException: No suitable driver found for jdbc:sqlite:/home/levent
/temp/A9AProbeflaeche.db

在阅读了相应的 stackexchange 帖子后,我了解到这 问题可能是由 (1) 格式错误的文件 URL 或 (2) 驱动程序没有 加载。以下是一些进一步的信息:

  • 我通过 Tools --> Libraries 将 sqlitejdbc-3.7.2.jar 添加到库类路径中,并通过 Window --> Projects 将 sqlitejdbc-3.7.2.jar 添加到项目库中.
  • 我还使用this function 检查了类路径。它包含 jdbc jar 文件的路径,正如预期的那样。
  • 我可以通过 Services 菜单毫无问题地连接到数据库,因此我可以假设 URL 是正确的,以及我的系统上运行的 sqlite。
  • 一些操作系统信息:我在 64 位 ARCH Linux 3.12.9-2 上运行 Netbeans 8.0。

谁能告诉我我在这里缺少什么?任何帮助表示赞赏!

问题已解决 这是对我有用的代码:

//...
private void mitemOpenFileActionPerformed(java.awt.event.ActionEvent evt)   
{                                              

    /**
    * Handles the dialogue for selecting and loading file.
    */
    JFileChooser fileChoose = new JFileChooser();
    fileChoose.showOpenDialog(this); 

    //Load the sql file
    try {
        //Get file path
        String filePath = fileChoose.getSelectedFile().toString();

        //Open connection
        Class.forName("org.sqlite.JDBC");
        Connection conn = DriverManager.getConnection("jdbc:sqlite:" + filePath);

        //Do stuff...                       

        //Close the connection
        conn.close();

    }

    //"Multicatch":
    catch (SQLException | ClassNotFoundException e) { 
    System.err.println("Database problem: " + e);
}
//...

【问题讨论】:

    标签: java sqlite netbeans jdbc sqlexception


    【解决方案1】:

    您可能需要加载驱动程序类,以便它使用以下代码将自身注册到 DriverManager: Class.forName("org.sqlite.JDBC");

    注意:这只需要在您的应用程序中调用一次。

    这是在 Java 包含 ServiceLoader API 之前的标准过程,现在 DriverManager 使用该 API 来注册它在类路径中找到的驱动程序,但驱动程序需要声明一个名为 java.sql.Driver 的文件,其中包含jar 目录 META-INF\services 中的驱动类。

    【讨论】:

    • 该死的,非常感谢!我实际上在不同的帖子中读到了Class.forName() 声明,但当时仍然有错误,可能是因为网址格式错误。另外,我认为它已经过时了,因为 DriverManager 处理它。现在完美运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-02
    • 2017-06-14
    • 2012-07-14
    • 2020-05-02
    • 2013-04-01
    • 2014-07-14
    相关资源
    最近更新 更多