home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / JDBC / JDBC_011 / JDBC-011.ZIP / jdbc / java / sql / ResultSet.java < prev    next >
Encoding:
Java Source  |  1996-11-10  |  21.5 KB  |  541 lines

  1. /*
  2.  * Copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "LICENSE"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  * 
  17.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  18.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  19.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  20.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  21.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  22.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  23.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  24.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  25.  * HIGH RISK ACTIVITIES.
  26.  */
  27.  
  28. package java.sql;
  29.  
  30. /**
  31.  * <P>A ResultSet provides access to a table of data generated by
  32.  * executing a Statement. The table rows are retrieved in
  33.  * sequence. Within a row its column values can be accessed in any
  34.  * order.
  35.  * 
  36.  * <P>A ResultSet maintains a cursor pointing to its current row of
  37.  * data.  Initially the cursor is positioned before the first row.
  38.  * The 'next' method moves the cursor to the next row.
  39.  *
  40.  * <P>The getXXX methods retrieve column values for the current
  41.  * row.  You can retrieve values either using the index number of the
  42.  * column, or by using the name of the column.  In general using the 
  43.  * column index will be more efficient.  Columns are numbered from 1.
  44.  *
  45.  * <P>For maximum portability, ResultSet columns within each row should be
  46.  * read in left-to-right order and each column should be read only once.
  47.  *
  48.  * <P>For the getXXX methods, the JDBC driver attempts to convert the
  49.  * underlying data to the specified Java type and returns a suitable
  50.  * Java value.  See the JDBC specification for allowable mappings
  51.  * from SQL types to Java types with the ResultSet.getXXX methods.
  52.  *
  53.  * <P>Column names used as input to getXXX methods are case insensitive.
  54.  * When performing a getXXX using a column name, if several columns have
  55.  * the same name, then the value of the first matching column will be
  56.  * returned.
  57.  *
  58.  * <P>A ResultSet is automatically closed by the Statement that
  59.  * generated it when that Statement is closed, re-executed, or is used
  60.  * to retrieve the next result from a sequence of multiple results.
  61.  * 
  62.  * <P>The number, types and properties of a ResultSet's columns are
  63.  * provided by the ResulSetMetaData object returned by the getMetaData
  64.  * method.
  65.  *
  66.  * @see Statement#executeQuery 
  67.  * @see Statement#getResultSet 
  68.  * @see ResultSetMetaData 
  69.  */
  70.  
  71. public interface ResultSet {
  72.  
  73.     /**
  74.      * A ResultSet is initially positioned before its first row; the
  75.      * first call to next makes the first row the current row; the
  76.      * second call makes the second row the current row, etc. 
  77.      *
  78.      * <P>If an input stream from the previous row is open, it is
  79.      * implicitly closed. The ResultSet's warning chain is cleared
  80.      * when a new row is read.
  81.      *
  82.      * @return true if the new current row is valid; false if there
  83.      * are no more rows 
  84.      */
  85.     boolean next() throws SQLException;
  86.  
  87.  
  88.     /**
  89.      * In some cases, it is desirable to immediately release a
  90.      * ResultSet's database and JDBC resources instead of waiting for
  91.      * this to happen when it is automatically closed; the close
  92.      * method provides this immediate release.
  93.      *
  94.      * <P><B>Note:</B> A ResultSet is automatically closed by the
  95.      * Statement that generated it when that Statement is closed,
  96.      * re-executed, or is used to retrieve the next result from a
  97.      * sequence of multiple results. A ResultSet is also automatically
  98.      * closed when it is garbage collected.  
  99.      */
  100.     void close() throws SQLException;
  101.  
  102.     /**
  103.      * A column may have the value of SQL NULL; wasNull reports whether
  104.      * the last column read had this special value.
  105.      * Note that you must first call getXXX on a column to try to read
  106.      * its value and then call wasNull() to find if the value was
  107.      * the SQL NULL.
  108.      *
  109.      * @return true if last column read was SQL NULL
  110.      */
  111.     boolean wasNull() throws SQLException;
  112.     
  113.     //======================================================================
  114.     // Methods for accessing results by column index
  115.     //======================================================================
  116.  
  117.     /**
  118.      * Get the value of a column in the current row as a Java String.
  119.      *
  120.      * @param columnIndex the first column is 1, the second is 2, ...
  121.      * @return the column value; if the value is SQL NULL, the result is null
  122.      */
  123.     String getString(int columnIndex) throws SQLException;
  124.  
  125.     /**
  126.      * Get the value of a column in the current row as a Java boolean.
  127.      *
  128.      * @param columnIndex the first column is 1, the second is 2, ...
  129.      * @return the column value; if the value is SQL NULL, the result is false
  130.      */
  131.     boolean getBoolean(int columnIndex) throws SQLException;
  132.  
  133.     /**
  134.      * Get the value of a column in the current row as a Java byte.
  135.      *
  136.      * @param columnIndex the first column is 1, the second is 2, ...
  137.      * @return the column value; if the value is SQL NULL, the result is 0
  138.      */
  139.     byte getByte(int columnIndex) throws SQLException;
  140.  
  141.     /**
  142.      * Get the value of a column in the current row as a Java short.
  143.      *
  144.      * @param columnIndex the first column is 1, the second is 2, ...
  145.      * @return the column value; if the value is SQL NULL, the result is 0
  146.      */
  147.     short getShort(int columnIndex) throws SQLException;
  148.  
  149.     /**
  150.      * Get the value of a column in the current row as a Java int.
  151.      *
  152.      * @param columnIndex the first column is 1, the second is 2, ...
  153.      * @return the column value; if the value is SQL NULL, the result is 0
  154.      */
  155.     int getInt(int columnIndex) throws SQLException;
  156.  
  157.     /**
  158.      * Get the value of a column in the current row as a Java long.
  159.      *
  160.      * @param columnIndex the first column is 1, the second is 2, ...
  161.      * @return the column value; if the value is SQL NULL, the result is 0
  162.      */
  163.     long getLong(int columnIndex) throws SQLException;
  164.  
  165.     /**
  166.      * Get the value of a column in the current row as a Java float.
  167.      *
  168.      * @param columnIndex the first column is 1, the second is 2, ...
  169.      * @return the column value; if the value is SQL NULL, the result is 0
  170.      */
  171.     float getFloat(int columnIndex) throws SQLException;
  172.  
  173.     /**
  174.      * Get the value of a column in the current row as a Java double.
  175.      *
  176.      * @param columnIndex the first column is 1, the second is 2, ...
  177.      * @return the column value; if the value is SQL NULL, the result is 0
  178.      */
  179.     double getDouble(int columnIndex) throws SQLException;
  180.  
  181.     /**
  182.      * Get the value of a column in the current row as a java.lang.Bignum object.
  183.      *
  184.      * @param columnIndex the first column is 1, the second is 2, ...
  185.      * @param scale the number of digits to the right of the decimal
  186.      * @return the column value; if the value is SQL NULL, the result is null
  187.      */
  188.     Bignum getBignum(int columnIndex, int scale) throws SQLException;
  189.  
  190.     /**
  191.      * Get the value of a column in the current row as a Java byte array.
  192.      * The bytes represent the raw values returned by the driver.
  193.      *
  194.      * @param columnIndex the first column is 1, the second is 2, ...
  195.      * @return the column value; if the value is SQL NULL, the result is null
  196.      */
  197.     byte[] getBytes(int columnIndex) throws SQLException;
  198.  
  199.     /**
  200.      * Get the value of a column in the current row as a java.sql.Date object.
  201.      *
  202.      * @param columnIndex the first column is 1, the second is 2, ...
  203.      * @return the column value; if the value is SQL NULL, the result is null
  204.      */
  205.     java.sql.Date getDate(int columnIndex) throws SQLException;
  206.  
  207.     /**
  208.      * Get the value of a column in the current row as a java.sql.Time object.
  209.      *
  210.      * @param columnIndex the first column is 1, the second is 2, ...
  211.      * @return the column value; if the value is SQL NULL, the result is null
  212.      */
  213.     java.sql.Time getTime(int columnIndex) throws SQLException;
  214.  
  215.     /**
  216.      * Get the value of a column in the current row as a java.sql.Timestamp object.
  217.      *
  218.      * @param columnIndex the first column is 1, the second is 2, ...
  219.      * @return the column value; if the value is SQL NULL, the result is null
  220.      */
  221.     java.sql.Timestamp getTimestamp(int columnIndex) throws SQLException;
  222.  
  223.     /**
  224.      * A column value can be retrieved as a stream of ASCII characters 
  225.      * and then read in chunks from the stream.  This method is particularly
  226.      * suitable for retrieving large LONGVARCHAR values.  The JDBC driver will
  227.      * do any necessary conversion from the database format into ASCII.
  228.      *
  229.      * <P><B>Note:</B> All the data in the returned stream must be
  230.      * read prior to getting the value of any other column. The next
  231.      * call to a get method implicitly closes the stream. . Also, a
  232.      * stream may return 0 for available() whether there is data
  233.      * available or not.
  234.      *
  235.      * @param columnIndex the first column is 1, the second is 2, ...
  236.      * @return a Java input stream that delivers the database column value
  237.      * as a stream of one byte ASCII characters.  If the value is SQL NULL
  238.      * then the result is null.  
  239.      */
  240.     java.io.InputStream getAsciiStream(int columnIndex) throws SQLException;
  241.  
  242.     /**
  243.      * A column value can be retrieved as a stream of Unicode characters 
  244.      * and then read in chunks from the stream.  This method is particularly
  245.      * suitable for retrieving large LONGVARCHAR values.  The JDBC driver will
  246.      * do any necessary conversion from the database format into Unicode.
  247.      *
  248.      * <P><B>Note:</B> All the data in the returned stream must be
  249.      * read prior to getting the value of any other column. The next
  250.      * call to a get method implicitly closes the stream. . Also, a
  251.      * stream may return 0 for available() whether there is data
  252.      * available or not.
  253.      *
  254.      * @param columnIndex the first column is 1, the second is 2, ...
  255.      * @return a Java input stream that delivers the database column value
  256.      * as a stream of two byte Unicode characters.  If the value is SQL NULL
  257.      * then the result is null.  
  258.      */
  259.     java.io.InputStream getUnicodeStream(int columnIndex) throws SQLException;
  260.  
  261.     /**
  262.      * A column value can be retrieved as a stream of uninterpreted bytes 
  263.      * and then read in chunks from the stream.  This method is particularly
  264.      * suitable for retrieving large LONGVARBINARY values.
  265.      *
  266.      * <P><B>Note:</B> All the data in the returned stream must be
  267.      * read prior to getting the value of any other column. The next
  268.      * call to a get method implicitly closes the stream. Also, a
  269.      * stream may return 0 for available() whether there is data
  270.      * available or not.
  271.      *
  272.      * @param columnIndex the first column is 1, the second is 2, ...
  273.      * @return a Java input stream that delivers the database column value
  274.      * as a stream of uninterpreted bytes.  If the value is SQL NULL
  275.      * then the result is null.  
  276.      */
  277.     java.io.InputStream getBinaryStream(int columnIndex)
  278.         throws SQLException;
  279.  
  280.  
  281.     //======================================================================
  282.     // Methods for accessing results by column name
  283.     //======================================================================
  284.  
  285.     /**
  286.      * Get the value of a column in the current row as a Java String.
  287.      *
  288.      * @param columnName is the SQL name of the column
  289.      * @return the column value; if the value is SQL NULL, the result is null
  290.      */
  291.     String getString(String columnName) throws SQLException;
  292.  
  293.     /**
  294.      * Get the value of a column in the current row as a Java boolean.
  295.      *
  296.      * @param columnName is the SQL name of the column
  297.      * @return the column value; if the value is SQL NULL, the result is false
  298.      */
  299.     boolean getBoolean(String columnName) throws SQLException;
  300.  
  301.     /**
  302.      * Get the value of a column in the current row as a Java byte.
  303.      *
  304.      * @param columnName is the SQL name of the column
  305.      * @return the column value; if the value is SQL NULL, the result is 0
  306.      */
  307.     byte getByte(String columnName) throws SQLException;
  308.  
  309.     /**
  310.      * Get the value of a column in the current row as a Java short.
  311.      *
  312.      * @param columnName is the SQL name of the column
  313.      * @return the column value; if the value is SQL NULL, the result is 0
  314.      */
  315.     short getShort(String columnName) throws SQLException;
  316.  
  317.     /**
  318.      * Get the value of a column in the current row as a Java int.
  319.      *
  320.      * @param columnName is the SQL name of the column
  321.      * @return the column value; if the value is SQL NULL, the result is 0
  322.      */
  323.     int getInt(String columnName) throws SQLException;
  324.  
  325.     /**
  326.      * Get the value of a column in the current row as a Java long.
  327.      *
  328.      * @param columnName is the SQL name of the column
  329.      * @return the column value; if the value is SQL NULL, the result is 0
  330.      */
  331.     long getLong(String columnName) throws SQLException;
  332.  
  333.     /**
  334.      * Get the value of a column in the current row as a Java float.
  335.      *
  336.      * @param columnName is the SQL name of the column
  337.      * @return the column value; if the value is SQL NULL, the result is 0
  338.      */
  339.     float getFloat(String columnName) throws SQLException;
  340.  
  341.     /**
  342.      * Get the value of a column in the current row as a Java double.
  343.      *
  344.      * @param columnName is the SQL name of the column
  345.      * @return the column value; if the value is SQL NULL, the result is 0
  346.      */
  347.     double getDouble(String columnName) throws SQLException;
  348.  
  349.     /**
  350.      * Get the value of a column in the current row as a java.lang.Bignum object.
  351.      *
  352.      * @param columnName is the SQL name of the column
  353.      * @param scale the number of digits to the right of the decimal
  354.      * @return the column value; if the value is SQL NULL, the result is null
  355.      */
  356.     Bignum getBignum(String columnName, int scale) throws SQLException;
  357.  
  358.     /**
  359.      * Get the value of a column in the current row as a Java byte array.
  360.      * The bytes represent the raw values returned by the driver.
  361.      *
  362.      * @param columnName is the SQL name of the column
  363.      * @return the column value; if the value is SQL NULL, the result is null
  364.      */
  365.     byte[] getBytes(String columnName) throws SQLException;
  366.  
  367.     /**
  368.      * Get the value of a column in the current row as a java.sql.Date object.
  369.      *
  370.      * @param columnName is the SQL name of the column
  371.      * @return the column value; if the value is SQL NULL, the result is null
  372.      */
  373.     java.sql.Date getDate(String columnName) throws SQLException;
  374.  
  375.     /**
  376.      * Get the value of a column in the current row as a java.sql.Time object.
  377.      *
  378.      * @param columnName is the SQL name of the column
  379.      * @return the column value; if the value is SQL NULL, the result is null
  380.      */
  381.     java.sql.Time getTime(String columnName) throws SQLException;
  382.  
  383.     /**
  384.      * Get the value of a column in the current row as a java.sql.Timestamp object.
  385.      *
  386.      * @param columnName is the SQL name of the column
  387.      * @return the column value; if the value is SQL NULL, the result is null
  388.      */
  389.     java.sql.Timestamp getTimestamp(String columnName) throws SQLException;
  390.  
  391.     /**
  392.      * A column value can be retrieved as a stream of ASCII characters 
  393.      * and then read in chunks from the stream.  This method is particularly
  394.      * suitable for retrieving large LONGVARCHAR values.  The JDBC driver will
  395.      * do any necessary conversion from the database format into ASCII.
  396.      *
  397.      * <P><B>Note:</B> All the data in the returned stream must
  398.      * be read prior to getting the value of any other column. The
  399.      * next call to a get method implicitly closes the stream.
  400.      *
  401.      * @param columnName is the SQL name of the column
  402.      * @return a Java input stream that delivers the database column value
  403.      * as a stream of one byte ASCII characters.  If the value is SQL NULL
  404.      * then the result is null.
  405.      */
  406.     java.io.InputStream getAsciiStream(String columnName) throws SQLException;
  407.  
  408.     /**
  409.      * A column value can be retrieved as a stream of Unicode characters 
  410.      * and then read in chunks from the stream.  This method is particularly
  411.      * suitable for retrieving large LONGVARCHAR values.  The JDBC driver will
  412.      * do any necessary conversion from the database format into Unicode.
  413.      *
  414.      * <P><B>Note:</B> All the data in the returned stream must
  415.      * be read prior to getting the value of any other column. The
  416.      * next call to a get method implicitly closes the stream.
  417.      *
  418.      * @param columnName is the SQL name of the column
  419.      * @return a Java input stream that delivers the database column value
  420.      * as a stream of two byte Unicode characters.  If the value is SQL NULL
  421.      * then the result is null.
  422.      */
  423.     java.io.InputStream getUnicodeStream(String columnName) throws SQLException;
  424.  
  425.     /**
  426.      * A column value can be retrieved as a stream of uninterpreted bytes 
  427.      * and then read in chunks from the stream.  This method is particularly
  428.      * suitable for retrieving large LONGVARBINARY values.
  429.      *
  430.      * <P><B>Note:</B> All the data in the returned stream must
  431.      * be read prior to getting the value of any other column. The
  432.      * next call to a get method implicitly closes the stream.
  433.      *
  434.      * @param columnName is the SQL name of the column
  435.      * @return a Java input stream that delivers the database column value
  436.      * as a stream of uninterpreted bytes.  If the value is SQL NULL
  437.      * then the result is null.
  438.      */
  439.     java.io.InputStream getBinaryStream(String columnName)
  440.         throws SQLException;
  441.  
  442.  
  443.     //=====================================================================
  444.     // Advanced features:
  445.     //=====================================================================
  446.  
  447.     /**
  448.      * <p>The first warning reported by calls on this ResultSet is
  449.      * returned. Subsequent ResultSet warnings will be chained to this
  450.      * SQLWarning.
  451.      *
  452.      * <P>The warning chain is automatically cleared each time a new
  453.      * row is read.
  454.      *
  455.      * <P><B>Note:</B> This warning chain only covers warnings caused
  456.      * by ResultSet methods.  Any warning caused by statement methods
  457.      * (such as reading OUT parameters) will be chained on the
  458.      * Statement object. 
  459.      *
  460.      * @return the first SQLWarning or null 
  461.      */
  462.     SQLWarning getWarnings() throws SQLException;
  463.  
  464.     /**
  465.      * After this call getWarnings returns null until a new warning is
  466.      * reported for this ResultSet.  
  467.      */
  468.     void clearWarnings() throws SQLException;
  469.  
  470.     /**
  471.      * Get the name of the SQL cursor used by this ResultSet.
  472.      *
  473.      * <P>In SQL, a result table is retrieved through a cursor that is
  474.      * named. The current row of a result can be updated or deleted
  475.      * using a positioned update/delete statement that references the
  476.      * cursor name. 
  477.      * 
  478.      * <P>JDBC supports this SQL feature by providing the name of the
  479.      * SQL cursor used by a ResultSet. The current row of a ResultSet
  480.      * is also the current row of this SQL cursor.
  481.      *
  482.      * <P><B>Note:</B> If positioned update is not supported a
  483.      * SQLException is thrown
  484.      *
  485.      * @return the ResultSet's SQL cursor name
  486.      */
  487.     String getCursorName() throws SQLException;
  488.  
  489.     /**
  490.      * The number, types and properties of a ResultSet's columns
  491.      * are provided by the getMetaData method.
  492.      *
  493.      * @return the description of a ResultSet's columns
  494.      */
  495.     ResultSetMetaData getMetaData() throws SQLException;
  496.  
  497.     /**
  498.      * <p>Get the value of a column in the current row as a Java object.
  499.      *
  500.      * <p>This method will return the value of the given column as a
  501.      * Java object.  The type of the Java object will be the default
  502.      * Java Object type corresponding to the column's SQL type,
  503.      * following the mapping specified in the JDBC spec.
  504.      *
  505.      * <p>This method may also be used to read datatabase specific abstract
  506.      * data types.
  507.      *
  508.      * @param columnIndex the first column is 1, the second is 2, ...
  509.      * @return A java.lang.Object holding the column value.  
  510.      */
  511.     Object getObject(int columnIndex) throws SQLException;
  512.  
  513.     /**
  514.      * <p>Get the value of a column in the current row as a Java object.
  515.      *
  516.      * <p>This method will return the value of the given column as a
  517.      * Java object.  The type of the Java object will be the default
  518.      * Java Object type corresponding to the column's SQL type,
  519.      * following the mapping specified in the JDBC spec.
  520.      *
  521.      * <p>This method may also be used to read datatabase specific abstract
  522.      * data types.
  523.      *
  524.      * @param columnName is the SQL name of the column
  525.      * @return A java.lang.Object holding the column value.  
  526.      */
  527.     Object getObject(String columnName) throws SQLException;
  528.  
  529.     //----------------------------------------------------------------
  530.  
  531.     /**
  532.      * Map a Resultset column name to a ResultSet column index.
  533.      *
  534.      * @param columnName the name of the column
  535.      * @return the column index
  536.      */
  537.  
  538.     int findColumn(String columnName) throws SQLException;
  539.  
  540. }
  541.