Home >Topics >Access >Access database connection error solution

Access database connection error solution

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-11-01 14:16:185392browse

Access database connection error solution

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驱动

Access database connection error solution

若不存在则需要安装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文件,解析代码:

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

python学习网,大量的免费access数据库教程,欢迎在线学习!

The above is the detailed content of Access database connection error solution. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to delete a subformNext article:How to delete a subform