user3837019
user3837019

Reputation: 221

Connecting SQLite in java via eclipse

Hi i'm currently new to programming and i'm trying to connect my sqlite database in java. I looked at some youtube videos and did exactly as they said.

I'm trying to get access to this database. The database has 3 fields, name, bio, and image. I want to get access to the information in the database.

My code:

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


    public class Database {
         public static void main(String[] args) {

              Connection connection = null;
              ResultSet resultSet = null;
              Statement statement = null;

              try {
                   Class.forName("org.sqlite.JDBC");
                   connection = DriverManager
                             .getConnection("jdbc:sqlite:C:\\Users\\Mariam\\Documents\\GoogleApp\\info.sqlight");
                   statement = connection.createStatement();
                   resultSet = statement
                             .executeQuery("SELECT name FROM PeoplesInfo");
                   while (resultSet.next()) {
                        System.out.println("NAME:"
                                  + resultSet.getString("name"));
                   }
              } catch (Exception e) {
                   e.printStackTrace();
              } finally {
                   try {
                        resultSet.close();
                        statement.close();
                        connection.close();
                   } catch (Exception e) {
                        e.printStackTrace();
                   }
              }
         }
    }

I currently get the following errors:

java.lang.ClassNotFoundException: org.sqlite.JDBC
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at Database.main(Database.java:15)
java.lang.NullPointerException
    at Database.main(Database.java:29)

Many thanks in advance.

Upvotes: 0

Views: 1121

Answers (2)

stevecross
stevecross

Reputation: 5684

It seems that you did not put the driver in your classpath. In Eclipse you can add a jar to your classpath as following:

Right click on your project
Choose 'Build Path'
Choose 'Add External Archives...'
Navigate to the jar-file that includes the SQLite JDBC driver and open it

Now the class org.sqlite.JDBC should be found. If you are using a JDBC 4 driver, you can omit the call to Class.forName().

Upvotes: 1

g.carvalho97
g.carvalho97

Reputation: 332

Your program is not able to find the suitable driver to connect to the database. Thus, giving you a ClassNotFoundException.

Also, check if the database address is correct. I don't remember seeing .sqlight as a database file extension.

Upvotes: 1

Related Questions