jeudi 25 juin 2015

Error connection to SQLite Database using java

I want to use SQLite in Java, so I created a database and a table with some records. I want to retrieve the records, but I am getting an error while trying to connect. I had download the sqlite jdbc from http://ift.tt/1fOS9gr . I am running the next code and getting the error: java.lang.ClassNotFoundException: org.sqlite.JDBC in the statement Class.forName("org.sqlite.JDBC"):

public List<Message> queryAll() {
        List<Message> results = new ArrayList<>();
        Connection c = null;
        Statement stmt = null;
        try {
            Class.forName("org.sqlite.JDBC");
            c = DriverManager.getConnection("jdbc:sqlite:test.db");
            c.setAutoCommit(false);
            System.out.println("Opened database successfully");

            stmt = c.createStatement();
            ResultSet rs = stmt.executeQuery( "SELECT * FROM MESSAGES;" );            
            while ( rs.next() ) {
               //Get record from cursor
                int id = rs.getInt("id");
                String  message = rs.getString("message");
                Date created  = rs.getDate("created");
                String  author = rs.getString("author");
                Message m= new Message(id,message,created,author);

                //add the record into the list
                results.add(m);
            }
            rs.close();
            stmt.close();
            c.close();            

        } catch ( Exception e ) {
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
            System.exit(0);
          }
          System.out.println("Operation done successfully");
        return results;

Do you know what else I have to do?

Thank you in advance for your help.

Cheers

Aucun commentaire:

Enregistrer un commentaire