JAVA把数据库查询结果转化为Hashmap,ArrayList,Vector

时间:2022-11-06 19:36:24

package cn.edu.hust.col;

import cn.edu.hust.common.tools.*;
import java.util.*;
import java.sql.ResultSetMetaData;
import java.sql.Types;

public class CourseDbConn    extends DBConn

{
  public CourseDbConn()

{
super();
  }

  public CourseDbConn(String str) {
    super(str);
  }

  /**
   * 查询数据 返回的是一个ArrayList对象,对象中的每一个元素是一个HashMap对象
   * @param sql String 查询语句
   * @return ArrayList 结果集
   * @throws Exception
   */
  public ArrayList queryListAsHashMap(String sql) throws Exception {
    java.sql.ResultSet rs = this.executeSql(sql);
    ArrayList list = new ArrayList(20);
    ResultSetMetaData rsm = rs.getMetaData();
    int count = rsm.getColumnCount();
    while (rs.next()) {
      HashMap map = new HashMap();
      for (int i = 0; i < count; i++) {
        String columnName = rsm.getColumnName( (i + 1));
        int sqlType = rsm.getColumnType(i+1);
        Object sqlView = rs.getString(columnName);
        if (Types.CHAR == sqlType && null != sqlView) {
            map.put(columnName, sqlView.toString().trim());
        } else {
            map.put(columnName, sqlView);
        }
      }
      list.add(map);
    }
    rs.close();
    return list;
  }

  /**
   * 查询数据 返回的是一个Vector对象,对象中的每一个元素是一个HashMap对象
   * @param sql String
   * @return Vector
   * @throws Exception
   */
  public Vector queryVectorAsHashMap(String sql) throws Exception {
    java.sql.ResultSet rs = this.executeSql(sql);
    Vector list = new Vector(20);
    ResultSetMetaData rsm = rs.getMetaData();
    int count = rsm.getColumnCount();
    while (rs.next()) {
      HashMap map = new HashMap();
      for (int i = 0; i < count; i++) {
        String columnName = rsm.getColumnName( (i + 1));
        map.put(columnName, rs.getString(columnName));
      }
      list.add(map);
    }
    rs.close();
    return list;
  }

  /**
   * 查询数据 返回的是一个ArrayList对象,对象中的每一个元素是一个Vector对象
   * @param sql String
   * @return Vector
   * @throws Exception
   */
  public ArrayList queryListAsVector(String sql) throws Exception {
    java.sql.ResultSet rs = this.executeSql(sql);
    ArrayList list = new ArrayList(20);
    ResultSetMetaData rsm = rs.getMetaData();
    int count = rsm.getColumnCount();
    while (rs.next()) {
      Vector map = new Vector();
      for (int i = 0; i < count; i++) {
        String columnName = rsm.getColumnName( (i + 1));
        map.add(rs.getString(columnName));
      }
      list.add(map);
    }
    rs.close();
    //this.close();
    return list;
  }

  /**
   * 查询数据 返回的是一个Vector对象,对象中的每一个元素是一个Vector对象
   * @param sql String
   * @return Vector
   * @throws Exception
   */
  public Vector queryVectorAsVector(String sql) throws Exception {
    java.sql.ResultSet rs = this.executeSql(sql);
    Vector list = new Vector(20);
    ResultSetMetaData rsm = rs.getMetaData();
    int count = rsm.getColumnCount();
    while (rs.next()) {
      Vector map = new Vector();
      for (int i = 0; i < count; i++) {
        String columnName = rsm.getColumnName( (i + 1));
        map.add(rs.getString(columnName));
      }
      list.add(map);
    }
    rs.close();
    //this.close();
    return list;
  }

  /**
   * 查询详细信息 ,返回结果是一个HASH对像
   * @param sql String 查询语句
   * @return HashMap 结果集
   * @throws Exception
   */
  public HashMap queryDetailAsHashMap(String sql) throws Exception {
    HashMap map = new HashMap();
    //Log.debug(sql);
    java.sql.ResultSet rs = this.executeSql(sql);
    ResultSetMetaData rsm = rs.getMetaData();
    int count = rsm.getColumnCount();
    if (rs.next()) {
      for (int i = 0; i < count; i++) {
        String columnName = rsm.getColumnName( (i + 1));
        int sqlType = rsm.getColumnType(i+1);
        Object sqlView = rs.getObject(columnName);
        if (Types.CHAR == sqlType && null != sqlView) {
            map.put(columnName, sqlView.toString().trim());
        } else {
            map.put(columnName, sqlView);
        }
      }
    }
    rs.close();
    //this.close();
    return map;
  }

  /**
   * 查询详细信息,返回结果是一个Vector对象
   * @param sql String 查询语句
   * @return Vector 查询结果
   * @throws Exception
   */
  public Vector queryDetailAsVector(String sql) throws Exception {
    Vector map = new Vector();
    //Log.debug(sql);
    java.sql.ResultSet rs = this.executeSql(sql);
    ResultSetMetaData rsm = rs.getMetaData();
    int count = rsm.getColumnCount();
    if (rs.next()) {
      for (int i = 0; i < count; i++) {
        String columnName = rsm.getColumnName( (i + 1));
        map.add(rs.getString(columnName));
      }
    }
    rs.close();
    //this.close();
    return map;
  }

  /**
   * 更新数据
   * @param sql String sql语句
   * @return boolean 更新成功返回真
   * @throws Exception
   */
  public boolean update(String sql) throws Exception {
    int iReturn = this.updateSql(sql);
    return iReturn == 0;
  }

  /**
   * 插入数据
   * @param sql String sql语句
   * @return boolean 添加成功返回真
   * @throws Exception
   */
  public boolean insert(String sql) throws Exception {
    int iReturn = this.updateSql(sql);
    return iReturn == 0;
  }

}

 

http://blog.csdn.net/hwctl/archive/2007/06/02/1635303.aspx