怎样从我的Java程序建立到sqlite3的连接?尝试时出现的包错误。

huangapple go评论66阅读模式
英文:

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

问题

当我尝试连接我的数据库时,它抛出了以下错误:

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

来自以下代码:

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);
      }
      
      
   }
   
}

这段代码是我老师为我们的期末作业提供的,我想知道为什么他会包含一个不存在的包呢?为什么org.sqlite包不存在?我不明白,有人可以帮帮我吗?:C

英文:

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

答案1

得分: 1

SQLiteConfigsqlite-jdbc-3.x.x.jar JDBC 驱动程序 JAR 文件的一个关键部分的类。如果出现问题,那么显然在类路径中找不到驱动程序的 jar 文件。请确保将 sqlite-jdbc-3.x.x.jar 文件添加到您的项目中。

如果使用 Netbeans:

  • 使用鼠标,在项目窗格(位于 IDE 左侧)中单击项目名称一次,使其高亮显示;
  • 右键单击,从弹出菜单中选择属性选项(位于底部)。将会出现项目属性对话框窗口;
  • 类别窗格(位于项目属性窗口左侧)中选择
  • 您应该在编译时库列表中看到sqlite-jdbc-3.x.x.jar文件(x.x 当然是您想要使用的版本号)。如果驱动程序 JAR 文件显示在列表中,则点击位于项目属性窗口底部的<kbd>取消</kbd>按钮。这表示您的项目中已包含驱动程序。
  • 如果驱动程序 JAR 文件未显示在列表中,则选择位于编译时库列表右侧的<kbd>添加 Jar/文件夹</kbd>按钮;
  • 将出现一个添加 Jar/文件夹导航对话框。导航到存储sqlite-jdbc-3.x.x.jar驱动程序 JAR 文件的位置,然后选择它,然后点击<kbd>打开</kbd>按钮。导航对话框窗口将关闭,并在编译时库列表中显示您选择的 JAR 文件;
  • 选择位于项目属性窗口底部的<kbd>确定</kbd>按钮。项目属性窗口将关闭。
  • 现在您已经将sqlite-jdbc-3.x.x.jar驱动程序添加到项目中。您可能需要保存工作并关闭 NetBeans IDE,然后重新打开它,以使 SQLite JDBC 驱动程序生效。测试您的代码。

对于其他 IDE,我无法提供帮助,因为我根本不使用它们。

您提供的代码对我有效。因为您成功导入了org.sqlite.SQLiteConfig,这基本上告诉我您确实在项目中包含了驱动程序,但由于代码是由您的老师提供的,所以复制/粘贴可能会导致导入语句存在问题,IDE 可能会以某种方式标示出错误。

英文:

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 <kbd> Cancel </kbd> 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 <kbd>
    Add Jar/Folder </kbd> 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 <kbd> Open </kbd> button.
    The Navigation dialog window will close and your selected JAR file will be
    displayed within the Compile-time Libraries list;
  • Select the <kbd> OK </kbd> 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.

答案2

得分: 0

在 jGRASP 中,使用 "Settings" > "PATH / CLASSPATH" > "Workspace"(或项目),然后切换到 "CLASSPATHS" 选项卡,将 jar 文件添加到类路径中。

英文:

In jGRASP, use "Settings" > "PATH / CLASSPATH" > "Workspace" (or project) then the "CLASSPATHS" tab to add the jar file to the classpath.

huangapple
  • 本文由 发表于 2020年3月16日 02:12:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/60696111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定