Java Netbeans中SQLite的JDBC:未找到合适的驱动程序
我需要将数据从SQLite文件加载到我在Netbeans中开发的java程序中。 该文件将通过swing菜单项加载。我在开车 以下是我认为很重要的代码块:Java Netbeans中SQLite的JDBC:未找到合适的驱动程序,java,sqlite,netbeans,jdbc,sqlexception,Java,Sqlite,Netbeans,Jdbc,Sqlexception,我需要将数据从SQLite文件加载到我在Netbeans中开发的java程序中。 该文件将通过swing菜单项加载。我在开车 以下是我认为很重要的代码块: // header stuff package aufgabe_9; import java.sql.*; //... // menu item opening the file private void mitemOpenFileActionPerformed(java.awt.event.ActionEvent evt) {
// 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);}
}
}
//...
运行程序并通过菜单加载文件时,出现以下错误:
java.sql.SQLException: No suitable driver found for jdbc:sqlite:/home/levent
/temp/A9AProbeflaeche.db
在阅读了各自的stackexchange帖子后,我了解到
问题可能是由于(1)文件URL格式错误或(2)驱动程序未被激活
加载。以下是一些进一步的信息:
- 我通过工具-->库将sqlitejdbc-3.7.2.jar添加到库类路径中,并通过窗口-->项目将其添加到项目库中
- 我还使用检查了类路径。它包含JDBCJAR文件的路径,正如预期的那样
- 我可以通过服务菜单连接到数据库,没有任何问题,因此我可以假设URL是正确的,并且sqlite在我的系统上运行
- 一些操作系统信息:我正在64位ARCH Linux 3.12.9-2上运行Netbeans 8.0
//...
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”) 注意:在应用程序中只需调用一次
在Java包含ServiceLoader API之前,这是一个标准过程,现在DriverManager使用该API注册在类路径中找到的驱动程序,但是驱动程序需要声明一个名为Java.sql.Driver的文件,该文件包含其jar的META-INF\services目录中的驱动程序类名称。,谢谢!实际上,我在不同的帖子中读到了
Class.forName()
语句,但当时仍然出现错误,可能是因为url格式错误。此外,我认为它已经过时,因为DriverManager
正在处理它。它现在工作得很好。