dimanche 25 octobre 2015

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 prgram 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 i sthe 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?

Aucun commentaire:

Enregistrer un commentaire