简体   繁体   English

Netbeans中的SQLite JDBC:找不到合适的驱动程序

[英]JDBC for SQLite in Netbeans: No suitable driver found

I need to load data from an SQLite file into a java program which I develop in Netbeans. 我需要将SQLite文件中的数据加载到我在Netbeans中开发的java程序中。 The file is to be loaded via a swing menu item. 该文件将通过swing菜单项加载。 I'm using sqlitejdbc as driver. 我正在使用sqlitejdbc作为驱动程序。

Here are the code blocks I assume to be important: 以下是我认为重要的代码块:

// 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);}
    }                                  
}

//...

When running the program and loading a file via the menu, I get the following error: 运行程序并通过菜单加载文件时,出现以下错误:

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

After reading the respective stackexchange posts, I understand that this problem can be caused by (1) a malformed file URL or (2) the driver not being loaded. 在阅读了各自的stackexchange帖子之后,我明白这个问题可能是由于(1)格式错误的文件URL或(2)未加载驱动程序引起的。 Here's some further information: 以下是一些进一步的信息:

  • I added the sqlitejdbc-3.7.2.jar to the library classpath via Tools --> Libraries as well as to the project libraries via Window --> Projects . 我通过Tools - > Libraries将sqlitejdbc-3.7.2.jar添加到库类路径中,并通过Window - > Projects添加到项目库中。
  • I also checked the Classpath by using this function . 我还使用此函数检查了Classpath It contains the path to the jdbc jar-file just as expected. 它包含jdbc jar文件的路径,正如预期的那样。
  • I can connect to the database via the Services menu without any problems, so I can assume the URL to be correct, as well as sqlite running on my system. 我可以通过“ 服务”菜单连接到数据库而没有任何问题,因此我可以假设URL正确,以及在我的系统上运行的sqlite。
  • Some OS information: I'm running Netbeans 8.0 on 64 bit ARCH Linux 3.12.9-2. 一些操作系统信息:我在64位ARCH Linux 3.12.9-2上运行Netbeans 8.0。

Can anybody tell me what I'm missing here? 任何人都可以告诉我我在这里缺少什么吗? Any help appreciated! 任何帮助赞赏!

Problem solved Here is the code that works for me: 问题解决了以下代码对我有用:

//...
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);
}
//...

You probably need to load the driver class so that it registers itself to the DriverManager using the following code: Class.forName("org.sqlite.JDBC"); 您可能需要加载驱动程序类,以便使用以下代码将自身注册到DriverManager:Class.forName(“org.sqlite.JDBC”);

Note: this only needs to be called once in your application. 注意:这只需要在您的应用程序中调用一次。

This was a standard procedure before the Java included the ServiceLoader API, now the DriverManager uses that API to register the drivers it finds in the classpath, but the drivers need to declare a file named java.sql.Driver containing the name of the driver class in the directory META-INF\\services of their jar. 这是Java包含ServiceLoader API之前的标准过程,现在DriverManager使用该API来注册它在类路径中找到的驱动程序,但驱动程序需要声明一个名为java.sql.Driver的文件,其中包含驱动程序类的名称在他们的jar的目录META-INF \\ services中。

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

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