Results 1 to 10 of 10
- 07-30-2010, 12:55 AM #1
Member
- Join Date
- Jul 2010
- Location
- Lima, Peru
- Posts
- 47
- Rep Power
- 0
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.
- 07-30-2010, 07:47 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
How did you determine that "apparently it is not finding the driver", when you ignore the exception?
- 07-30-2010, 10:25 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Put another way, what exception do you get (full text and stack trace)?
What command do you use to run your project?
- 07-30-2010, 10:06 PM #4
Member
- Join Date
- Jul 2010
- Location
- Lima, Peru
- Posts
- 47
- Rep Power
- 0
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.
- 08-02-2010, 09:51 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
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.
- 08-02-2010, 09:54 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
- 08-02-2010, 10:23 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
That's when you point them to the docs and walk away...:)
- 08-09-2010, 12:53 AM #8
Member
- Join Date
- Jul 2010
- Location
- Lima, Peru
- Posts
- 47
- Rep Power
- 0
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.
- 08-09-2010, 12:59 AM #9
Where is the file: first.class? The java program is looking for it.java -cp "D:\Sun\SDK\sqlitejdbc-v056.jar" first
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
- 08-09-2010, 01:24 AM #10
Member
- Join Date
- Jul 2010
- Location
- Lima, Peru
- Posts
- 47
- Rep Power
- 0
Similar Threads
-
ClassNotFoundException: org.sqlite.JDBC
By JohnnyR in forum AWT / SwingReplies: 4Last Post: 02-25-2009, 06:30 AM -
JDBC Driver problem
By Swamipsn in forum New To JavaReplies: 3Last Post: 08-09-2007, 03:55 PM -
JDBC driver connection problem
By creativehacker in forum JDBCReplies: 3Last Post: 07-10-2007, 09:58 PM -
Help with JDBC driver
By Daniel in forum JDBCReplies: 2Last Post: 07-03-2007, 08:16 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks