Search code examples
javasqlitenetbeansodbc

sqlite not found in Netbeans Java


I am using Netbeans 16 and Java 19.0.2 on Windows 11.

I am getting:

java.lang.ClassNotFoundException: org.sqlite.JDBC

when I try to access a new sqlite file:

package com.thompco.propertymanager.table;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Database {
    String filename;
    Connection connection;  

    public Database(String filename) throws ClassNotFoundException {
        this.filename = filename;
        connect();
    }

    public final void connect() throws ClassNotFoundException {  
        try {
            Class.forName("org.sqlite.JDBC");
            String url = String.format("jdbc:sqlite:%s", filename);  
            connection = DriverManager.getConnection(url);  
            System.out.println("Connection to SQLite has been established.");  
        } catch (SQLException e) {  
            System.out.println(e.getMessage());  
        } finally {  
            try {  
                if (connection != null) {  
                    connection.close();  
                }  
            } catch (SQLException ex) {  
                System.out.println(ex.getMessage());  
            }  
        }  
    }  
        
    public static void main(String[] args) {
        try {
            Database database = new Database("newFile.sqlite");
            database.connect(); 
            database.createTransactionTable();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }  
}

I (think) I have added the sqlite jar to my path: enter image description here

I tried to add it at the top of my file: enter image description here


Solution

  • It looks like you added the sqlite jar to the list of jars for the library Absolute Layout. Now you still need to make sure that this library is added to your project (a prerequisite before you can import the classes in your source code).

    Hints:

    • You likely should have added sqlite as a separate library.
    • It would be more advisable to use the Maven build system and specify dependencies in pom.xml. That one can be version controlled. And Maven will download the libraries on your behalf.