⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rsutil.java~1~

📁 超值周末现场管理软件,用Excel存储数据
💻 JAVA~1~
字号:
package org.leifeng.dbutil;import java.util.*;import java.sql.*;/** * * <p>Title: RSUtil</p> * <p>Description: 将ResultSet包装成Java类集</p> * <p>Copyright: Copyright (c) 2006</p> * <p>Company: </p> * @author not attributable * @version 1.0 */public class RSUtil {  /**   * 将ResultSet包装成Map数组,每一个Map的 键/值 即 字段名/字段值   * @param rs ResultSet   * @return Map数组(List类),若ResultSet为空,则返回size为0的List   * @throws SQLException   */  public static List toMaps(ResultSet rs)throws SQLException{    List list = new ArrayList();    ResultSetMetaData rsmd = rs.getMetaData();    int columnCount = rsmd.getColumnCount();    String [] columns = new String[columnCount];    for(int i = 1; i <= columnCount; i++){        columns[i - 1] = rsmd.getColumnName(i);    }    while(rs.next()){      Map map = new HashMap();      for(int i = 1; i <= columnCount; i++){        Object columnValue = rs.getObject(i);        map.put(columns[i - 1],columnValue);      }      list.add(map);    }    return list;  }  /**   * 将ResultSet直接转换为二维数组   * @param rs ResultSet   * @return 二维数组(List类,数组中的每一行也是List类),若ResultSet为空,则返回size为0的List   * @throws SQLException   */  public static List toValueList(ResultSet rs) throws SQLException{    List list = new ArrayList();    ResultSetMetaData rsmd = rs.getMetaData();    int columnCount = rsmd.getColumnCount();    while(rs.next()){      List row = new ArrayList();      for(int i = 1; i <= columnCount; i++){        Object columnValue = rs.getObject(i);        row.add(columnValue);      }      list.add(row);    }    return list;  }  /**   * 将RSUtil.toMaps()得到的Map数组转换为“字段名/列值”的Map。这个Map的键为字段名,值为该字段名对应的一整列值(List)   * @param maps RSUtil.toMaps()得到的Map数组,不能为NULL,但可以size为0   * @return “字段名/列值”的Map。如果输入的Map数组的size为0,则返回size为0的Map   */  public static Map mapsToColumnsMap(List maps){    Map columnsMap = new HashMap();     //欲返回的值    List columnsList = new ArrayList(); //字段名数组    if(maps.size() > 0){      //先生成字段名数组      Map firstRow = (Map)maps.get(0);      columnsList.addAll(firstRow.keySet());      //列数组      List[] values = new List[columnsList.size()];      for(int l = 0; l < values.length; l++){        values[l] = new ArrayList();      }      for(int i = 0; i < maps.size(); i++){        Map row = (Map) maps.get(i);        for(int j = 0; j < columnsList.size(); j++){          values[j].add(row.get(columnsList.get(j)));        }      }      //将“字段名/列”放入columnsMap      for(int k = 0; k < columnsList.size(); k++){        columnsMap.put(columnsList.get(k),values[k]);      }    }    return columnsMap;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -