【发布时间】:2014-08-12 18:22:33
【问题描述】:
我需要将 SQLite 文件中的数据加载到我在 Netbeans 中开发的 java 程序中。 该文件将通过摆动菜单项加载。我使用sqlitejdbc 作为驱动程序。
以下是我认为很重要的代码块:
// 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) 驱动程序没有 加载。以下是一些进一步的信息:
- 我通过 Tools --> Libraries 将 sqlitejdbc-3.7.2.jar 添加到库类路径中,并通过 Window --> Projects 将 sqlitejdbc-3.7.2.jar 添加到项目库中.
- 我还使用this function 检查了类路径。它包含 jdbc jar 文件的路径,正如预期的那样。
- 我可以通过 Services 菜单毫无问题地连接到数据库,因此我可以假设 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);
}
//...
【问题讨论】:
标签: java sqlite netbeans jdbc sqlexception