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