一个典型的用access数据库访问的类

package DWR;


import java.io.File;
import java.net.URLDecoder;
import java.sql.* ;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-8-10
 * Time: 下午2:46
 */
public class DBclass {
   // public static void main(String args[]) throws SQLException {
    public List getResult(Map formMap) throws SQLException {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        List list = new ArrayList();
        try{
            String str =Thread.currentThread().getContextClassLoader().getResource("") + "";
            String strpath = str.substring(6, str.lastIndexOf("WEB-INF"))  ;
            strpath=URLDecoder.decode(strpath, "UTF-8") ;
            System.out.print(strpath);
            // 路径示例 file:/F:/TestOut/web/WEB-INF/classes/
            strpath  = strpath+"CORBA_DB/" ;
            strpath = strpath + getDBfilename(strpath);
            strpath =  strpath+".mdb" ;
            //System.out.print(strpath);
            Class.forName("net.sf.log4jdbc.DriverSpy");
            String strDBQ = "DBQ="+strpath;
            String dburl ="jdbc:log4jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};";//此为NO-DSN方式
            dburl = dburl+ strDBQ;
            //String dburl ="jdbc:odbc:odbcName";//此为ODBC连接方式
            conn = DriverManager.getConnection(dburl);
            stmt=conn.createStatement();


            String sqls = formMap.get("sqls").toString();
            rs = stmt.executeQuery(sqls);
            ResultSetMetaData data=rs.getMetaData();
            while(rs.next()){
                Map map = new HashMap();
                            for(int i = 1 ; i<= data.getColumnCount() ; i++){
                             //获得指定列的列名
                            String columnName = data.getColumnName(i);
                            //获得指定列的列值
                            String columnValue = rs.getString(i);
                            map.put(columnName,columnValue);
                    }
                list.add(map);

            }
        } catch (Exception e) {
          System.out.print(e.getMessage());
           list=null;
        }finally {
            try{
                if(rs != null) {
                    rs.close();
                }
                if(stmt != null) {
                    stmt.close();
                }
                if(conn!=null){
                    conn.close();
                }
            } catch (Exception e){
                e.printStackTrace();
            }
        }
        return list;
     }

 

 


    public String getDBfilename(String PATH){
        Set<String> fileSet = new HashSet<String>();
        File[] files = new File(PATH).listFiles();
        String str ="";
        for (File file : files) {
            // 由于Set的元素是不重复的,不用做重复判断,直接放入
            fileSet.add(getPrefixFileName(file.getName()));
        }
        // 由于Set允许null,所以要去除
        fileSet.remove(null);
        //迭代的方法
        Iterator it = fileSet.iterator();
        //这个set是否有下一个
        while(it.hasNext()){
            //有就把下一个取出来
            str = (String) it.next();
        }
        return  str  ;

    }
    public static String getPrefixFileName(String fileName) {
        if (fileName == null || fileName.length() == 0) {
            return null;
        }
        int index = fileName.indexOf(".");
        if (index < 0) {
            // 不含SEPARATOR的文件名
            return null;
        }
        // 从文件名开头到SEPARATOR的位置,截取
        return fileName.substring(0, index);
    }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值