java 操作access文件

做项目要导入导出mdb文件,百度了一下,写了自己的工具类

用到jar包Access_JDBC30.jar,下载路径http://download.csdn.net/download/qq_24190041/9951300

/**
* 获取数据库链接
* @param mdbPath    文件路径
* @return
*/
private static Connection getConn(String mdbPath) {
Properties prop = new Properties();  
    prop.put("charSet", "UTF-8");  
    String dbUr1 = "jdbc:Access:///" + mdbPath; 
    String driver = "com.hxtt.sql.access.AccessDriver";
   
    Connection conn = null;
    try {
        Class.forName(driver); //classLoader,加载对应驱动
        conn = DriverManager.getConnection(dbUr1, prop);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return conn;
}


/**
     * 建表
     * @param mdbPath        数据库文件路径
     * @param types           类型
     * @throws SQLException
     */
    public static void createTable(String mdbPath,String... types) throws SQLException {
    Connection conn = getConn(mdbPath); 
    for(String type : types){

               //根据类型获取建表语句
    String sql = AccessTableCreateSqlUtils.getSql(type);
    if(sql!= null){
    Statement stmt = conn.createStatement(
    ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    stmt.execute(sql);
    }
    }
    conn.close();
    }
     /**
     * 获取查询数据
     * @param mdbPath    文件路径
     * @param sql        查询语句
     * @param column     查询的列
     * @return
     * @throws Exception
     */
    public static List> resolverMdb(String mdbPath,  
            String sql, Object... column) throws Exception {  
        List> entityList = new ArrayList<>();  
        if (mdbPath.isEmpty() || sql.isEmpty() || column.length < 1) {  
            throw new Exception("mdb文件路径不能为空或者SQL语句不能为空,并且列的长度不能小于1");  
        }  
        
        try (   Connection conn = getConn(mdbPath);  
                Statement statement = conn.createStatement();  
                ResultSet result = statement.executeQuery(sql)) {  
            Map mapList = null;  
            while (result.next()) {  
                mapList = new HashMap<>();  
                for (Object col : column) {  
                    mapList.put((String) col, result.getString((String) col));  
                }  
                entityList.add(mapList);  
            }  
            conn.close();
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return entityList;  
    }  

/**
* 获取所有表数据
* @param mdbPath    文件路径
* @return Map>> Map<表名,List<表数据>>
*/
    public static Map>> resolverMdb(String mdbPath)  {  
    Map>> entityMap = new HashMap>>();
    Connection conn = null;
    Statement stmt = null;
        ResultSet rs = null;
        String tableName = null; 
        try {  
            conn = getConn(mdbPath);  

            //查询所有的表
            ResultSet tables = conn.getMetaData().getTables(  
            conn.getCatalog(), null, null,  
                     new String[] { "TABLE" });
            stmt = (Statement) conn.createStatement();  
            while(tables.next()){
            // 获取表名  
            tableName = tables.getString("TABLE_NAME");// getXXX can only be used once  
            rs = stmt.executeQuery("select * from " + tableName);  
            // 读取表的内容  
            ResultSetMetaData data = rs.getMetaData();  
            List> entityList = new ArrayList<>();
            while (rs.next()) {  
            Map map = new HashMap();
            for (int i = 0; i < data.getColumnCount(); i++) {  
            String columnName = data.getColumnName(i + 1);  
            map.put(columnName, rs.getString(i + 1));
//            System.out.print(columnName +":"+ rs.getString(i + 1) + "    ");  
            }  
            entityList.add(map);
//            System.out.println();  
            }  
            entityMap.put(tableName, entityList);
            }
        } catch (Exception e) { 
        logger.error(e.getMessage());
            e.printStackTrace();  
        } finally{
        try {
stmt.close();
conn.close();
} catch (SQLException e) {
logger.error(e.getMessage());
e.printStackTrace();
}  
        }
        return entityMap;  
    }  

你可能感兴趣的:(java操作Access)