package com.utils;


import com.sun.org.apache.xerces.internal.xs.StringList;
import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;

public class AccessUtil {

    static Logger log = Logger.getLogger(AccessUtil.class);


    public static List<Map<String,String>> getAccessData(String filepath){
        List<Map<String,String>> resultList = new ArrayList<Map<String,String>>();
        Connection con =null;
        Statement stmt =null;
        ResultSet rs =null;
        try{
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");// 这个驱动的地址不要改
            con = DriverManager.getConnection("jdbc:ucanaccess://"+filepath);
            stmt = con.createStatement();
            // 查找数据
            rs = stmt.executeQuery("select * from 借书证 ");

            String losedate =DateUtils.getSimpleDateFormat("yyyy-MM-dd").format(new Date());

            while (rs.next()) {
                Map<String,String> readerMap=new HashMap<String, String>();
                String temp = rs.getString("读者类型");

                readerMap.put("读者类型",temp==null || "".equals(temp) ?"学生":temp);
                readerMap.put("读者证号",rs.getString("读者证号"));
                readerMap.put("姓名",rs.getString("姓名"));
                readerMap.put("性别",rs.getString("性别"));
                readerMap.put("所属部门/班级",rs.getString("所属部门/班级"));
                readerMap.put("借阅权限",rs.getString("借阅权限"));
                temp = rs.getString("失效日期");
                readerMap.put("失效日期",temp==null || "".equals(temp) ?losedate:temp);
                readerMap.put("有效证件",rs.getString("有效证件"));
                readerMap.put("证件号码",rs.getString("证件号码"));
                resultList.add(readerMap);
            }

        }catch (Exception e){
            log.error(e,e);
        } finally{
            try{
                rs.close();
                stmt.close();
                con.close();
            }catch (Exception e){
                log.error(e,e);
            }
        }
        return resultList;
    }

    /**
     * 测试
     * */
    public static void main(String[] args) throws  Exception{
        System.out.println(getAccessData("C:\\access.mdb"));
    }
}
