jdbcresultset.java

来自「一个可以在applet窗体上持行sql语句并显示返回结果的程序」· Java 代码 · 共 1,814 行 · 第 1/5 页

JAVA
1,814
字号
/*
 * jdbcResultSet.java
 */

package org.hsql;
import java.sql.*;
import java.io.*;
import java.math.BigDecimal;
import java.util.*;

/**
 * <P><font color="#009900">
 * In Hypersonic SQL, the class jdbcResultSet implements both the
 * ResultSet and ResultSetMetaData interfaces. To conform the JDBC standard,
 * it is anyway required to call the 'getMetaData' function to get the
 * ResultSetMetaData of the ResultSet. This is so because in other JDBC drivers
 * the interfaces are not implemented in the same class.
 * <P>
 * ResultSetMetaData can be used to find out about the types
 * and properties of the columns in a ResultSet.
 * </font><P>
 * <P>A ResultSet provides access to a table of data.  A ResultSet
 * object is usually generated by executing a Statement.
 *
 * <P>A ResultSet maintains a cursor pointing to its current row of
 * data.  Initially the cursor is positioned before the first row.
 * The 'next' method moves the cursor to the next row.
 *
 * <P>The getXXX methods retrieve column values for the current
 * row.  You can retrieve values using either the index number of the
 * column or the name of the column.  In general, using the
 * column index will be more efficient.  Columns are numbered from 1.
 *
 * <P>For maximum portability, ResultSet columns within each row should be
 * read in left-to-right order and each column should be read only once.
 *
 * <P>For the getXXX methods, the JDBC driver attempts to convert the
 * underlying data to the specified Java type and returns a suitable
 * Java value.  See the JDBC specification for allowable mappings
 * from SQL types to Java types with the ResultSet.getXXX methods.
 *
 * <P>Column names used as input to getXXX methods are case
 * insensitive.  When performing a getXXX using a column name, if
 * several columns have the same name, then the value of the first
 * matching column will be returned. The column name option is
 * designed to be used when column names are used in the SQL
 * query. For columns that are NOT explicitly named in the query, it
 * is best to use column numbers. If column names are used, there is
 * no way for the programmer to guarantee that they actually refer to
 * the intended columns.
 *
 * <P>A ResultSet is automatically closed by the Statement that
 * generated it when that Statement is closed, re-executed, or used
 * to retrieve the next result from a sequence of multiple results.
 *
 * <P>The number, types and properties of a ResultSet's columns are
 * provided by the ResulSetMetaData object returned by the getMetaData
 * method.
 *
 * @see jdbcStatement#executeQuery
 * @see jdbcStatement#getResultSet
 */

public class jdbcResultSet implements ResultSet,ResultSetMetaData {
  private Result rResult;
  private Record nCurrent;
  private int iCurrentRow;
  private int iUpdateCount;
  private boolean bInit; // false if before first row
  private int iColumnCount;
  private boolean bWasNull;

  /**
   * Moves the cursor down one row from its current position.
   * A ResultSet cursor is initially positioned before the first row; the
   * first call to next makes the first row the current row; the
   * second call makes the second row the current row, and so on.
   *
   * <P>If an input stream is open for the current row, a call
   * to the method <code>next</code> will
   * implicitly close it. The ResultSet's warning chain is cleared
   * when a new row is read.
   *
   * @return true if the new current row is valid; false if there
   * are no more rows
   */
  public boolean next() throws SQLException {
    bWasNull=false; // todo: test if other databases do the same
    if(rResult==null) {
      return false; // throw exception ?
    }
    if(bInit==false) {
      nCurrent=rResult.rRoot;
      bInit=true;
      iCurrentRow=1;
    } else {
      Trace.check(nCurrent!=null,Trace.NO_DATA_IS_AVAILABLE);
      nCurrent=nCurrent.next;
      iCurrentRow++;
    }
    if(nCurrent==null) {
      iCurrentRow=0;
      return false;
    }
    return true;
  }
  /**
	 * Releases this <code>ResultSet</code> object's database and
	 * JDBC resources immediately instead of waiting for
   * this to happen when it is automatically closed.
   *
   * <P><B>Note:</B> A ResultSet is automatically closed by the
   * Statement that generated it when that Statement is closed,
   * re-executed, or is used to retrieve the next result from a
   * sequence of multiple results. A ResultSet is also automatically
   * closed when it is garbage collected.
   */
  public void close() {
    iUpdateCount=-1;
    rResult=null;
  }
  /**
   * Reports whether
   * the last column read had a value of SQL NULL.
   * Note that you must first call getXXX on a column to try to read
   * its value and then call wasNull() to see if the value read was
   * SQL NULL.
   *
   * @return true if last column read was SQL NULL and false otherwise
   */
  public boolean wasNull() {
    return bWasNull;
  }
  /**
   * Maps the given Resultset column name to its ResultSet column index.
   *
   * @param columnName the name of the column
   * @return the column index
   * @exception SQLException if a database access error occurs
   */
  public int findColumn(String columnName) throws SQLException {
    if(Trace.TRACE) Trace.trace(columnName);
    for(int i=0;i<iColumnCount;i++) {
      if(columnName.equalsIgnoreCase(rResult.sLabel[i])) {
        return i+1;
      }
    }
    throw Trace.error(Trace.COLUMN_NOT_FOUND);
  }
  /**
   * Gets the value of a column in the current row as a Java String.
   *
   * @param columnIndex the first column is 1, the second is 2, ...
   * @return the column value; if the value is SQL NULL, the result is null
   * @exception SQLException if a database access error occurs
   */
  public String getString(int column) throws SQLException {
    checkColumn(--column);
    checkAvailable();
    Object o=nCurrent.data[column];
    // use checknull because getColumnInType is not used
    checkNull(o);
    return o==null ? null : o.toString();
  }
  /**
   * Gets the value of a column in the current row as a Java boolean.
   *
   * @param columnIndex the first column is 1, the second is 2, ...
   * @return the column value; if the value is SQL NULL, the result is false
   * @exception SQLException if a database access error occurs
   */
  public boolean getBoolean(int column) throws SQLException {
    Object o=getColumnInType(--column,Column.BIT);
    return o==null ? false : ((Boolean)o).booleanValue();
  }
  /**
   * Gets the value of a column in the current row as a Java byte.
   *
   * @param columnIndex the first column is 1, the second is 2, ...
   * @return the column value; if the value is SQL NULL, the result is 0
   * @exception SQLException if a database access error occurs
   */
  public byte getByte(int column) throws SQLException {
    return (byte)getShort(column);
  }
  /**
   * Gets the value of a column in the current row as a Java short.
   *
   * @param columnIndex the first column is 1, the second is 2, ...
   * @return the column value; if the value is SQL NULL, the result is 0
   * @exception SQLException if a database access error occurs
   */
  public short getShort(int column) throws SQLException {
    Object o=getColumnInType(--column,Column.SMALLINT);
    return o==null ? 0 : ((Short)o).shortValue();
  }
  /**
   * Gets the value of a column in the current row as a Java int.
   *
   * @param columnIndex the first column is 1, the second is 2, ...
   * @return the column value; if the value is SQL NULL, the result is 0
   * @exception SQLException if a database access error occurs
   */
  public int getInt(int column) throws SQLException {
    Object o=getColumnInType(--column,Column.INTEGER);
    return o==null ? 0 : ((Integer)o).intValue();
  }
  /**
   * Gets the value of a column in the current row as a Java long.
   *
   * @param columnIndex the first column is 1, the second is 2, ...
   * @return the column value; if the value is SQL NULL, the result is 0
   * @exception SQLException if a database access error occurs
   */
  public long getLong(int column) throws SQLException {
    Object o=getColumnInType(--column,Column.BIGINT);
    return o==null ? 0 : ((Long)o).longValue();
  }
  /**
   * Gets the value of a column in the current row as a Java float.
   *
   * @param columnIndex the first column is 1, the second is 2, ...
   * @return the column value; if the value is SQL NULL, the result is 0
   * @exception SQLException if a database access error occurs
   */
  public float getFloat(int column) throws SQLException {
    Object o=getColumnInType(--column,Column.REAL);
    return o==null ? (float)0.0 : ((Float)o).floatValue();
  }
  /**
   * Gets the value of a column in the current row as a Java double.
   *
   * @param columnIndex the first column is 1, the second is 2, ...
   * @return the column value; if the value is SQL NULL, the result is 0
   * @exception SQLException if a database access error occurs
   */
  public double getDouble(int column) throws SQLException {
    Object o=getColumnInType(--column,Column.DOUBLE);
    return o==null ? 0.0 : ((Double)o).doubleValue();
  }
  /**
   * Gets the value of a column in the current row as a java.math.BigDecimal
   * object.
   *
   * @param columnIndex the first column is 1, the second is 2, ...
   * @param scale the number of digits to the right of the decimal
   * @return the column value; if the value is SQL NULL, the result is null
   * @exception SQLException if a database access error occurs
   * @deprecated
   */
  public BigDecimal getBigDecimal(int column,int scale)
  throws SQLException {
    return (BigDecimal)getColumnInType(--column,Column.DECIMAL);
  }
  /**
   * Gets the value of a column in the current row as a Java byte array.
   * The bytes represent the raw values returned by the driver.
   *
   * @param columnIndex the first column is 1, the second is 2, ...
   * @return the column value; if the value is SQL NULL, the result is null
   * @exception SQLException if a database access error occurs
   */
  public byte[] getBytes(int column) throws SQLException {
    Object o=getColumnInType(--column,Column.BINARY);
    return o==null ? null : ((ByteArray)o).byteValue();
  }
  /**
   * Gets the value of a column in the current row as a java.sql.Date object.
   *
   * @param columnIndex the first column is 1, the second is 2, ...
   * @return the column value; if the value is SQL NULL, the result is null
   * @exception SQLException if a database access error occurs
   */
  public java.sql.Date getDate(int column) throws SQLException {
    return (java.sql.Date)getColumnInType(--column,Column.DATE);
  }
  /**
   * Gets the value of a column in the current row as a java.sql.Time object.
   *
   * @param columnIndex the first column is 1, the second is 2, ...
   * @return the column value; if the value is SQL NULL, the result is null
   * @exception SQLException if a database access error occurs
   */
  public Time getTime(int column) throws SQLException {
    return (Time)getColumnInType(--column,Column.TIME);
  }
  /**
   * Gets the value of a column in the current row as a java.sql.Timestamp
   * object.
   *
   * @param columnIndex the first column is 1, the second is 2, ...
   * @return the column value; if the value is SQL NULL, the result is null
   * @exception SQLException if a database access error occurs
   */
  public Timestamp getTimestamp(int column) throws SQLException {
    return (Timestamp)getColumnInType(--column,Column.TIMESTAMP);
  }
  /**
	 * Gets the value of a column in the current row as a stream of
	 * ASCII characters. The value can then be read in chunks from the
	 * stream. This method is particularly
   * suitable for retrieving large LONGVARCHAR values.  The JDBC driver will
   * do any necessary conversion from the database format into ASCII.
   *
   * <P><B>Note:</B> All the data in the returned stream must be
   * read prior to getting the value of any other column. The next
   * call to a get method implicitly closes the stream.  Also, a
   * stream may return 0 when the method <code>available</code>
   * is called whether there is data
   * available or not.
   *
   * @param columnIndex the first column is 1, the second is 2, ...
   * @return a Java input stream that delivers the database column value
   * as a stream of one byte ASCII characters.  If the value is SQL NULL
   * then the result is null.
   * @exception SQLException if a database access error occurs
   */
  public InputStream getAsciiStream(int column) throws SQLException {
    return getUnicodeStream(column);
  }
  /**
	 * Gets the value of a column in the current row as a stream of
	 * Unicode characters. The value can then be read in chunks from the
	 * stream. This method is particularly
   * suitable for retrieving large LONGVARCHAR values.  The JDBC driver will
   * do any necessary conversion from the database format into Unicode.
	 * The byte format of the Unicode stream must Java UTF-8,
	 * as specified in the Java Virtual Machine Specification.
   *
   * <P><B>Note:</B> All the data in the returned stream must be
   * read prior to getting the value of any other column. The next
   * call to a get method implicitly closes the stream. Also, a
   * stream may return 0 when the method <code>available</code>
   * is called whether there is data
   * available or not.
   *
   * @param columnIndex the first column is 1, the second is 2, ...
   * @return a Java input stream that delivers the database column value
   * as a stream of two-byte Unicode characters.  If the value is SQL NULL
   * then the result is null.
   * @exception SQLException if a database access error occurs
   * @deprecated
   */
  public InputStream getUnicodeStream(int column) throws SQLException {
    return new ByteArrayInputStream(getBytes(column));
  }
  /**
	 * Gets the value of a column in the current row as a stream of
	 * uninterpreted bytes. The value can then be read in chunks from the
	 * stream. This method is particularly
   * suitable for retrieving large LONGVARBINARY values.
   *
   * <P><B>Note:</B> All the data in the returned stream must be
   * read prior to getting the value of any other column. The next
   * call to a get method implicitly closes the stream. Also, a
   * stream may return 0 when the method <code>available</code>
   * is called whether there is data
   * available or not.
   *
   * @param columnIndex the first column is 1, the second is 2, ...
   * @return a Java input stream that delivers the database column value
   * as a stream of uninterpreted bytes.  If the value is SQL NULL
   * then the result is null.

⌨️ 快捷键说明

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