Netbeans中用于SQLite的JDBC:找不到合适的驱动程序 [英] JDBC for SQLite in Netbeans: No suitable driver found
问题描述
我需要将数据从SQLite文件加载到我在Netbeans中开发的Java程序中. 该文件将通过摆动菜单项加载.我正在使用 sqlitejdbc 作为驱动程序.
I need to load data from an SQLite file into a java program which I develop in Netbeans. The file is to be loaded via a swing menu item. I'm using sqlitejdbc as driver.
以下是我认为很重要的代码块:
Here are the code blocks I assume to be important:
// header stuff
package aufgabe_9;
import java.sql.*;
//...
// menu item opening the file
private void mitemOpenFileActionPerformed(java.awt.event.ActionEvent evt)
{
/**
* Handles the dialogue for selecting and loading file.
*/
JFileChooser fileChoose = new JFileChooser();
fileChoose.showOpenDialog(this); //'this' calls the current object
//Load the sql file
try {
String filePath = fileChoose.getSelectedFile().toString();
Connection conn = DriverManager.getConnection("jdbc:sqlite:" +
filePath);
//Close the connection
if (conn != null)
conn.close();
}
catch (SQLException e){System.err.println("Database problem: " + e);}
}
}
//...
运行程序并通过菜单加载文件时,出现以下错误:
When running the program and loading a file via the menu, I get the following error:
java.sql.SQLException: No suitable driver found for jdbc:sqlite:/home/levent
/temp/A9AProbeflaeche.db
阅读相应的stackexchange帖子后,我了解到 问题可能是由于(1)格式错误的文件URL或(2)驱动程序不正确 已加载.这里是一些进一步的信息:
After reading the respective stackexchange posts, I understand that this problem can be caused by (1) a malformed file URL or (2) the driver not being loaded. Here's some further information:
- I added the sqlitejdbc-3.7.2.jar to the library classpath via Tools --> Libraries as well as to the project libraries via Window --> Projects.
- I also checked the Classpath by using this function. It contains the path to the jdbc jar-file just as expected.
- I can connect to the database via the Services menu without any problems, so I can assume the URL to be correct, as well as sqlite running on my system.
- Some OS information: I'm running Netbeans 8.0 on 64 bit ARCH Linux 3.12.9-2.
有人可以告诉我我在这里想念的东西吗?任何帮助表示赞赏!
Can anybody tell me what I'm missing here? Any help appreciated!
问题已解决 这是对我有用的代码:
Problem solved Here is the code that works for me:
//...
private void mitemOpenFileActionPerformed(java.awt.event.ActionEvent evt)
{
/**
* Handles the dialogue for selecting and loading file.
*/
JFileChooser fileChoose = new JFileChooser();
fileChoose.showOpenDialog(this);
//Load the sql file
try {
//Get file path
String filePath = fileChoose.getSelectedFile().toString();
//Open connection
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:" + filePath);
//Do stuff...
//Close the connection
conn.close();
}
//"Multicatch":
catch (SQLException | ClassNotFoundException e) {
System.err.println("Database problem: " + e);
}
//...
推荐答案
您可能需要加载驱动程序类,以便它使用以下代码将其自身注册到DriverManager: Class.forName("org.sqlite.JDBC");
You probably need to load the driver class so that it registers itself to the DriverManager using the following code: Class.forName("org.sqlite.JDBC");
注意:这只需要在您的应用程序中调用一次.
Note: this only needs to be called once in your application.
这是Java包含ServiceLoader API之前的标准过程,现在DriverManager使用该API注册它在类路径中找到的驱动程序,但是驱动程序需要声明一个名为java.sql.Driver的文件,其中包含以下名称: jar的META-INF \ services目录中的驱动程序类.
This was a standard procedure before the Java included the ServiceLoader API, now the DriverManager uses that API to register the drivers it finds in the classpath, but the drivers need to declare a file named java.sql.Driver containing the name of the driver class in the directory META-INF\services of their jar.
这篇关于Netbeans中用于SQLite的JDBC:找不到合适的驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!