Timothy Lawman
Timothy Lawman

Reputation: 2302

Problems connecting to sqlite database in java

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?

Upvotes: 0

Views: 1574

Answers (2)

JJF
JJF

Reputation: 2777

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

Upvotes: 0

OneCricketeer
OneCricketeer

Reputation: 191681

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

Upvotes: 2

Related Questions