home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / sql / Statement.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  15.1 KB  |  425 lines

  1. /*
  2.  * @(#)Statement.java    1.11 98/03/18
  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. ("Confidential Information").  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>A Statement object is 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 execute 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.      * Execute 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.      * Execute a 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.      * In many cases, it is desirable to immediately release a
  57.      * Statements's database and JDBC resources instead of waiting for
  58.      * this to happen when it is automatically closed; the close
  59.      * method provides this immediate release.
  60.      *
  61.      * <P><B>Note:</B> A Statement is automatically closed when it is
  62.      * garbage collected. When a Statement is closed, its current
  63.      * ResultSet, if one exists, is also closed.  
  64.      *
  65.      * @exception SQLException if a database-access error occurs.
  66.      */
  67.     void close() throws SQLException;
  68.  
  69.     //----------------------------------------------------------------------
  70.  
  71.     /**
  72.      * The maxFieldSize limit (in bytes) is the maximum amount of data
  73.      * returned for any column value; it only applies to BINARY,
  74.      * VARBINARY, LONGVARBINARY, CHAR, VARCHAR, and LONGVARCHAR
  75.      * columns.  If the limit is exceeded, the excess data is silently
  76.      * discarded.
  77.      *
  78.      * @return the current max column size limit; zero means unlimited 
  79.      * @exception SQLException if a database-access error occurs.
  80.      */
  81.     int getMaxFieldSize() throws SQLException;
  82.     
  83.     /**
  84.      * The maxFieldSize limit (in bytes) is set to limit the size of
  85.      * data that can be returned for any column value; it only applies
  86.      * to BINARY, VARBINARY, LONGVARBINARY, CHAR, VARCHAR, and
  87.      * LONGVARCHAR fields.  If the limit is exceeded, the excess data
  88.      * is silently discarded. For maximum portability use values
  89.      * greater than 256.
  90.      *
  91.      * @param max the new max column size limit; zero means unlimited 
  92.      * @exception SQLException if a database-access error occurs.
  93.      */
  94.     void setMaxFieldSize(int max) throws SQLException;
  95.  
  96.     /**
  97.      * The maxRows limit is the maximum number of rows that a
  98.      * ResultSet can contain.  If the limit is exceeded, the excess
  99.      * rows are silently dropped.
  100.      *
  101.      * @return the current max row limit; zero means unlimited
  102.      * @exception SQLException if a database-access error occurs.
  103.      */
  104.     int getMaxRows() throws SQLException;
  105.  
  106.     /**
  107.      * The maxRows limit is set to limit the number of rows that any
  108.      * ResultSet can contain.  If the limit is exceeded, the excess
  109.      * rows are silently dropped.
  110.      *
  111.      * @param max the new max rows limit; zero means unlimited 
  112.      * @exception SQLException if a database-access error occurs.
  113.      */
  114.     void setMaxRows(int max) throws SQLException;
  115.  
  116.     /**
  117.      * If escape scanning is on (the default), the driver will do
  118.      * escape substitution before sending the SQL to the database.
  119.      *
  120.      * Note: Since prepared statements have usually been parsed prior
  121.      * to making this call, disabling escape processing for prepared
  122.      * statements will like have no affect.
  123.      *
  124.      * @param enable true to enable; false to disable
  125.      * @exception SQLException if a database-access error occurs.
  126.      */
  127.     void setEscapeProcessing(boolean enable) throws SQLException;
  128.  
  129.     /**
  130.      * The queryTimeout limit is the number of seconds the driver will
  131.      * wait for a Statement to execute. If the limit is exceeded, a
  132.      * SQLException is thrown.
  133.      *
  134.      * @return the current query timeout limit in seconds; zero means unlimited 
  135.      * @exception SQLException if a database-access error occurs.
  136.      */
  137.     int getQueryTimeout() throws SQLException;
  138.  
  139.     /**
  140.      * The queryTimeout limit is the number of seconds the driver will
  141.      * wait for a Statement to execute. If the limit is exceeded, a
  142.      * SQLException is thrown.
  143.      *
  144.      * @param seconds the new query timeout limit in seconds; zero means 
  145.      * unlimited 
  146.      * @exception SQLException if a database-access error occurs.
  147.      */
  148.     void setQueryTimeout(int seconds) throws SQLException;
  149.  
  150.     /**
  151.      * Cancel can be used by one thread to cancel a statement that
  152.      * is being executed by another thread.
  153.      *
  154.      * @exception SQLException if a database-access error occurs.
  155.      */
  156.     void cancel() throws SQLException;
  157.  
  158.     /**
  159.      * The first warning reported by calls on this Statement is
  160.      * returned.  A Statment's execute methods clear its SQLWarning
  161.      * chain. Subsequent Statement warnings will be chained to this
  162.      * SQLWarning.
  163.      *
  164.      * <p>The warning chain is automatically cleared each time
  165.      * a statement is (re)executed.
  166.      *
  167.      * <P><B>Note:</B> If you are processing a ResultSet then any
  168.      * warnings associated with ResultSet reads will be chained on the
  169.      * ResultSet object.
  170.      *
  171.      * @return the first SQLWarning or null 
  172.      * @exception SQLException if a database-access error occurs.
  173.      */
  174.     SQLWarning getWarnings() throws SQLException;
  175.  
  176.     /**
  177.      * After this call, getWarnings returns null until a new warning is
  178.      * reported for this Statement.  
  179.      *
  180.      * @exception SQLException if a database-access error occurs.
  181.      */
  182.     void clearWarnings() throws SQLException;
  183.  
  184.     /**
  185.      * setCursorname defines the SQL cursor name that will be used by
  186.      * subsequent Statement execute methods. This name can then be
  187.      * used in SQL positioned update/delete statements to identify the
  188.      * current row in the ResultSet generated by this statement.  If
  189.      * the database doesn't support positioned update/delete, this
  190.      * method is a noop.
  191.      *
  192.      * <P><B>Note:</B> By definition, positioned update/delete
  193.      * execution must be done by a different Statement than the one
  194.      * which generated the ResultSet being used for positioning. Also,
  195.      * cursor names must be unique within a Connection.
  196.      *
  197.      * @param name the new cursor name.  
  198.      * @exception SQLException if a database-access error occurs.
  199.      */
  200.     void setCursorName(String name) throws SQLException;
  201.     
  202.     //----------------------- Multiple Results --------------------------
  203.  
  204.     /**
  205.      * Execute a SQL statement that may return multiple results.
  206.      * Under some (uncommon) situations a single SQL statement may return
  207.      * multiple result sets and/or update counts.  Normally you can ignore
  208.      * this, unless you're executing a stored procedure that you know may
  209.      * return multiple results, or unless you're dynamically executing an
  210.      * unknown SQL string.  The "execute", "getMoreResults", "getResultSet"
  211.      * and "getUpdateCount" methods let you navigate through multiple results.
  212.      *
  213.      * The "execute" method executes a SQL statement and indicates the
  214.      * form of the first result.  You can then use getResultSet or
  215.      * getUpdateCount to retrieve the result, and getMoreResults to
  216.      * move to any subsequent result(s).
  217.      *
  218.      * @param sql any SQL statement
  219.      * @return true if the next result is a ResultSet; false if it is
  220.      * an update count or there are no more results
  221.      * @exception SQLException if a database-access error occurs.
  222.      * @see #getResultSet
  223.      * @see #getUpdateCount
  224.      * @see #getMoreResults 
  225.      */
  226.     boolean execute(String sql) throws SQLException;
  227.     
  228.     /**
  229.      *  getResultSet returns the current result as a ResultSet.  It
  230.      *  should only be called once per result.
  231.      *
  232.      * @return the current result as a ResultSet; null if the result
  233.      * is an update count or there are no more results
  234.      * @exception SQLException if a database-access error occurs.
  235.      * @see #execute 
  236.      */
  237.     ResultSet getResultSet() throws SQLException; 
  238.  
  239.     /**
  240.      *  getUpdateCount returns the current result as an update count;
  241.      *  if the result is a ResultSet or there are no more results, -1
  242.      *  is returned.  It should only be called once per result.
  243.      * 
  244.      * @return the current result as an update count; -1 if it is a
  245.      * ResultSet or there are no more results
  246.      * @exception SQLException if a database-access error occurs.
  247.      * @see #execute 
  248.      */
  249.     int getUpdateCount() throws SQLException; 
  250.  
  251.     /**
  252.      * getMoreResults moves to a Statement's next result.  It returns true if 
  253.      * this result is a ResultSet.  getMoreResults also implicitly
  254.      * closes any current ResultSet obtained with getResultSet.
  255.      *
  256.      * There are no more results when (!getMoreResults() &&
  257.      * (getUpdateCount() == -1)
  258.      *
  259.      * @return true if the next result is a ResultSet; false if it is
  260.      * an update count or there are no more results
  261.      * @exception SQLException if a database-access error occurs.
  262.      * @see #execute 
  263.      */
  264.     boolean getMoreResults() throws SQLException; 
  265.  
  266.  
  267.     //--------------------------JDBC 2.0-----------------------------
  268.  
  269.  
  270.     /**
  271.      * JDBC 2.0
  272.      *
  273.      * Give a hint as to the direction in which the rows in a result set
  274.      * will be processed. The hint applies only to result sets created 
  275.      * using this Statement object.  The default value is 
  276.      * ResultSet.FETCH_FORWARD.
  277.      *
  278.      * @param direction the initial direction for processing rows
  279.      * @exception SQLException if a database-access error occurs or direction
  280.      * is not one of ResultSet.FETCH_FORWARD, ResultSet.FETCH_REVERSE, or
  281.      * ResultSet.FETCH_UNKNOWN
  282.      */
  283.     void setFetchDirection(int direction) throws SQLException;
  284.  
  285.     /**
  286.      * JDBC 2.0
  287.      *
  288.      * Determine the fetch direction.
  289.      *
  290.      * @return the default fetch direction
  291.      * @exception SQLException if a database-access error occurs
  292.      */
  293.     int getFetchDirection() throws SQLException;
  294.  
  295.     /**
  296.      * JDBC 2.0
  297.      *
  298.      * Give the JDBC driver a hint as to the number of rows that should 
  299.      * be fetched from the database when more rows are needed.  The number 
  300.      * of rows specified only affects result sets created using this 
  301.      * statement. If the value specified is zero, then the hint is ignored.
  302.      * The default value is zero.
  303.      *
  304.      * @param rows the number of rows to fetch
  305.      * @exception SQLException if a database-access error occurs, or the
  306.      * condition 0 <= rows <= this.getMaxRows() is not satisfied.
  307.      */
  308.     void setFetchSize(int rows) throws SQLException;
  309.   
  310.     /**
  311.      * JDBC 2.0
  312.      *
  313.      * Determine the default fetch size.
  314.      */
  315.     int getFetchSize() throws SQLException;
  316.  
  317.     /**
  318.      * JDBC 2.0
  319.      *
  320.      * Give the JDBC driver a hint as to the number of keys contained in the
  321.      * keyset of a TYPE_KEYSET result set.  The number of keys specified only 
  322.      * affects result sets created using this statement. If the value 
  323.      * specified is zero, then result sets produced by the statement will
  324.      * have a keyset size equal to the size of the result set. The default 
  325.      * value is zero.
  326.      *
  327.      * @param rows the number of keys in a keyset
  328.      * @exception SQLException if a database-access error occurs, or the
  329.      * condition this.getFetchSize() <= keys <= this.getMaxRows() is not 
  330.      * satisfied.
  331.      */
  332.     void setKeysetSize(int keys) throws SQLException;
  333.  
  334.     /**
  335.      * JDBC 2.0
  336.      *
  337.      * Determine the default keyset size.
  338.      */
  339.     int getKeysetSize() throws SQLException;
  340.  
  341.     /**
  342.      * JDBC 2.0
  343.      * 
  344.      * Set the concurrency value to be used for result sets
  345.      * produced by this statement.  If no value is set explicitly for
  346.      * a statement, the connection's setting is used.
  347.      *
  348.      * @param value the concurrency value
  349.      * @exception SQLException if a database-access error occurs or
  350.      * value is not one of ResultSet.CONCUR_READ_ONLY, CONCUR_LOCK, 
  351.      * CONCUR_VERSION, or CONCUR_VALUE.
  352.      */
  353.     void setResultSetConcurrency(int value) throws SQLException;
  354.  
  355.     /**
  356.      * JDBC 2.0
  357.      *
  358.      * Determine the default concurrency.
  359.      */
  360.     int getResultSetConcurrency() throws SQLException;
  361.  
  362.     /**
  363.      * JDBC 2.0
  364.      *
  365.      * Set the type for result sets produced by this statement.  If no 
  366.      * value is set explicitly for a statement, the connection's setting 
  367.      * is used.
  368.      * 
  369.      * @exception SQLException if a database-access error occurs, or
  370.      * type is not one of ResultSet.TYPE_FORWARD_ONLY, etc.
  371.      */
  372.     void setResultSetType(int value) throws SQLException;
  373.  
  374.     /**
  375.      * JDBC 2.0
  376.      *
  377.      * Determine the default result set type.
  378.      */
  379.     int getResultSetType() ;
  380.  
  381.     /**
  382.      * JDBC 2.0
  383.      *
  384.      * Adds a SQL command to the current batch of commmands for the statement.
  385.      * This method is optional.
  386.      *
  387.      * @param sql typically this is a static SQL INSERT or UPDATE statement
  388.      * @exception SQLException if a database-access error occurs, or the
  389.      * driver does not support batch statements
  390.      */
  391.     void addBatch( String sql ) throws SQLException;
  392.  
  393.     /**
  394.      * JDBC 2.0
  395.      *
  396.      * Make the set of commands in the current batch empty.
  397.      * This method is optional.
  398.      *
  399.      * @exception SQLException if a database-access error occurs, or the
  400.      * driver does not support batch statements
  401.      */
  402.     void clearBatch() throws SQLException;
  403.  
  404.     /**
  405.      * JDBC 2.0
  406.      * 
  407.      * Submit a batch of commands to the database for execution.
  408.      * This method is optional.
  409.      *
  410.      * @return an array of update counts containing one element for each
  411.      * command in the batch.  The array is ordered according 
  412.      * to the order in which commands were inserted into the batch
  413.      * @exception SQLException if a database-access error occurs, or the
  414.      * driver does not support batch statements
  415.      */
  416.     int[] executeBatch() throws SQLException;
  417.  
  418.     /**
  419.      * JDBC 2.0
  420.      * 
  421.      * Return the Connection that produced the Statement.
  422.      */
  423.     Connection getConnection()  throws SQLException;
  424. }    
  425.