How do I establish a connection from my java program to sqlite3? Package error when attempting

Zerenity :

When I try to connect to my database; it throws me these error:

PersonTidbok.java:3: error: package org.sqlite does not exist
import org.sqlite.SQLiteConfig;
                 ^
PersonTidbok.java:18: error: cannot find symbol
         SQLiteConfig config = new SQLiteConfig();  
         ^
  symbol:   class SQLiteConfig
  location: class PersonTidbok

PersonTidbok.java:18: error: cannot find symbol
         SQLiteConfig config = new SQLiteConfig();  
                                   ^
  symbol:   class SQLiteConfig
  location: class PersonTidbok
3 errors

From the following code:

import java.io.*;
import java.sql.*;
import org.sqlite.SQLiteConfig;

public class PersonTidbok {

   public static final String DB_URL = "jdbc:sqlite:C:/Users/zeren/SQlite/slutuppgift";   

   public static final String DRIVER = "org.sqlite.JDBC";  

   public static void main(String[] args) throws IOException {
      Connection conn = null;

      try {
         Class.forName(DRIVER);
         SQLiteConfig config = new SQLiteConfig();  
         config.enforceForeignKeys(true); 
         conn = DriverManager.getConnection(DB_URL,config.toProperties());  
      } catch (Exception e) {

         System.out.println( e.toString() );
         System.exit(0);
      }


   }

}

The code was given from my teacher for our final assigment, and im wondering why he'd include a package that doesnt exist somehow? How doesnt package org.sqlite not exist? I dont get it, someone help me" :C

DevilsHnd :

SQLiteConfig is a class which is an integral part of the sqlite-jdbc-3.x.x.jar JDBC driver JAR file. If this is failing then obviously you driver jar file can not be found within the classpath. Be sure to add the sqlite-jdbc-3.x.x.jar file to your project.

If in Netbeans:

  • With your mouse, click once on the project name from the Projects pane (on left of IDE) so that it is highlighted;
  • Righ-click and select the Properties option from the popup menu that appears (at bottom). The Project Properties dialog window will appear;
  • In the Categories pane (on left side of the Project Properties window) select Libraries;
  • You should see the sqlite-jdbc-3.x.x.jar file within the Compile-time Libraries list (x.x would of course be the version numbers you want to use). If the driver JAR file is shown in the list then hit the Cancel button located at the bottom of the Project Properties window. You do have the driver as part of your project.
  • If the driver JAR file is not shown in the list then select the Add Jar/Folder button located on the right side of the Compile-time Libraries list;
  • An Add Jar/Folder navigation dialog will appear. Navigate to where you have stored your copy of the sqlite-jdbc-3.x.x.jar Driver JAR file and select it then hit the Open button. The Navigation dialog window will close and your selected JAR file will be displayed within the Compile-time Libraries list;
  • Select the OK button located on the bottom of the Project Properties window. The Project Properties window will close.
  • You now have the sqlite-jdbc-3.x.x.jar driver added to your project. You may need to save your work and close the NetBeans IDE, then reopen it for the SQLite JDBC driver to take affect. Test your code.

I can't help you with other IDE's since I simply don't use them.

The code you provided works for me. Because you have managed to import org.sqlite.SQLiteConfig basically tells me that you do have the driver included within your project but, since the code was given by your teacher then a copy/paste would account for the import but its validity would be highlighted (or underlined) in some fashion via the IDE indicating an error.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=291580&siteId=1