Results 1 to 10 of 10
  1. #1
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Angry Problem finding SQLite JDBC driver

    So I am trying to use SQLite. I have placed both sqlite3.exe and the JDBC driver (sqlitejdbc-v056.jar) into my current directory.

    Within java I place the following code:


    try{
    Class.forName("org.sqlite.JDBC");
    Connection con = DriverManager.getConnection("jdbc:sqlite:mydata.db ");
    Statement stmt = con.createStatement();
    stmt.executeUpdate("create table mytable(x INTEGER PRIMARY KEY, name);");
    stmt.close();
    con.close();
    }catch(Exception e){}

    Now, apparently it is not finding the driver. I have tried many strategies and I still have problems. Does anyone know what is going on?

    I also tried setting the classpath among other things but it still did not work. If you have any tips for me they would be greatly appreciated.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    How did you determine that "apparently it is not finding the driver", when you ignore the exception?

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,934
    Rep Power
    19

    Default

    Put another way, what exception do you get (full text and stack trace)?
    What command do you use to run your project?

  4. #4
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Default

    HERE IS THE FULL CODE:

    import java.sql.*;


    public class litef3{


    public static void main(String[] args) {

    try{
    Class.forName("org.sqlite.JDBC");
    Connection con =

    DriverManager.getConnection("jdbc:sqlite:mydata.db ");
    Statement stmt = con.createStatement();
    stmt.executeUpdate("create table mytable(x INTEGER PRIMARY

    KEY, name);");
    stmt.close();
    con.close();
    }catch(Exception e){System.err.println(e.getMessage());}


    }

    }



    HERE IS WHAT I TYPE IN THE COMMAND LINE:

    >javac litef3.java
    >java litef3
    org.sqlite.JDBC

    So I basically get the message "org.sqlite.JDBC"




    Then I tried modifying my code to throw an SQLException:



    import java.sql.*;


    public class litef3{


    public static void main(String[] args) throws SQLException{

    try{
    Class.forName("org.sqlite.JDBC");
    Connection con =

    DriverManager.getConnection("jdbc:sqlite:mydata.db ");
    Statement stmt = con.createStatement();
    stmt.executeUpdate("create table mytable(x INTEGER PRIMARY
    KEY, name);");
    stmt.close();
    con.close();
    }catch(SQLException e){System.err.println(e.getMessage());}


    }

    }



    EXECUTION:
    >javac litef3.java
    >java litef3
    litef3.java:13: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
    Class.forName("org.sqlite.JDBC");

    (I'm not sure how to make the SQLException work right)


    My OS is Windows XP pro, and I am simply using Notepad to code my program. Again, both the .jar file of the driver, and the sqlite3.exe program are in my current directory.


    So I think the problem is with the "Class.forName("org.sqlite.JDBC");" line. As far as I know the path is correct?

    Thanks again.
    Last edited by PrinceSendai; 07-30-2010 at 10:09 PM.

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,934
    Rep Power
    19

    Default

    First off, when logging exceptions print the stack trace, not the message...you lose loads of useful data when simply doing getMessage().

    Second, you need to include all required libraries in the runtime classpath, that is list the jars you use using a "-cp" switch. Without that it won't find the driver, for example, because it's not on its classpath.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by Tolls View Post
    Second, you need to include all required libraries in the runtime classpath, that is list the jars you use using a "-cp" switch. Without that it won't find the driver, for example, because it's not on its classpath.
    That's the thing, it's not on the classpath and I am not going to wade any deeper into that, as classpath discussions on a forum, in my experience, lead to nothing but frustation on both sides.

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,934
    Rep Power
    19

    Default

    That's when you point them to the docs and walk away...:)

  8. #8
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Default

    Thanks for the help. I tried experimenting a little more but I still can't get it to work.

    import java.sql.*;

    public class first{

    public static void main(String[] args){

    try{
    Class.forName("org.sqlite.JDBC");
    Connection con = DriverManager.getConnection("jdbc:sqlite:first.db" , "admin", "admin");
    Statement stmt = con.createStatement();
    stmt.executeUpdate("create table the_table (x INTEGER PRIMARY KEY, data)");
    stmt.executeUpdate("insert into the_table values (1, 'This is amazing.')");
    stmt.close();
    con.close();
    }catch(Exception e){e.printStackTrace();}
    }
    }

    >>>java -cp "D:\Sun\SDK\sqlitejdbc-v056.jar" first
    Exception in thread "main" java.lang.NoClassDefFoundError: first
    Caused by: java.lang.ClassNotFoundException: first
    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.ClassLoader.loadClassInternal(Unknown Source)
    Could not find the main class: first. Program will exit.


    Does anyone know what the problem is? I either have trouble with the driver, or I have trouble finding my own class.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    java -cp "D:\Sun\SDK\sqlitejdbc-v056.jar" first
    Where is the file: first.class? The java program is looking for it.
    Perhaps you need to add to the classpath to include the current directory.
    Try this, add a ;. to the end of the -cp:
    java -cp "D:\Sun\SDK\sqlitejdbc-v056.jar";. first[

    NoClassDefFoundError: first

  10. #10
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

Similar Threads

  1. ClassNotFoundException: org.sqlite.JDBC
    By JohnnyR in forum AWT / Swing
    Replies: 4
    Last Post: 02-25-2009, 06:30 AM
  2. JDBC Driver problem
    By Swamipsn in forum New To Java
    Replies: 3
    Last Post: 08-09-2007, 03:55 PM
  3. JDBC driver connection problem
    By creativehacker in forum JDBC
    Replies: 3
    Last Post: 07-10-2007, 09:58 PM
  4. Help with JDBC driver
    By Daniel in forum JDBC
    Replies: 2
    Last Post: 07-03-2007, 08:16 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •