使用 NetBeans 连接到 Java 中的 Access 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20007353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connect to an Access database in Java using NetBeans
提问by Kaoru
How do I connect to an Access database in Java?
如何连接到 Java 中的 Access 数据库?
I have done like this:
我这样做了:
package inspection.management.system;
import java.sql.*;
/**
*
* @author Fuhans
*/
public class Database
{
public static void DatabaseConnectivity()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + "d:\program files\project\program\inspection management system\db1.accdb";
Connection conn = DriverManager.getConnection(url);
System.out.println("Connection Successful");
InfoBox.ShowMessageBox("Connection Successful!", "Success");
}
catch (Exception e)
{
System.err.println("Got an exception!");
System.err.println(e.getMessage());
InfoBox.ShowMessageBox("Got an Exception!", "Error");
InfoBox.ShowMessageBox(e.getMessage(), "Error");
}
}
}
if (_textField1.equals("Fuhans") && _passwordField1.equals("Xavega"))
{
Sound.PlaySound(1);
InfoBox.ShowMessageBox("Successfully Login!", "Success");
Database.DatabaseConnectivity();
}
When i successfully login, it gave me error on database:
当我成功登录时,它给了我数据库错误:
What have i done wrong?
我做错了什么?
回答by Ysr Shk
You should create a DSN(Data Source Name) first.
您应该先创建一个 DSN(数据源名称)。
In control panel, if there is no drivers for accessing even though you have installed then there is a possibility that you may not getting odbcad32.exe file path. Choose your path from this, and then right click Data Sources(ODBC)[where you are creating DSN], and paste one of following path there.
在控制面板中,如果您安装了驱动程序仍然无法访问,那么您可能无法获取 odbcad32.exe 文件路径。从中选择您的路径,然后右键单击数据源(ODBC)[您正在创建 DSN 的位置],并将以下路径之一粘贴到那里。
The 32-bit version of the Odbcad32.exe file is located at:
%WinDir%\Windows\SysWoW64
The 64-bit version of the Odbcad32.exe file is located at:
%WinDir%\Windows\System32
32 位版本的 Odbcad32.exe 文件位于:
%WinDir%\Windows\SysWoW64
64 位版本的 Odbcad32.exe 文件位于:
%WinDir%\Windows\System32
and while accessing, do like this:
在访问时,请执行以下操作:
String url = "jdbc:odbc:dsn_name";
Connection conn = DriverManager.getConnection(url);
回答by Mr J
change the statement:
更改语句:
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + "d:\program files\project\program\inspection management system\db1.accdb";
to:
到:
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + "C://program files//project//program//inspection management system//db1.accdb";
回答by Gord Thompson
in the ODBC Administrator app, i don't have Ms access Driver, i just have SQL Driver.
在 ODBC 管理员应用程序中,我没有 Ms 访问驱动程序,我只有 SQL 驱动程序。
Now that the JDBC-ODBC Bridge has been removed from Java (since Java 8) you should consider using the UCanAccessJDBC driver. It is a pure Java implementation so it works on non-Windows platforms, too.
现在 JDBC-ODBC Bridge 已从 Java 中删除(自 Java 8 起),您应该考虑使用UCanAccessJDBC 驱动程序。它是一个纯 Java 实现,因此它也适用于非 Windows 平台。
For more information see
有关更多信息,请参阅