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