Levent B.
Levent B.

Reputation: 33

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:

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

Upvotes: 3

Views: 5344

Answers (1)

user3679868
user3679868

Reputation: 693

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.

Upvotes: 1

Related Questions