Access MDB文件解析查询,Access数据库解析工具类MdbUtils

================================

©Copyright 蕃薯耀 2020-01-07

https://www.cnblogs.com/fanshuyao/

 

 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class MdbUtils {

    private final static String JDBC_DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
    private final static String JDBC_URL = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=";
    
    /**
     * 查询mdb文件的表数据
     * @param absoluteFilePath mdb文件绝对路径
     * @param sql 查询的sql语句
     * @return
     */
    public static List> read(String absoluteFilePath, String sql){
        
        List> listMap = new ArrayList>();
        Properties prop = new Properties();
        prop.put("charset", "utf-8");//解决中文乱码?没有也行(GB2312/GBK)
        //prop.put("user", "");
        //prop.put("password", "");
        
        String url = JDBC_URL + absoluteFilePath;
        //PreparedStatement preparedStatement = null;
        Statement statement = null;
        ResultSet resultSet = null;
        Connection connection = null;
        try{
            Class.forName(JDBC_DRIVER);
            connection = DriverManager.getConnection(url, prop);
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
            
            while(resultSet.next()){
                Map map = new HashMap();
                for(int i=1; i<= resultSetMetaData.getColumnCount(); i++){
                    String columnName = resultSetMetaData.getColumnName(i);//列名
                    Object columnValue = resultSet.getObject(i);
                    map.put(columnName, columnValue);
                }
                listMap.add(map);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                if(statement != null){
                    statement.close();
                }
                if(connection != null){
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return listMap;
    }
    
    public static void main(String[] args) {
        String sql = "select * from cu_proj_zxgh_land";
        //List> listMap = read("C:/db/test.mdb", sql);
        List> listMap = read("C:/db/02-地块划分与指标控制图.mdb", sql);
        if(listMap != null && listMap.size() > 0){
            System.out.println("=====listMap.size()="+listMap.size());
            for (Map map : listMap) {
                System.out.println(map.toString());
                System.out.println("");
            }
        }
    }
    
    
}

 

 

(如果你觉得文章对你有帮助,欢迎捐赠,^_^,谢谢!) 

Access MDB文件解析查询,Access数据库解析工具类MdbUtils_第1张图片

 

================================

©Copyright 蕃薯耀 2020-01-07

https://www.cnblogs.com/fanshuyao/

 

你可能感兴趣的:(Access MDB文件解析查询,Access数据库解析工具类MdbUtils)