访问保存在类路径中的Microsoft Access数据库 [英] Accessing a Microsoft Access database that is saved in the classpath
问题描述
我正在尝试访问存储在类路径中的数据库.我已经安装了ucanaccess 3.0.0和所有必需的.jars.
I am trying to access a database that is stored in the classpath. I have installed ucanaccess 3.0.0 and all the required .jars.
我的项目层次结构:
:
这是我到目前为止的代码:
Here is the code I have so far:
public void login()
{
Connection conn;
try {
conn = DriverManager.getConnection("jdbc:ucanaccess:/database/theDB.accdb");
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("SELECT Student_Number FROM User");
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这是一个简单的登录屏幕,我只是在单击按钮时测试与数据库的连接.我知道引用绝对文件路径不好,所以我认为将文件放在类路径中会更好.
It's a simple login screen and I'm just testing the connection to the databse on a button click. I understand that referencing an absolute file-path is not good, so I thought having the file in the classpath would be better.
我遇到错误
找不到适用于jdbc:ucanaccess:file:/C:/Users/Gandalf/workspace/FubbleApp/bin/database/theDB.accdb的驱动程序
No suitable driver found for jdbc:ucanaccess:file:/C:/Users/Gandalf/workspace/FubbleApp/bin/database/theDB.accdb
所以我认为它必须是"/database/theDB.accdb",但是我不确定如何解决此问题.
So I think it must be the "/database/theDB.accdb" but I am not sure how to fix this issue.
感谢您的帮助.预先感谢
Any help is appreciated. Thanks in advance
推荐答案
您在连接URL中提供的数据库文件(.accdb或.mdb)的路径必须为
The path to the database file (.accdb or .mdb) that you provide in your connection URL must be either
-
绝对路径,或
an absolute path, or
从当前工作目录开始的相对路径,该路径在您的应用程序运行时有效,在您的情况下,它看起来是"C:/Users/Gandalf/workspace/FubbleApp/bin/".
a relative path from the current working directory that is in effect when your application is running, which in your case appears to be "C:/Users/Gandalf/workspace/FubbleApp/bin/".
如果您希望您的应用程序自动搜索CLASSPATH中的数据库文件,则需要提供自己的代码来执行此操作,或者包括一些第三方代码来为您进行搜索.
If you want your application to automatically search the CLASSPATH for the database file you will need to either provide your own code to do that or include some third-party code to do the search for you.
这篇关于访问保存在类路径中的Microsoft Access数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!