2

I've read through many of the threads posted on this topic but none of them have been able to solve my problem. I use a mac and I'm trying to connect a java project to sqlite and I'm having some problems. I have successfully connected to mysql to java. To connect mysql I had to add a mysql.jar file to my system/library/java/extensions folder. Once I did that it was smooth sailing. I have not been to fortunate with sqlite. I went to xerial and tried to use their sqlite jdbc but doing that created a ton of other errors when I ran it.

Is there a simple method and an easy jar I can use to connect java to sqlite? something like the process I described above for mysql?

If not can anyone offer me some help.

Thanks for your help!

here is the code I used:

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

public class SQLiteJDBC
{
  public static void main(String[] args) throws ClassNotFoundException
  {
// load the sqlite-JDBC driver using the current class loader
Class.forName("org.sqlite.JDBC");

Connection connection = null;
try
{
  // create a database connection
  connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
  Statement statement = connection.createStatement();
  statement.setQueryTimeout(30);  // set timeout to 30 sec.

  statement.executeUpdate("drop table if exists person");
  statement.executeUpdate("create table person (id integer, name string)");
  statement.executeUpdate("insert into person values(1, 'leo')");
  statement.executeUpdate("insert into person values(2, 'yui')");
  ResultSet rs = statement.executeQuery("select * from person");
  while(rs.next())
  {
    // read the result set
    System.out.println("name = " + rs.getString("name"));
    System.out.println("id = " + rs.getInt("id"));
  }
}
catch(SQLException e)
{
  // if the error message is "out of memory", 
  // it probably means no database file is found
  System.err.println(e.getMessage());
}
finally
{
  try
  {
    if(connection != null)
      connection.close();
  }
  catch(SQLException e)
  {
    // connection close failed.
    System.err.println(e);
      }
    }
  }
}

these are the errors I received when using the xerial jar:

Exception in thread "main" java.lang.NoClassDefFoundError: org/sqlite/NativeDB at java.lang.ClassLoader$NativeLibrary.load(Native Method) at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1939) at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1864) at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1825) at java.lang.Runtime.load0(Runtime.java:792) at java.lang.System.load(System.java:1059) at org.sqlite.SQLiteJDBCLoader.loadNativeLibrary(SQLiteJDBCLoader.java:200) at org.sqlite.SQLiteJDBCLoader.extractAndLoadLibraryFile(SQLiteJDBCLoader.java:148) at org.sqlite.SQLiteJDBCLoader.loadSQLiteNativeLibrary(SQLiteJDBCLoader.java:249) at org.sqlite.SQLiteJDBCLoader.initialize(SQLiteJDBCLoader.java:65) at org.sqlite.core.NativeDB.load(NativeDB.java:53) at org.sqlite.core.CoreConnection.open(CoreConnection.java:136) at org.sqlite.core.CoreConnection.(CoreConnection.java:66) at org.sqlite.jdbc3.JDBC3Connection.(JDBC3Connection.java:21) at org.sqlite.jdbc4.JDBC4Connection.(JDBC4Connection.java:23) at org.sqlite.SQLiteConnection.(SQLiteConnection.java:44) at org.sqlite.JDBC.createConnection(JDBC.java:113) at org.sqlite.JDBC.connect(JDBC.java:87) at java.sql.DriverManager.getConnection(DriverManager.java:579) at java.sql.DriverManager.getConnection(DriverManager.java:243) at SQLiteJDBC.main(SQLiteJDBC.java:18) Caused by: java.lang.ClassNotFoundException: org.sqlite.NativeDB at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) ... 21 more

these are the errors I received without the xerial jar:

Exception in thread "main" java.lang.ClassNotFoundException: org.sqlite.JDBC at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:186) at SQLiteJDBC.main(SQLiteJDBC.java:12)

demuro1
  • 289
  • 1
  • 4
  • 15

1 Answers1

2

Perhaps you already found the answer but just in case. I had the same issue, also with my mac, and I found the solution in this other question. In summary, that jar is not suitable for mac, but you can use sqlite-jdbc-3.7.2.jar from the same site. I hope I have been helpful.

Community
  • 1
  • 1
Raquel
  • 36
  • 2
  • I did eventually get it working but I found a similar solution as the one posed in the link you provided. Thanks for the follow up! – demuro1 May 26 '14 at 20:35