package com.utils;

import org.apache.log4j.Logger;

import java.sql.*;
import java.util.*;
import java.util.Date;


public class FoxproUtil {


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

    public static List<Map<String,String>> getFoxProData(String filepath){
        List<Map<String,String>> resultList = new ArrayList<Map<String,String>>();
        Connection con =null;
        Statement stmt =null;
        ResultSet rs =null;
        try{
            String DB_URL = "jdbc:odbc:Driver={Microsoft FoxPro VFP Driver (*.dbf)};" +
                    "SourceType=DBF;" +			//此处指定解析文件的后缀
                    "SourceDB="+filepath.substring(0,filepath.lastIndexOf("\\")); 	//此处为dbf文件所在的目录

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Properties p = new Properties();
            p.put("charSet", "GBK");
            con = DriverManager.getConnection(DB_URL,p);

            stmt = con.createStatement();
            // 查找数据
            rs = stmt.executeQuery("select * from foxpro ");

            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("读者类型").trim();

                readerMap.put("读者类型",temp==null || "".equals(temp) ?"学生":temp);
                readerMap.put("读者证号",rs.getString("读者证号").trim());
                readerMap.put("姓名",rs.getString("姓名").trim());
                readerMap.put("性别",rs.getString("性别").trim());
                readerMap.put("所属部门/班级",rs.getString("部门").trim());
                readerMap.put("借阅权限",rs.getString("借阅权限").trim());
                temp = rs.getString("失效日期").trim();
                readerMap.put("失效日期",temp==null || "".equals(temp) ?losedate:temp);
                readerMap.put("有效证件",rs.getString("有效证件").trim());
                readerMap.put("证件号码",rs.getString("证件号码").trim());
                resultList.add(readerMap);
            }
        }catch (Exception e){
            e.printStackTrace();
            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(getFoxProData("E:\\"));
    }
}
