0

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.

2
  • 1
    Is the SQLite driver jar in your classpath? Commented Jul 14, 2014 at 13:23
  • possible duplicate of How to connect SQLite with Java? Commented Jul 14, 2014 at 13:25

2 Answers 2

1

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().

Sign up to request clarification or add additional context in comments.

2 Comments

This helped solved it thanks. Also i'm able to print out the information on the database on the terminal window. I'm currently having a problem printing out the images which are stored on the database. Any help with that?
You should ask a new question and provide more information for this problem.
1

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.

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.