0

I am following a tutorial on connecting my SQLite database to a Java application.

When I run the program I get the following error in the console of NetBeans:

run:

Error connecting to the databasejava.sql.SQLException: No suitable driver found for jdbc:C:\Users\lawman\Documents\Java Working Directory\LoginSql\src\project123.sqlite

BUILD SUCCESSFUL (total time: 0 seconds)

Here is my directory:

enter image description here

I have code to connect to the database in class tobecalledbymain.

I have main in mainclass which creates an instance of tobecalledbymain.

In my library file, I have sqlite-jdbcs.jar imported.

Here is the code for tobecalledinmain:

import java.sql.*;

public class tobecalledinmain {

    public tobecalledinmain(){
        Connection con = null;
        Statement st=null;
        ResultSet rs=null;
        try
        {
            Class.forName("org.sqlite.JDBC");
            con = DriverManager.getConnection("jdbc:C:\\Users\\lawman\\Documents\\"
                    + "Java Working Directory\\LoginSql\\"
                    + "src\\project123.sqlite");
            st=con.createStatement();

            //select all records from the table employee
            //table has three firlds: employeeid,name and surname
            rs=st.executeQuery("SELECT * FROM Employee;");
            while (rs.next())
            {
                 int id = rs.getInt("Employeeid");
                 String name = rs.getString("Name");
                 System.out.println("id = " + id);
                 System.out.println("name= " + name);
                 System.out.println();
            }
            rs.close();
            st.close();
            con.close();

        }catch(Exception e)
        {
            System.out.println("Error connecting to the database" + e);
        }

    }

}

Here is the mainclass code:

public class mainClass {

    public static void main(String[] args){
        new tobecalledinmain();
    }

}
;;

I am not sure why we need to semi-colons!

Anyway, when the tutorial concludes he gets a result from the console. I get the said error message.

What are the drivers in the error message referring to and how do I get them?

2 Answers 2

2

Your jdbc connection string does not specify sqlite. Try this, and use forward slashes.

Connection con = DriverManager.getConnection("jdbc:sqlite:C:/PATH/TO/database.db");
Sign up to request clarification or add additional context in comments.

Comments

0

This was the WRONG answer, pls disregard.

You need to add the .jar for the SQLite database driver to your classpath when you run your code. See https://bitbucket.org/xerial/sqlite-jdbc

You can see how to do that in Netbeans here: How to add a JAR in NetBeans

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.