简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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