home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / sql / Statement.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  15.4 KB  |  413 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)Statement.java    1.16 98/09/29
  3.  * 
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. (<code>Confidential Information<code>).  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.sql;
  16.  
  17. /**
  18.  * <P>The object used for executing a static SQL statement
  19.  * and obtaining the results produced by it. 
  20.  *
  21.  * <P>Only one ResultSet per Statement can be open at any point in
  22.  * time. Therefore, if the reading of one ResultSet is interleaved
  23.  * with the reading of another, each must have been generated by
  24.  * different Statements. All statement <code>execute</code> methods implicitly
  25.  * close a statment's current ResultSet if an open one exists.
  26.  *
  27.  * @see Connection#createStatement
  28.  * @see ResultSet 
  29.  */
  30. public interface Statement {
  31.  
  32.     /**
  33.      * Executes a SQL statement that returns a single ResultSet.
  34.      *
  35.      * @param sql typically this is a static SQL SELECT statement
  36.      * @return a ResultSet that contains the data produced by the
  37.      * query; never null 
  38.      * @exception SQLException if a database access error occurs
  39.      */
  40.     ResultSet executeQuery(String sql) throws SQLException;
  41.  
  42.     /**
  43.      * Executes an SQL INSERT, UPDATE or DELETE statement. In addition,
  44.      * SQL statements that return nothing, such as SQL DDL statements,
  45.      * can be executed.
  46.      *
  47.      * @param sql a SQL INSERT, UPDATE or DELETE statement or a SQL
  48.      * statement that returns nothing
  49.      * @return either the row count for INSERT, UPDATE or DELETE or 0
  50.      * for SQL statements that return nothing
  51.      * @exception SQLException if a database access error occurs
  52.      */
  53.     int executeUpdate(String sql) throws SQLException;
  54.  
  55.     /**
  56.      * Releases this <code>Statement</code> object's database 
  57.      * and JDBC resources immediately instead of waiting for
  58.      * this to happen when it is automatically closed.
  59.      * It is generally good practice to release resources as soon as
  60.      * you are finished with them to avoid tying up database
  61.      * resources.
  62.      * <P><B>Note:</B> A Statement is automatically closed when it is
  63.      * garbage collected. When a Statement is closed, its current
  64.      * ResultSet, if one exists, is also closed.  
  65.      *
  66.      * @exception SQLException if a database access error occurs
  67.      */
  68.     void close() throws SQLException;
  69.  
  70.     //----------------------------------------------------------------------
  71.  
  72.     /**
  73.      * Returns the maximum number of bytes allowed
  74.      * for any column value. 
  75.      * This limit is the maximum number of bytes that can be
  76.      * returned for any column value.
  77.      * The limit applies only to BINARY,
  78.      * VARBINARY, LONGVARBINARY, CHAR, VARCHAR, and LONGVARCHAR
  79.      * columns.  If the limit is exceeded, the excess data is silently
  80.      * discarded.
  81.      *
  82.      * @return the current max column size limit; zero means unlimited 
  83.      * @exception SQLException if a database access error occurs
  84.      */
  85.     int getMaxFieldSize() throws SQLException;
  86.     
  87.     /**
  88.      * Sets the limit for the maximum number of bytes in a column to
  89.      * the given number of bytes.  This is the maximum number of bytes 
  90.      * that can be returned for any column value.  This limit applies
  91.      * only to BINARY, VARBINARY, LONGVARBINARY, CHAR, VARCHAR, and
  92.      * LONGVARCHAR fields.  If the limit is exceeded, the excess data
  93.      * is silently discarded. For maximum portability, use values
  94.      * greater than 256.
  95.      *
  96.      * @param max the new max column size limit; zero means unlimited 
  97.      * @exception SQLException if a database access error occurs
  98.      */
  99.     void setMaxFieldSize(int max) throws SQLException;
  100.  
  101.     /**
  102.      * Retrieves the maximum number of rows that a
  103.      * ResultSet can contain.  If the limit is exceeded, the excess
  104.      * rows are silently dropped.
  105.      *
  106.      * @return the current max row limit; zero means unlimited
  107.      * @exception SQLException if a database access error occurs
  108.      */
  109.     int getMaxRows() throws SQLException;
  110.  
  111.     /**
  112.      * Sets the limit for the maximum number of rows that any
  113.      * ResultSet can contain to the given number.
  114.      * If the limit is exceeded, the excess
  115.      * rows are silently dropped.
  116.      *
  117.      * @param max the new max rows limit; zero means unlimited 
  118.      * @exception SQLException if a database access error occurs
  119.      */
  120.     void setMaxRows(int max) throws SQLException;
  121.  
  122.     /**
  123.      * Sets escape processing on or off.
  124.      * If escape scanning is on (the default), the driver will do
  125.      * escape substitution before sending the SQL to the database.
  126.      *
  127.      * Note: Since prepared statements have usually been parsed prior
  128.      * to making this call, disabling escape processing for prepared
  129.      * statements will have no effect.
  130.      *
  131.      * @param enable true to enable; false to disable
  132.      * @exception SQLException if a database access error occurs
  133.      */
  134.     void setEscapeProcessing(boolean enable) throws SQLException;
  135.  
  136.     /**
  137.      * Retrieves the number of seconds the driver will
  138.      * wait for a Statement to execute. If the limit is exceeded, a
  139.      * SQLException is thrown.
  140.      *
  141.      * @return the current query timeout limit in seconds; zero means unlimited 
  142.      * @exception SQLException if a database access error occurs
  143.      */
  144.     int getQueryTimeout() throws SQLException;
  145.  
  146.     /**
  147.      * Sets the number of seconds the driver will
  148.      * wait for a Statement to execute to the given number of seconds.
  149.      * If the limit is exceeded, a SQLException is thrown.
  150.      *
  151.      * @param seconds the new query timeout limit in seconds; zero means 
  152.      * unlimited 
  153.      * @exception SQLException if a database access error occurs
  154.      */
  155.     void setQueryTimeout(int seconds) throws SQLException;
  156.  
  157.     /**
  158.      * Cancels this <code>Statement</code> object if both the DBMS and
  159.      * driver support aborting an SQL statement.
  160.      * This method can be used by one thread to cancel a statement that
  161.      * is being executed by another thread.
  162.      *
  163.      * @exception SQLException if a database access error occurs
  164.      */
  165.     void cancel() throws SQLException;
  166.  
  167.     /**
  168.      * Retrieves the first warning reported by calls on this Statement.
  169.      * Subsequent Statement warnings will be chained to this
  170.      * SQLWarning.
  171.      *
  172.      * <p>The warning chain is automatically cleared each time
  173.      * a statement is (re)executed.
  174.      *
  175.      * <P><B>Note:</B> If you are processing a ResultSet, any
  176.      * warnings associated with ResultSet reads will be chained on the
  177.      * ResultSet object.
  178.      *
  179.      * @return the first SQLWarning or null 
  180.      * @exception SQLException if a database access error occurs
  181.      */
  182.     SQLWarning getWarnings() throws SQLException;
  183.  
  184.     /**
  185.      * Clears all the warnings reported on this <code>Statement</code>
  186.      * object. After a call to this method,
  187.      * the method <code>getWarnings</code> will return 
  188.      * null until a new warning is reported for this Statement.  
  189.      *
  190.      * @exception SQLException if a database access error occurs
  191.      */
  192.     void clearWarnings() throws SQLException;
  193.  
  194.     /**
  195.      * Defines the SQL cursor name that will be used by
  196.      * subsequent Statement <code>execute</code> methods. This name can then be
  197.      * used in SQL positioned update/delete statements to identify the
  198.      * current row in the ResultSet generated by this statement.  If
  199.      * the database doesn't support positioned update/delete, this
  200.      * method is a noop.  To insure that a cursor has the proper isolation
  201.      * level to support updates, the cursor's SELECT statement should be
  202.      * of the form 'select for update ...'. If the 'for update' phrase is 
  203.      * omitted, positioned updates may fail.
  204.      *
  205.      * <P><B>Note:</B> By definition, positioned update/delete
  206.      * execution must be done by a different Statement than the one
  207.      * which generated the ResultSet being used for positioning. Also,
  208.      * cursor names must be unique within a connection.
  209.      *
  210.      * @param name the new cursor name, which must be unique within
  211.      *             a connection
  212.      * @exception SQLException if a database access error occurs
  213.      */
  214.     void setCursorName(String name) throws SQLException;
  215.     
  216.     //----------------------- Multiple Results --------------------------
  217.  
  218.     /**
  219.      * Executes a SQL statement that may return multiple results.
  220.      * Under some (uncommon) situations a single SQL statement may return
  221.      * multiple result sets and/or update counts.  Normally you can ignore
  222.      * this unless you are (1) executing a stored procedure that you know may
  223.      * return multiple results or (2) you are dynamically executing an
  224.      * unknown SQL string.  The  methods <code>execute</code>,
  225.      * <code>getMoreResults</code>, <code>getResultSet</code>,
  226.      * and <code>getUpdateCount</code> let you navigate through multiple results.
  227.      *
  228.      * The <code>execute</code> method executes a SQL statement and indicates the
  229.      * form of the first result.  You can then use getResultSet or
  230.      * getUpdateCount to retrieve the result, and getMoreResults to
  231.      * move to any subsequent result(s).
  232.      *
  233.      * @param sql any SQL statement
  234.      * @return true if the next result is a ResultSet; false if it is
  235.      * an update count or there are no more results
  236.      * @exception SQLException if a database access error occurs
  237.      * @see #getResultSet
  238.      * @see #getUpdateCount
  239.      * @see #getMoreResults 
  240.      */
  241.     boolean execute(String sql) throws SQLException;
  242.     
  243.     /**
  244.      *  Returns the current result as a <code>ResultSet</code> object. 
  245.      *  This method should be called only once per result.
  246.      *
  247.      * @return the current result as a ResultSet; null if the result
  248.      * is an update count or there are no more results
  249.      * @exception SQLException if a database access error occurs
  250.      * @see #execute 
  251.      */
  252.     ResultSet getResultSet() throws SQLException; 
  253.  
  254.     /**
  255.      *  Returns the current result as an update count;
  256.      *  if the result is a ResultSet or there are no more results, -1
  257.      *  is returned. 
  258.      *  This method should be called only once per result.
  259.      * 
  260.      * @return the current result as an update count; -1 if it is a
  261.      * ResultSet or there are no more results
  262.      * @exception SQLException if a database access error occurs
  263.      * @see #execute 
  264.      */
  265.     int getUpdateCount() throws SQLException; 
  266.  
  267.     /**
  268.      * Moves to a Statement's next result.  It returns true if 
  269.      * this result is a ResultSet.  This method also implicitly
  270.      * closes any current ResultSet obtained with getResultSet.
  271.      *
  272.      * There are no more results when (!getMoreResults() &&
  273.      * (getUpdateCount() == -1)
  274.      *
  275.      * @return true if the next result is a ResultSet; false if it is
  276.      * an update count or there are no more results
  277.      * @exception SQLException if a database access error occurs
  278.      * @see #execute 
  279.      */
  280.     boolean getMoreResults() throws SQLException; 
  281.  
  282.  
  283.     //--------------------------JDBC 2.0-----------------------------
  284.  
  285.  
  286.     /**
  287.      * JDBC 2.0
  288.      *
  289.      * Gives the driver a hint as to the direction in which
  290.      * the rows in a result set
  291.      * will be processed. The hint applies only to result sets created 
  292.      * using this Statement object.  The default value is 
  293.      * ResultSet.FETCH_FORWARD.
  294.      * <p>Note that this method sets the default fetch direction for 
  295.      * result sets generated by this <code>Statement</code> object.
  296.      * Each result set has its own methods for getting and setting
  297.      * its own fetch direction.
  298.      * @param direction the initial direction for processing rows
  299.      * @exception SQLException if a database access error occurs
  300.      * or the given direction
  301.      * is not one of ResultSet.FETCH_FORWARD, ResultSet.FETCH_REVERSE, or
  302.      * ResultSet.FETCH_UNKNOWN
  303.      */
  304.     void setFetchDirection(int direction) throws SQLException;
  305.  
  306.     /**
  307.      * JDBC 2.0
  308.      *
  309.      * Retrieves the direction for fetching rows from
  310.      * database tables that is the default for result sets
  311.      * generated from this <code>Statement</code> object.
  312.      * If this <code>Statement</code> object has not set
  313.      * a fetch direction by calling the method <code>setFetchDirection</code>,
  314.      * the return value is implementation-specific.
  315.      *
  316.      * @return the default fetch direction for result sets generated
  317.      *          from this <code>Statement</code> object
  318.      * @exception SQLException if a database access error occurs
  319.      */
  320.     int getFetchDirection() throws SQLException;
  321.  
  322.     /**
  323.      * JDBC 2.0
  324.      *
  325.      * Gives the JDBC driver a hint as to the number of rows that should 
  326.      * be fetched from the database when more rows are needed.  The number 
  327.      * of rows specified affects only result sets created using this 
  328.      * statement. If the value specified is zero, then the hint is ignored.
  329.      * The default value is zero.
  330.      *
  331.      * @param rows the number of rows to fetch
  332.      * @exception SQLException if a database access error occurs, or the
  333.      * condition 0 <= rows <= this.getMaxRows() is not satisfied.
  334.      */
  335.     void setFetchSize(int rows) throws SQLException;
  336.   
  337.     /**
  338.      * JDBC 2.0
  339.      *
  340.      * Retrieves the number of result set rows that is the default 
  341.      * fetch size for result sets
  342.      * generated from this <code>Statement</code> object.
  343.      * If this <code>Statement</code> object has not set
  344.      * a fetch size by calling the method <code>setFetchSize</code>,
  345.      * the return value is implementation-specific.
  346.      * @return the default fetch size for result sets generated
  347.      *          from this <code>Statement</code> object
  348.      * @exception SQLException if a database access error occurs
  349.      */
  350.     int getFetchSize() throws SQLException;
  351.  
  352.     /**
  353.      * JDBC 2.0
  354.      *
  355.      * Retrieves the result set concurrency.
  356.      */
  357.     int getResultSetConcurrency() throws SQLException;
  358.  
  359.     /**
  360.      * JDBC 2.0
  361.      *
  362.      * Determine the result set type.
  363.      */
  364.     int getResultSetType()  throws SQLException;
  365.  
  366.     /**
  367.      * JDBC 2.0
  368.      *
  369.      * Adds a SQL command to the current batch of commmands for the statement.
  370.      * This method is optional.
  371.      *
  372.      * @param sql typically this is a static SQL INSERT or UPDATE statement
  373.      * @exception SQLException if a database access error occurs, or the
  374.      * driver does not support batch statements
  375.      */
  376.     void addBatch( String sql ) throws SQLException;
  377.  
  378.     /**
  379.      * JDBC 2.0
  380.      *
  381.      * Makes the set of commands in the current batch empty.
  382.      * This method is optional.
  383.      *
  384.      * @exception SQLException if a database access error occurs or the
  385.      * driver does not support batch statements
  386.      */
  387.     void clearBatch() throws SQLException;
  388.  
  389.     /**
  390.      * JDBC 2.0
  391.      * 
  392.      * Submits a batch of commands to the database for execution.
  393.      * This method is optional.
  394.      *
  395.      * @return an array of update counts containing one element for each
  396.      * command in the batch.  The array is ordered according 
  397.      * to the order in which commands were inserted into the batch.
  398.      * @exception SQLException if a database access error occurs or the
  399.      * driver does not support batch statements
  400.      */
  401.     int[] executeBatch() throws SQLException;
  402.  
  403.     /**
  404.      * JDBC 2.0
  405.      * 
  406.      * Returns the <code>Connection</code> object
  407.      * that produced this <code>Statement</code> object.
  408.      * @return the connection that produced this statement
  409.      * @exception SQLException if a database access error occurs
  410.      */
  411.     Connection getConnection()  throws SQLException;
  412. }    
  413.