简体   繁体   English

如何建立从我的 java 程序到 sqlite3 的连接? 尝试时包错误

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

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? org.sqlite 包怎么不存在? I dont get it, someone help me" :C我不明白,有人帮帮我”:C

SQLiteConfig is a class which is an integral part of the sqlite-jdbc-3.xxjar JDBC driver JAR file. SQLiteConfig是一个类,它是sqlite-jdbc-3.xxjar JDBC 驱动程序 JAR 文件的一个组成部分。 If this is failing then obviously you driver jar file can not be found within the classpath.如果这失败了,那么显然在类路径中找不到驱动程序 jar 文件。 Be sure to add the sqlite-jdbc-3.xxjar file to your project.确保将sqlite-jdbc-3.xxjar文件添加到您的项目中。

If in Netbeans:如果在 Netbeans 中:

  • With your mouse, click once on the project name from the Projects pane (on left of IDE) so that it is highlighted;用鼠标在Projects窗格(IDE 左侧)中单击一次项目名称,使其突出显示;
  • 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 ;在 Categories 窗格(在 Project Properties 窗口的左侧)选择Libraries
  • You should see the sqlite-jdbc-3.xxjar file within the Compile-time Libraries list (xx would of course be the version numbers you want to use).您应该在编译时库列表中看到sqlite-jdbc-3.xxjar文件(xx 当然是您要使用的版本号)。 If the driver JAR file is shown in the list then hit the Cancel button located at the bottom of the Project Properties window.如果驱动程序 JAR 文件显示在列表中,则点击位于“项目属性”窗口底部的“取消”按钮。 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;如果驱动程序 JAR 文件未显示在列表中,则选择位于编译时库列表右侧的添加 Jar/文件夹按钮;
  • An Add Jar/Folder navigation dialog will appear.将出现一个添加 Jar/文件夹导航对话框。 Navigate to where you have stored your copy of the sqlite-jdbc-3.xxjar Driver JAR file and select it then hit the Open button.导航到您存储sqlite-jdbc-3.xxjar驱动程序 JAR 文件副本的位置,然后选择它,然后点击打开按钮。 The Navigation dialog window will close and your selected JAR file will be displayed within the Compile-time Libraries list; Navigation 对话框窗口将关闭,您选择的 JAR 文件将显示在Compile-time Libraries列表中;
  • 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.xxjar driver added to your project.您现在已将sqlite-jdbc-3.xxjar驱动程序添加到您的项目中。 You may need to save your work and close the NetBeans IDE, then reopen it for the SQLite JDBC driver to take affect.您可能需要保存您的工作并关闭 NetBeans IDE,然后重新打开它以使 SQLite JDBC 驱动程序生效。 Test your code.测试你的代码。

I can't help you with other IDE's since I simply don't use them.我无法帮助您使用其他 IDE,因为我根本不使用它们。

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.因为您已成功导入org.sqlite.SQLiteConfig基本上告诉我您的项目中确实包含驱动程序,但是,由于代码是您的老师提供的,因此复制/粘贴将解释导入,但其有效性将被突出显示(或下划线)以某种方式通过 IDE 指示错误。

在 jGRASP 中,使用“设置”>“路径/类路径”>“工作区”(或项目)然后使用“类路径”选项卡将 jar 文件添加到类路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当 jdbc 与 sqlite3 数据库连接时,我该怎么做才能避免“内存不足”的错误? - What do I have to do to avoid error of “out of memory”, when connection by jdbc with sqlite3 database? Java中的SQLite3 - 如何确定我的连接是否有效并且表是否存在? - SQLite3 in Java - How can I determine if my connection is valid and a table exists? 当它包含一个外部Java程序包时,如何在命令行上编译我的主Java程序? - How do I compile my main Java program on the command line when it includes an external java package? 如何使用Java建立与服务器的连接? - How do you establish a connection to a server in Java? 我的Java程序如何能够连续使用MATLAB,而不必每次都建立连接? - How can my Java program use MATLAB continuously without having to establish a connection everytime? Java SQLite3连接失败 - Java SQLite3 Connection Failing 如何为Ubuntu打包Java程序? - How do I package a Java program for Ubuntu? Java Mosquitto建立连接错误 - Java Mosquitto establish connection error 当我停止我的程序时,如何防止我的 java 应用程序丢弃未确认的 RabbitMQ 消息? - How do I prevent my java application from discarding unacknowledged RabbitMQ messages when I stop my program? 尝试从Java客户端建立SSL连接时出错 - Error trying to establish SSL connection from java client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM