Java 未使用 Eclipse 连接到 MS Access 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20085965/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:01:03  来源:igfitidea点击:

Java not connecting to MS Access database using Eclipse

javams-accessjdbc

提问by Hazim Ali

Can anyone help me? I've already tried to solve this for one hour and I'm still confused. Below is my code and i get this error when compiling.

谁能帮我?我已经尝试解决这个问题一个小时了,但我仍然感到困惑。以下是我的代码,编译时出现此错误。

Output msg::

输出消息::

DriverLoaded
Could Not Connect to Databasejava.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at DBConnect.<init>(DBConnect.java:11)
    at DBConnect.main(DBConnect.java:21)

Code::

代码::

import java.sql.*;

public class DBConnect {

    public DBConnect() {    
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            System.out.println("DriverLoaded");
            String url = "jdbc:odbc:; DRIVER = Microsoft Access Driver (*.mdb, *.accdb); DBQ = DB.accdb";
            Connection con = DriverManager.getConnection(url);
            System.out.println("Connection Established Successfully");
        } catch(Exception e) {
            e.printStackTrace();
            System.out.println("Could Not Connect to Database");
        }
    }

    public static void main (String args[]) {       
        DBConnect dbcon = new DBConnect();  
    }
}

采纳答案by Paul Samsotha

Overloaded methods for getConnection()

重载方法 getConnection()

1) getConnection( String url, Properties info )

1) getConnection( String url, Properties info )

url - a database url of the form jdbc:subprotocol:subname

info - a list of arbitrary string tag/value pairs as connection arguments; normally at least a "user" and "password" property should be included

url - 形式为 jdbc:subprotocol:subname 的数据库 url

info - 作为连接参数的任意字符串标记/值对列表;通常至少应包括“用户”和“密码”属性

2) getConnection( String url, String user, String password )

2) getConnection( String url, String user, String password )

url - a database url of the form jdbc:subprotocol:subname

user - the database user on whose behalf the Connection is being made

password - the user's password

url - 形式为 jdbc:subprotocol:subname 的数据库 url

user - 代表其建立连接的数据库用户

密码 - 用户的密码

3) getConnection( String url )

3) getConnection( String url )

url - a database url of the form jdbc:subprotocol:subname

url - 形式为 jdbc:subprotocol:subname 的数据库 url

Considering you're using the last constructor, it seems your url syntax in incorrect. I'm not familiar with MS Access, but I'll offer a suggestion I found on another answer.

考虑到您使用的是最后一个构造函数,看来您的 url 语法不正确。我不熟悉 MS Access,但我会提供我在另一个答案中找到的建议。

This is your syntax

这是你的语法

"jdbc:odbc:; DRIVER = Microsoft Access Driver (*.mdb, *.accdb); DBQ = DB.accdb"

A correct syntax I found was

我发现的正确语法是

File f = new File("\\***\***\****\***.accdb");

"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + f.getAbsolutePath();

Looks like you have an unnecessary semicolon after odbc and an extra space. Maybe you want to try the above syntax and see what happens. I'm not sure about the file part, but you may want to look into it if your url still fails after making the semicolon/space fix.

看起来你在 odbc 后面有一个不必要的分号和一个额外的空间。也许你想尝试上面的语法,看看会发生什么。我不确定文件部分,但是如果在修复分号/空格后您的 url 仍然失败,您可能需要查看它。

Check out this question also for more info on another option Connection with username and password

查看此问题,了解有关另一个选项的更多信息使用用户名和密码连接

回答by Balayesu Chilakalapudi

below is the working code for your problem...

以下是您的问题的工作代码...

import java.sql.*;

public class DBConnect {

    public DBConnect() {    
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            System.out.println("DriverLoaded");
            String url = "jdbc:odbc:instance";
            Connection con = DriverManager.getConnection(url);
            System.out.println("Connection Established Successfully");
        } catch(Exception e) {
            e.printStackTrace();
            System.out.println("Could Not Connect to Database");
        }
    }

    public static void main (String args[]) {       
        DBConnect dbcon = new DBConnect();  
    }
}

steps to be followed:

应遵循的步骤:

  1. create an access database DB.accdb in any directory of your windows xp system.
  2. open start > controlpanel > Performance and maintanance > Administrative Tools > Data sources (ODBC) >click System DSN tab > click add > choose Microsoft Access Driver (accdb,mdb) > give the name : instance , (since, getConnection("jdbc:odbc:instance") and click and browse the DB.accdb located in your hard drive ) press ok and restart your command prompt. and run the code again.

    Running the same code in eclipse

  3. create a java project.

  4. add a main class and edit the source of this main class. just copy and paste the about code.
  5. create a user library by adding the jar files from the jdk/bin directory.
  6. link the build path to the project by linking the userlibrary.
  7. run the project.
  1. 在 windows xp 系统的任何目录中创建一个访问数据库 DB.accdb。
  2. 打开开始 > 控制面板 > 性能和维护 > 管理工具 > 数据源 (ODBC) > 单击系统 DSN 选项卡 > 单击添加 > 选择 Microsoft Access 驱动程序(accdb,mdb)> 提供名称: instance ,(因为 getConnection("jdbc: odbc:instance") 并单击并浏览位于硬盘驱动器中的 DB.accdb )按确定并重新启动命令提示符。并再次运行代码。

    在 Eclipse 中运行相同的代码

  3. 创建一个java项目。

  4. 添加一个主类并编辑这个主类的源代码。只需复制并粘贴关于代码。
  5. 通过添加 jdk/bin 目录中的 jar 文件来创建用户库。
  6. 通过链接用户库将构建路径链接到项目。
  7. 运行项目。