封裝查詢

package cn.edu.hust.col;

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

/**
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: bluegrid.com.cn</p>
 *
 * @author zcp
 * @version 1.0
 */
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;
  }

}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章