首页 > 其他专区 > Access >

Access数据库连接错误解决办法

Access 2021-06-25

 

64位Windows系统连接Access数据库,程序中可能需要修改Access数据库连接:

32位:String strUrl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=c://demo.mdb"
 

64位:String strUrl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=c://demo.mdb"
 

修改后仍报错则进入“控制面板”-》“管理工具”-》“数据源(ODBC)”查看系统是否存在Access驱动

1572588410991980.png

若不存在则需要安装Microsoft Access驱动程序:

官方:http://www.microsoft.com/zh-cn/download/confirmation.aspx?id=13255
 

32位:http://download.microsoft.com/download/E/4/2/E4220252-5FAE-4F0A-B1B9-0B48B5FBCCF9/

AccessDatabaseEngine.exe

64位:http://download.microsoft.com/download/E/4/2/E4220252-5FAE-4F0A-B1B9-0B48B5FBCCF9/

AccessDatabaseEngine_X64.exe

下面是连接access的.mdb文件,解析代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

package test;

import java.io.File;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.Statement;

import java.util.Properties;

public class Test {

        /**

     * TODO : 读取文件access

     

     * @param filePath

     * @return

     * @throws ClassNotFoundException

     */ 

    public static void readFileACCESS(File mdbFile) { 

        Properties prop = new Properties(); 

        prop.put("charSet", "gb2312"); // 这里是解决中文乱码 

        prop.put("user", ""); 

        prop.put("password", ""); 

        //String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=" + mdbFile.

        getAbsolutePath(); 

        String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+ mdbFile.

        getAbsolutePath(); 

        Statement stmt = null; 

        ResultSet rs = null; 

        String tableName = null; 

        try

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 

            // 连接到mdb文件 

            Connection conn = DriverManager.getConnection(url, prop); 

            ResultSet tables = conn.getMetaData().getTables( 

                    mdbFile.getAbsolutePath(), null, null, 

                    new String[] { "TABLE" }); 

            // 获取第一个表名 

            if (tables.next()) { 

                tableName = tables.getString(3);// getXXX can only be used once 

            } else

                return

            

            stmt = (Statement) conn.createStatement(); 

            // 读取第一个表的内容 

            rs = stmt.executeQuery("select * from " + tableName); 

            ResultSetMetaData data = rs.getMetaData(); 

            while (rs.next()) { 

                for (int i = 1; i <= data.getColumnCount(); i++) { 

                    System.out.print(rs.getString(i) + "    "); 

                

                System.out.println(); 

            

        } catch (Exception e) { 

            e.printStackTrace(); 

        

    

  

    public static void main(String[] args) { 

        readFileACCESS(new File("C:\\Users\\Ninemax\\Desktop\\西太区医学索引.mdb")); 

    

}



Copyright © 2016-2023 office学习教程网 office.tqzw.net.cn. All Rights Reserved.