Java无法使用Eclipse连接到MS Access数据库 [英] Java not connecting to MS Access database using Eclipse

查看:228
本文介绍了Java无法使用Eclipse连接到MS Access数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我吗?我已经尝试解决了一个小时,但我仍然感到困惑. 下面是我的代码,编译时出现此错误.

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.

输出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)

代码::

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();  
    }
}

推荐答案

getConnection()

1) getConnection( String url, Properties info )

1) getConnection( String url, Properties info )

url- 格式为jdbc:subprotocol:subname

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

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

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

url-格式为jdbc:subprotocol:subname

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

3) getConnection( String url )

url- 格式为jdbc:subprotocol:subname

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

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

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.

这是你的语法

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

我发现了正确的语法

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

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

好像您在odbc之后有多余的分号和多余的空格.也许您想尝试上面的语法,看看会发生什么.我不确定文件部分,但是如果在进行分号/空格修复后您的URL仍然失败,您可能需要调查一下.

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.

还请查看此问题,以获取有关另一个选项的更多信息与用户名和密码

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

这篇关于Java无法使用Eclipse连接到MS Access数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆