?? resultset.java
字號:
/* Copyright (C) 2002-2004 MySQL AB This program is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. There are special exceptions to the terms and conditions of the GPL as it is applied to this software. View the full text of the exception exception in file EXCEPTIONS-CONNECTOR-J in the directory of this software distribution. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package com.mysql.jdbc;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.io.ObjectInputStream;import java.io.StringReader;import java.math.BigDecimal;import java.net.MalformedURLException;import java.net.URL;import java.sql.Array;import java.sql.Date;import java.sql.Ref;import java.sql.SQLException;import java.sql.SQLWarning;import java.sql.Time;import java.sql.Timestamp;import java.sql.Types;import java.util.Calendar;import java.util.GregorianCalendar;import java.util.HashMap;import java.util.Map;import java.util.TimeZone;/** * A ResultSet provides access to a table of data generated by executing a * Statement. The table rows are retrieved in sequence. Within a row its * column values can be accessed in any order. * * <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> * * <P> * The getXXX methods retrieve column values for the current row. You can * retrieve values either using the index number of the column, or by using * the name of the column. In general using the column index will be more * efficient. Columns are numbered from 1. * </p> * * <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> * * <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> * * <P> * Column names used as input to getXXX methods are case insenstive. 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 were used there is no way for * the programmer to guarentee that they actually refer to the intended * columns. * </p> * * <P> * 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. * </p> * * <P> * The number, types and properties of a ResultSet's columns are provided by * the ResultSetMetaData object returned by the getMetaData method. * </p> * * @author Mark Matthews * @version $Id: ResultSet.java,v 1.18.2.35 2004/11/05 16:57:46 mmatthew Exp $ * * @see ResultSetMetaData * @see java.sql.ResultSet */public class ResultSet implements java.sql.ResultSet { /** * This method ends up being staticly synchronized, so just store our own * copy.... */ private TimeZone defaultTimeZone; /** The Connection instance that created us */ protected com.mysql.jdbc.Connection connection; // The connection that created us /** Map column names (and all of their permutations) to column indices */ protected Map columnNameToIndex = null; /** Map of fully-specified column names to column indices */ protected Map fullColumnNameToIndex = null; /** The actual rows */ protected RowData rowData; // The results /** The warning chain */ protected java.sql.SQLWarning warningChain = null; /** The statement that created us */ protected com.mysql.jdbc.Statement owningStatement; /** The catalog that was in use when we were created */ protected String catalog = null; /** * Any info message from the server that was created while generating this * result set (if 'info parsing' is enabled for the connection). */ protected String serverInfo = null; /** The fields for this result set */ protected Field[] fields; // The fields /** Pointer to current row data */ protected byte[][] thisRow; // Values for current row /** Are we in the middle of doing updates to the current row? */ protected boolean doingUpdates = false; /** Has this result set been closed? */ protected boolean isClosed = false; /** Are we on the insert row? */ protected boolean onInsertRow = false; /** * Do we actually contain rows, or just information about * UPDATE/INSERT/DELETE? */ protected boolean reallyResult = false; /** Did the previous value retrieval find a NULL? */ protected boolean wasNullFlag = false; /** * First character of the query that created this result set...Used to * determine whether or not to parse server info messages in certain * circumstances. */ protected char firstCharOfQuery; /** The current row #, -1 == before start of result set */ protected int currentRow = -1; // Cursor to current row; /** The direction to fetch rows (always FETCH_FORWARD) */ protected int fetchDirection = FETCH_FORWARD; /** The number of rows to fetch in one go... */ protected int fetchSize = 0; /** Are we read-only or updatable? */ protected int resultSetConcurrency = 0; /** Are we scroll-sensitive/insensitive? */ protected int resultSetType = 0; /** How many rows were affected by UPDATE/INSERT/DELETE? */ protected long updateCount; // These are longs for // recent versions of the MySQL server. // // They get reduced to ints via the JDBC API, // but can be retrieved through a MySQLStatement // in their entirety. // /** Value generated for AUTO_INCREMENT columns */ protected long updateId = -1; private Calendar fastDateCal = null; private boolean hasBuiltIndexMapping = false; private boolean useStrictFloatingPoint = false; /** * Create a result set for an executeUpdate statement. * * @param updateCount the number of rows affected by the update * @param updateID the autoincrement value (if any) */ public ResultSet(long updateCount, long updateID) { this.updateCount = updateCount; this.updateId = updateID; reallyResult = false; fields = new Field[0]; } /** * Create a new ResultSet * * @param catalog the database in use when we were created * @param fields an array of Field objects (basically, the ResultSet * MetaData) * @param tuples actual row data * @param conn the Connection that created us. * * @throws SQLException if an error occurs */ public ResultSet(String catalog, Field[] fields, RowData tuples, com.mysql.jdbc.Connection conn) throws SQLException { this(fields, tuples); setConnection(conn); this.catalog = catalog; } /** * Creates a new ResultSet object. * * @param fields DOCUMENT ME! * @param tuples DOCUMENT ME! * * @throws SQLException DOCUMENT ME! */ public ResultSet(Field[] fields, RowData tuples) throws SQLException { //_currentRow = -1; this.fields = fields; this.rowData = tuples; this.updateCount = (long) rowData.size(); if (Driver.DEBUG) { System.out.println("Retrieved " + updateCount + " rows"); } this.reallyResult = true; // Check for no results if (this.rowData.size() > 0) { //_thisRow = _rows.next(); if (this.updateCount == 1) { if (this.thisRow == null) { //_currentRow = -1; this.rowData.close(); // empty result set this.updateCount = -1; } } } else { this.thisRow = null; } this.rowData.setOwner(this); } /** * JDBC 2.0 * * <p> * Determine if the cursor is after the last row in the result set. * </p> * * @return true if after the last row, false otherwise. Returns false when * the result set contains no rows. * * @exception SQLException if a database-access error occurs. */ public boolean isAfterLast() throws SQLException { if (Driver.TRACE) { Object[] args = { }; Debug.methodCall(this, "isAfterLast", args); } checkClosed(); boolean b = rowData.isAfterLast(); if (Driver.TRACE) { Debug.returnValue(this, "isAfterLast", new Boolean(b)); } return b; } /** * JDBC 2.0 Get an array column. * * @param i the first column is 1, the second is 2, ... * * @return an object representing an SQL array * * @throws SQLException if a database error occurs * @throws NotImplemented DOCUMENT ME! */ public java.sql.Array getArray(int i) throws SQLException { throw new NotImplemented(); } /** * JDBC 2.0 Get an array column. * * @param colName the column name * * @return an object representing an SQL array * * @throws SQLException if a database error occurs * @throws NotImplemented DOCUMENT ME! */ public java.sql.Array getArray(String colName) throws SQLException { throw new NotImplemented(); } /** * A column value can be retrieved as a stream of ASCII characters and then * read in chunks from the stream. This method is particulary 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 for * available() whether there is data available or not. * </p> * * @param columnIndex the first column is 1, the second is 2, ... * * @return a Java InputStream 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 java.sql.SQLException if a database access error occurs * * @see getBinaryStream */ public InputStream getAsciiStream(int columnIndex) throws java.sql.SQLException { checkRowPos(); return getBinaryStream(columnIndex); } /** * DOCUMENT ME! * * @param columnName DOCUMENT ME! * * @return DOCUMENT ME! * * @throws java.sql.SQLException DOCUMENT ME! */ public InputStream getAsciiStream(String columnName) throws java.sql.SQLException { return getAsciiStream(findColumn(columnName)); } //--------------------------------------------------------------------- // Traversal/Positioning //--------------------------------------------------------------------- /** * JDBC 2.0 * * <p> * Determine if the cursor is before the first row in the result set. * </p> * * @return true if before the first row, false otherwise. Returns false * when the result set contains no rows. * * @exception SQLException if a database-access error occurs. */ public boolean isBeforeFirst() throws SQLException { if (Driver.TRACE) { Object[] args = { }; Debug.methodCall(this, "isBeforeFirst", args); } checkClosed(); boolean b = rowData.isBeforeFirst(); if (Driver.TRACE) { Debug.returnValue(this, "isBeforeFirst", new Boolean(b)); } return b; } /**
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -