JDBC for SQLite in Netbeans: No suitable driver found

限于喜欢 提交于 2019-12-23 17:05:33

问题


I need to load data from an SQLite file into a java program which I develop in Netbeans. The file is to be loaded via a swing menu item. I'm using sqlitejdbc as driver.

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. 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.
  • I also checked the Classpath by using this function. It contains the path to the jdbc jar-file just as expected.
  • 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.
  • Some OS information: I'm running Netbeans 8.0 on 64 bit ARCH Linux 3.12.9-2.

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

回答1:


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");

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.



来源:https://stackoverflow.com/questions/24353597/jdbc-for-sqlite-in-netbeans-no-suitable-driver-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!