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

📄 大容量表的读取缓冲器.txt

📁 压缩包中的程序是一个关于中国象棋的源代码
💻 TXT
字号:
/**
 * <p>Title:大容量表的读取缓冲器 </p>
 * 2005-03-09
 * @author Javaest
 * @version 1.0
 */
import java.sql.*;
import java.util.*;

public class RecordBuffer {
  public static final int ADD_RECORDS_AFTER_USE = 1;
  public static final int WHEN_RECORDS_IS_NOT_ENOUGH = 0;
  int addMode = this.WHEN_RECORDS_IS_NOT_ENOUGH;
  int deaultGetRecordNum=20;
  LinkedList recordQueue = new LinkedList();
  Connection con;
  String tableName;
  String keyAttribute;
  int startNum;
  int endNum;
  int increment;
  int queueMinLength;
  int queueStandardlength;
  int currentRange;
  public RecordBuffer(Connection con, String tableName, String keyAttribute,
                      int startNum, int endNum, int increment,
                      int queueMinLength, int queueStandardlength) {
    this.con = con;
    this.tableName = tableName;
    this.keyAttribute = keyAttribute;
    this.startNum = startNum;
    this.endNum = endNum;
    this.increment = increment;
    this.queueMinLength = queueMinLength;
    this.queueStandardlength = queueStandardlength;
    this.currentRange = queueStandardlength;

  }
  public Vector[] getRecords() throws DoNotHaveEnoughRecordToFillQueueException {
return     this.getRecords(deaultGetRecordNum);
  }

  public Vector[] getRecords(int num) throws
      DoNotHaveEnoughRecordToFillQueueException {
    if (num > queueMinLength) {
      num = queueMinLength;
    }
    if (num > recordQueue.size()) {
      this.addRecordToQueue();
    }
    Vector[] returnData = new Vector[num];
    for (int i = 0; i < num; i++) {
      returnData[i] = (Vector) recordQueue.getLast();
      recordQueue.removeLast();
    }
    if (addMode == this.ADD_RECORDS_AFTER_USE) {
      this.addRecordToQueue();
    }

    return returnData;

  }

  public void addRecordToQueue() throws
      DoNotHaveEnoughRecordToFillQueueException {
    Vector[] records = null;
    StringBuffer sql = new StringBuffer("Select top ");
    sql.append(queueMinLength);
    sql.append(" * form ");
    sql.append(tableName);
    sql.append(" where ");
    sql.append(keyAttribute);
    sql.append(" between ");
    sql.append(startNum);
    sql.append(" and ");
    sql.append( (startNum + currentRange));
    try {
      ResultSet rs = con.createStatement().executeQuery(sql.toString());
      records = ResultSetProcessing.getVectorsOfRecord(rs);
      for (int i = 0; i < records.length; i++) {
        recordQueue.addFirst(records[i]);
      }
      if (records.length < queueMinLength) {
        currentRange = currentRange * 2;
        this.addRecordToQueue();

      }

      currentRange = queueStandardlength;

    }
    catch (DoNotHaveRecordInResultSetException ex) {
      if ( (startNum + currentRange) > endNum) {
        throw new DoNotHaveEnoughRecordToFillQueueException("表已经没有记录!");
      }
      currentRange = currentRange * 2;

      this.addRecordToQueue();

    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public void setAddMode(int num) {
    if (num == this.ADD_RECORDS_AFTER_USE) {
      addMode = this.WHEN_RECORDS_IS_NOT_ENOUGH;
    }
    else {
      addMode = this.WHEN_RECORDS_IS_NOT_ENOUGH;
    }

  }
  public int getAddMode()
  {
    return addMode;
  }
  
  public void setDeaultGetRecordNum(int num)
  {
    if(num>this.queueMinLength)
    {
      deaultGetRecordNum=queueMinLength;
    }
    else
    {
      deaultGetRecordNum=num;
    }
  }
  public int getDeaultGetRecordNum()
  {
    return deaultGetRecordNum;
  }
}

class DoNotHaveEnoughRecordToFillQueueException
    extends Exception {
  public DoNotHaveEnoughRecordToFillQueueException() {

  }

  public DoNotHaveEnoughRecordToFillQueueException(String msg) {
    super(msg);
  }

}

class ResultSetProcessing {

  public static Vector[] getVectorsOfRecord(ResultSet rs) throws java.sql.
      SQLException, DoNotHaveRecordInResultSetException {
    Vector records = new Vector();
    

      while (rs.next()) {
        ResultSetMetaData rsmd=rs.getMetaData();
        int colNum=rsmd.getColumnCount();

        Vector currentRecord = new Vector(colNum);
        for (int i = 0; i < colNum; i++) {
          currentRecord.add(rs.getObject(i + 1));
        }
       
        records.add(currentRecord);
      }
      if(records.size()==0){
        throw new DoNotHaveRecordInResultSetException("ResultSet中没有记录!");
      }

      return (Vector[]) records.toArray();
 
  }


}
class DoNotHaveRecordInResultSetException extends Exception
{
  public DoNotHaveRecordInResultSetException()
  {
    
  }
  public DoNotHaveRecordInResultSetException(String msg)
  {
    super(msg);
  }
}

⌨️ 快捷键说明

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