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 / Connection.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  16.4 KB  |  443 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)Connection.java    1.19 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. ("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 connection (session) with a specific
  19.  * database. Within the context of a Connection, SQL statements are
  20.  * executed and results are returned.
  21.  *
  22.  * <P>A Connection's database is able to provide information
  23.  * describing its tables, its supported SQL grammar, its stored
  24.  * procedures, the capabilities of this connection, and so on. This
  25.  * information is obtained with the <code>getMetaData</code> method.
  26.  *
  27.  * <P><B>Note:</B> By default the Connection automatically commits
  28.  * changes after executing each statement. If auto commit has been
  29.  * disabled, an explicit commit must be done or database changes will
  30.  * not be saved.
  31.  *
  32.  * @see DriverManager#getConnection
  33.  * @see Statement 
  34.  * @see ResultSet
  35.  * @see DatabaseMetaData
  36.  */
  37. public interface Connection {
  38.  
  39.     /**
  40.      * Creates a <code>Statement</code> object for sending
  41.      * SQL statements to the database.
  42.      * SQL statements without parameters are normally
  43.      * executed using Statement objects. If the same SQL statement 
  44.      * is executed many times, it is more efficient to use a 
  45.      * PreparedStatement
  46.      *
  47.      * JDBC 2.0
  48.      *
  49.      * Result sets created using the returned Statement will have
  50.      * forward-only type, and read-only concurrency, by default.
  51.      *
  52.      * @return a new Statement object 
  53.      * @exception SQLException if a database access error occurs
  54.      */
  55.     Statement createStatement() throws SQLException;
  56.  
  57.     /**
  58.      * Creates a <code>PreparedStatement</code> object for sending
  59.      * parameterized SQL statements to the database.
  60.      * 
  61.      * A SQL statement with or without IN parameters can be
  62.      * pre-compiled and stored in a PreparedStatement object. This
  63.      * object can then be used to efficiently execute this statement
  64.      * multiple times.
  65.      *
  66.      * <P><B>Note:</B> This method is optimized for handling
  67.      * parametric SQL statements that benefit from precompilation. If
  68.      * the driver supports precompilation,
  69.      * the method <code>prepareStatement</code> will send
  70.      * the statement to the database for precompilation. Some drivers
  71.      * may not support precompilation. In this case, the statement may
  72.      * not be sent to the database until the <code>PreparedStatement</code> is
  73.      * executed.  This has no direct effect on users; however, it does
  74.      * affect which method throws certain SQLExceptions.
  75.      *
  76.      * JDBC 2.0
  77.      *
  78.      * Result sets created using the returned PreparedStatement will have
  79.      * forward-only type and read-only concurrency, by default.
  80.      *
  81.      * @param sql a SQL statement that may contain one or more '?' IN
  82.      * parameter placeholders
  83.      * @return a new PreparedStatement object containing the
  84.      * pre-compiled statement 
  85.      * @exception SQLException if a database access error occurs
  86.      */
  87.     PreparedStatement prepareStatement(String sql)
  88.         throws SQLException;
  89.  
  90.     /**
  91.      * Creates a <code>CallableStatement</code> object for calling
  92.      * database stored procedures.
  93.      * The CallableStatement provides
  94.      * methods for setting up its IN and OUT parameters, and
  95.      * methods for executing the call to a stored procedure.
  96.      *
  97.      * <P><B>Note:</B> This method is optimized for handling stored
  98.      * procedure call statements. Some drivers may send the call
  99.      * statement to the database when the method <code>prepareCall</code>
  100.      * is done; others
  101.      * may wait until the CallableStatement is executed. This has no
  102.      * direct effect on users; however, it does affect which method
  103.      * throws certain SQLExceptions.
  104.      *
  105.      * JDBC 2.0
  106.      *
  107.      * Result sets created using the returned CallableStatement will have
  108.      * forward-only type and read-only concurrency, by default.
  109.      *
  110.      * @param sql a SQL statement that may contain one or more '?'
  111.      * parameter placeholders. Typically this  statement is a JDBC
  112.      * function call escape string.
  113.      * @return a new CallableStatement object containing the
  114.      * pre-compiled SQL statement 
  115.      * @exception SQLException if a database access error occurs
  116.      */
  117.     CallableStatement prepareCall(String sql) throws SQLException;
  118.                         
  119.     /**
  120.      * Converts the given SQL statement into the system's native SQL grammar.
  121.      * A driver may convert the JDBC sql grammar into its system's
  122.      * native SQL grammar prior to sending it; this method returns the
  123.      * native form of the statement that the driver would have sent.
  124.      *
  125.      * @param sql a SQL statement that may contain one or more '?'
  126.      * parameter placeholders
  127.      * @return the native form of this statement
  128.      * @exception SQLException if a database access error occurs
  129.      */
  130.     String nativeSQL(String sql) throws SQLException;
  131.  
  132.     /**
  133.      * Sets this connection's auto-commit mode.
  134.      * If a connection is in auto-commit mode, then all its SQL
  135.      * statements will be executed and committed as individual
  136.      * transactions.  Otherwise, its SQL statements are grouped into
  137.      * transactions that are terminated by a call to either
  138.      * the method <code>commit</code> or the method <code>rollback</code>.
  139.      * By default, new connections are in auto-commit
  140.      * mode.
  141.      *
  142.      * The commit occurs when the statement completes or the next
  143.      * execute occurs, whichever comes first. In the case of
  144.      * statements returning a ResultSet, the statement completes when
  145.      * the last row of the ResultSet has been retrieved or the
  146.      * ResultSet has been closed. In advanced cases, a single
  147.      * statement may return multiple results as well as output
  148.      * parameter values. In these cases the commit occurs when all results and
  149.      * output parameter values have been retrieved.
  150.      *
  151.      * @param autoCommit true enables auto-commit; false disables
  152.      * auto-commit.  
  153.      * @exception SQLException if a database access error occurs
  154.      */
  155.     void setAutoCommit(boolean autoCommit) throws SQLException;
  156.  
  157.     /**
  158.      * Gets the current auto-commit state.
  159.      *
  160.      * @return the current state of auto-commit mode
  161.      * @exception SQLException if a database access error occurs
  162.      * @see #setAutoCommit 
  163.      */
  164.     boolean getAutoCommit() throws SQLException;
  165.  
  166.     /**
  167.      * Makes all changes made since the previous
  168.      * commit/rollback permanent and releases any database locks
  169.      * currently held by the Connection. This method should be
  170.      * used only when auto-commit mode has been disabled.
  171.      *
  172.      * @exception SQLException if a database access error occurs
  173.      * @see #setAutoCommit 
  174.      */
  175.     void commit() throws SQLException;
  176.  
  177.     /**
  178.      * Drops all changes made since the previous
  179.      * commit/rollback and releases any database locks currently held
  180.      * by this Connection. This method should be used only when auto-
  181.      * commit has been disabled.
  182.      *
  183.      * @exception SQLException if a database access error occurs
  184.      * @see #setAutoCommit 
  185.      */
  186.     void rollback() throws SQLException;
  187.  
  188.     /**
  189.      * Releases a Connection's database and JDBC resources
  190.      * immediately instead of waiting for
  191.      * them to be automatically released.
  192.      *
  193.      * <P><B>Note:</B> A Connection is automatically closed when it is
  194.      * garbage collected. Certain fatal errors also result in a closed
  195.      * Connection.
  196.      *
  197.      * @exception SQLException if a database access error occurs
  198.      */
  199.     void close() throws SQLException;
  200.  
  201.     /**
  202.      * Tests to see if a Connection is closed.
  203.      *
  204.      * @return true if the connection is closed; false if it's still open
  205.      * @exception SQLException if a database access error occurs
  206.      */
  207.     boolean isClosed() throws SQLException;
  208.  
  209.     //======================================================================
  210.     // Advanced features:
  211.  
  212.     /**
  213.      * Gets the metadata regarding this connection's database.
  214.      * A Connection's database is able to provide information
  215.      * describing its tables, its supported SQL grammar, its stored
  216.      * procedures, the capabilities of this connection, and so on. This
  217.      * information is made available through a DatabaseMetaData
  218.      * object.
  219.      *
  220.      * @return a DatabaseMetaData object for this Connection 
  221.      * @exception SQLException if a database access error occurs
  222.      */
  223.     DatabaseMetaData getMetaData() throws SQLException;
  224.  
  225.     /**
  226.      * Puts this connection in read-only mode as a hint to enable 
  227.      * database optimizations.
  228.      *
  229.      * <P><B>Note:</B> This method cannot be called while in the
  230.      * middle of a transaction.
  231.      *
  232.      * @param readOnly true enables read-only mode; false disables
  233.      * read-only mode.  
  234.      * @exception SQLException if a database access error occurs
  235.      */
  236.     void setReadOnly(boolean readOnly) throws SQLException;
  237.  
  238.     /**
  239.      * Tests to see if the connection is in read-only mode.
  240.      *
  241.      * @return true if connection is read-only and false otherwise
  242.      * @exception SQLException if a database access error occurs
  243.      */
  244.     boolean isReadOnly() throws SQLException;
  245.  
  246.     /**
  247.      * Sets a catalog name in order to select     
  248.      * a subspace of this Connection's database in which to work.
  249.      * If the driver does not support catalogs, it will
  250.      * silently ignore this request.
  251.      *
  252.      * @exception SQLException if a database access error occurs
  253.      */
  254.     void setCatalog(String catalog) throws SQLException;
  255.  
  256.     /**
  257.      * Returns the Connection's current catalog name.
  258.      *
  259.      * @return the current catalog name or null
  260.      * @exception SQLException if a database access error occurs
  261.      */
  262.     String getCatalog() throws SQLException;
  263.  
  264.     /**
  265.      * Indicates that transactions are not supported. 
  266.      */
  267.     int TRANSACTION_NONE         = 0;
  268.  
  269.     /**
  270.      * Dirty reads, non-repeatable reads and phantom reads can occur.
  271.      * This level allows a row changed by one transaction to be read
  272.      * by another transaction before any changes in that row have been
  273.      * committed (a "dirty read").  If any of the changes are rolled back, 
  274.      * the second transaction will have retrieved an invalid row.
  275.      */
  276.     int TRANSACTION_READ_UNCOMMITTED = 1;
  277.  
  278.     /**
  279.      * Dirty reads are prevented; non-repeatable reads and phantom
  280.      * reads can occur.  This level only prohibits a transaction
  281.      * from reading a row with uncommitted changes in it.
  282.      */
  283.     int TRANSACTION_READ_COMMITTED   = 2;
  284.  
  285.     /**
  286.      * Dirty reads and non-repeatable reads are prevented; phantom
  287.      * reads can occur.  This level prohibits a transaction from
  288.      * reading a row with uncommitted changes in it, and it also
  289.      * prohibits the situation where one transaction reads a row,
  290.      * a second transaction alters the row, and the first transaction
  291.      * rereads the row, getting different values the second time
  292.      * (a "non-repeatable read").
  293.      */
  294.     int TRANSACTION_REPEATABLE_READ  = 4;
  295.  
  296.     /**
  297.      * Dirty reads, non-repeatable reads and phantom reads are prevented.
  298.      * This level includes the prohibitions in
  299.      * TRANSACTION_REPEATABLE_READ and further prohibits the 
  300.      * situation where one transaction reads all rows that satisfy
  301.      * a WHERE condition, a second transaction inserts a row that
  302.      * satisfies that WHERE condition, and the first transaction
  303.      * rereads for the same condition, retrieving the additional
  304.      * "phantom" row in the second read.
  305.      */
  306.     int TRANSACTION_SERIALIZABLE     = 8;
  307.  
  308.     /**
  309.      * Attempts to change the transaction
  310.      * isolation level to the one given.
  311.      * The constants defined in the interface <code>Connection</code>
  312.      * are the possible transaction isolation levels.
  313.      *
  314.      * <P><B>Note:</B> This method cannot be called while
  315.      * in the middle of a transaction.
  316.      *
  317.      * @param level one of the TRANSACTION_* isolation values with the
  318.      * exception of TRANSACTION_NONE; some databases may not support
  319.      * other values
  320.      * @exception SQLException if a database access error occurs
  321.      * @see DatabaseMetaData#supportsTransactionIsolationLevel 
  322.      */
  323.     void setTransactionIsolation(int level) throws SQLException;
  324.  
  325.     /**
  326.      * Gets this Connection's current transaction isolation level.
  327.      *
  328.      * @return the current TRANSACTION_* mode value
  329.      * @exception SQLException if a database access error occurs
  330.      */
  331.     int getTransactionIsolation() throws SQLException;
  332.  
  333.     /**
  334.      * Returns the first warning reported by calls on this Connection.
  335.      *
  336.      * <P><B>Note:</B> Subsequent warnings will be chained to this
  337.      * SQLWarning.
  338.      *
  339.      * @return the first SQLWarning or null 
  340.      * @exception SQLException if a database access error occurs
  341.      */
  342.     SQLWarning getWarnings() throws SQLException;
  343.  
  344.     /**
  345.      * Clears all warnings reported for this <code>Connection</code> object.    
  346.      * After a call to this method, the method <code>getWarnings</code>
  347.      * returns null until a new warning is
  348.      * reported for this Connection.  
  349.      *
  350.      * @exception SQLException if a database access error occurs
  351.      */
  352.     void clearWarnings() throws SQLException;
  353.  
  354.  
  355.     //--------------------------JDBC 2.0-----------------------------
  356.  
  357.     /**
  358.      * JDBC 2.0
  359.      *
  360.      * Creates a <code>Statement</code> object that will generate
  361.      * <code>ResultSet</code> objects with the given type and concurrency.
  362.      * This method is the same as the <code>createStatement</code> method
  363.      * above, but it allows the default result set
  364.      * type and result set concurrency type to be overridden.
  365.      *
  366.      * @param resultSetType a result set type; see ResultSet.TYPE_XXX
  367.      * @param resultSetConcurrency a concurrency type; see ResultSet.CONCUR_XXX
  368.      * @return a new Statement object 
  369.      * @exception SQLException if a database access error occurs
  370.      */
  371.     Statement createStatement(int resultSetType, int resultSetConcurrency) 
  372.       throws SQLException;
  373.  
  374.     /**
  375.      * JDBC 2.0
  376.      *
  377.      * Creates a <code>PreparedStatement</code> object that will generate
  378.      * <code>ResultSet</code> objects with the given type and concurrency.
  379.      * This method is the same as the <code>prepareStatement</code> method
  380.      * above, but it allows the default result set
  381.      * type and result set concurrency type to be overridden.
  382.      *
  383.      * @param resultSetType a result set type; see ResultSet.TYPE_XXX
  384.      * @param resultSetConcurrency a concurrency type; see ResultSet.CONCUR_XXX
  385.      * @return a new PreparedStatement object containing the
  386.      * pre-compiled SQL statement 
  387.      * @exception SQLException if a database access error occurs
  388.      */
  389.      PreparedStatement prepareStatement(String sql, int resultSetType, 
  390.                     int resultSetConcurrency)
  391.        throws SQLException;
  392.  
  393.     /**
  394.      * JDBC 2.0
  395.      *
  396.      * Creates a <code>CallableStatement</code> object that will generate
  397.      * <code>ResultSet</code> objects with the given type and concurrency.
  398.      * This method is the same as the <code>prepareCall</code> method
  399.      * above, but it allows the default result set
  400.      * type and result set concurrency type to be overridden.
  401.      *
  402.      * @param resultSetType a result set type; see ResultSet.TYPE_XXX
  403.      * @param resultSetConcurrency a concurrency type; see ResultSet.CONCUR_XXX
  404.      * @return a new CallableStatement object containing the
  405.      * pre-compiled SQL statement 
  406.      * @exception SQLException if a database access error occurs
  407.      */
  408.     CallableStatement prepareCall(String sql, int resultSetType, 
  409.                  int resultSetConcurrency) throws SQLException;
  410.  
  411.     /**
  412.      * JDBC 2.0
  413.      *
  414.      * Gets the type map object associated with this connection.
  415.      * Unless the application has added an entry to the type map,
  416.      * the map returned will be empty.
  417.      *
  418.      * @return the <code>java.util.Map</code> object associated 
  419.      *         with this <code>Connection</code> object
  420.      */
  421.     java.util.Map getTypeMap() throws SQLException;
  422.  
  423.     /**
  424.      * JDBC 2.0
  425.      *
  426.      * Installs the given type map as the type map for
  427.      * this connection.  The type map will be used for the
  428.      * custom mapping of SQL structured types and distinct types.
  429.      *
  430.      * @param the <code>java.util.Map</code> object to install
  431.      *        as the replacement for this <code>Connection</code>
  432.      *        object's default type map
  433.      */
  434.     void setTypeMap(java.util.Map map) throws SQLException;
  435. }
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.