1. /*
  2. * @(#)PreparedStatement.java 1.24 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. import java.math.BigDecimal;
  9. import java.util.Calendar;
  10. /**
  11. * An object that represents a precompiled SQL statement.
  12. * <P>A SQL statement is pre-compiled and stored in a
  13. * PreparedStatement object. This object can then be used to
  14. * efficiently execute this statement multiple times.
  15. *
  16. * <P><B>Note:</B> The setXXX methods for setting IN parameter values
  17. * must specify types that are compatible with the defined SQL type of
  18. * the input parameter. For instance, if the IN parameter has SQL type
  19. * Integer, then the method <code>setInt</code> should be used.
  20. *
  21. * <p>If arbitrary parameter type conversions are required, the method
  22. * <code>setObject</code> should be used with a target SQL type.
  23. * <br>
  24. * Example of setting a parameter; <code>con</code> is an active connection
  25. * <pre><code>
  26. * PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES
  27. * SET SALARY = ? WHERE ID = ?");
  28. * pstmt.setBigDecimal(1, 153833.00)
  29. * pstmt.setInt(2, 110592)
  30. * </code></pre>
  31. *
  32. * @see Connection#prepareStatement
  33. * @see ResultSet
  34. */
  35. public interface PreparedStatement extends Statement {
  36. /**
  37. * Executes the SQL query in this <code>PreparedStatement</code> object
  38. * and returns the result set generated by the query.
  39. *
  40. * @return a ResultSet that contains the data produced by the
  41. * query; never null
  42. * @exception SQLException if a database access error occurs
  43. */
  44. ResultSet executeQuery() throws SQLException;
  45. /**
  46. * Executes the SQL INSERT, UPDATE or DELETE statement
  47. * in this <code>PreparedStatement</code> object.
  48. * In addition,
  49. * SQL statements that return nothing, such as SQL DDL statements,
  50. * can be executed.
  51. *
  52. * @return either the row count for INSERT, UPDATE or DELETE statements;
  53. * or 0 for SQL statements that return nothing
  54. * @exception SQLException if a database access error occurs
  55. */
  56. int executeUpdate() throws SQLException;
  57. /**
  58. * Sets the designated parameter to SQL NULL.
  59. *
  60. * <P><B>Note:</B> You must specify the parameter's SQL type.
  61. *
  62. * @param parameterIndex the first parameter is 1, the second is 2, ...
  63. * @param sqlType the SQL type code defined in java.sql.Types
  64. * @exception SQLException if a database access error occurs
  65. */
  66. void setNull(int parameterIndex, int sqlType) throws SQLException;
  67. /**
  68. * Sets the designated parameter to a Java boolean value. The driver converts this
  69. * to an SQL BIT value when it sends it to the database.
  70. *
  71. * @param parameterIndex the first parameter is 1, the second is 2, ...
  72. * @param x the parameter value
  73. * @exception SQLException if a database access error occurs
  74. */
  75. void setBoolean(int parameterIndex, boolean x) throws SQLException;
  76. /**
  77. * Sets the designated parameter to a Java byte value. The driver converts this
  78. * to an SQL TINYINT value when it sends it to the database.
  79. *
  80. * @param parameterIndex the first parameter is 1, the second is 2, ...
  81. * @param x the parameter value
  82. * @exception SQLException if a database access error occurs
  83. */
  84. void setByte(int parameterIndex, byte x) throws SQLException;
  85. /**
  86. * Sets the designated parameter to a Java short value. The driver converts this
  87. * to an SQL SMALLINT value when it sends it to the database.
  88. *
  89. * @param parameterIndex the first parameter is 1, the second is 2, ...
  90. * @param x the parameter value
  91. * @exception SQLException if a database access error occurs
  92. */
  93. void setShort(int parameterIndex, short x) throws SQLException;
  94. /**
  95. * Sets the designated parameter to a Java int value. The driver converts this
  96. * to an SQL INTEGER value when it sends it to the database.
  97. *
  98. * @param parameterIndex the first parameter is 1, the second is 2, ...
  99. * @param x the parameter value
  100. * @exception SQLException if a database access error occurs
  101. */
  102. void setInt(int parameterIndex, int x) throws SQLException;
  103. /**
  104. * Sets the designated parameter to a Java long value. The driver converts this
  105. * to an SQL BIGINT value when it sends it to the database.
  106. *
  107. * @param parameterIndex the first parameter is 1, the second is 2, ...
  108. * @param x the parameter value
  109. * @exception SQLException if a database access error occurs
  110. */
  111. void setLong(int parameterIndex, long x) throws SQLException;
  112. /**
  113. * Sets the designated parameter to a Java float value. The driver converts this
  114. * to an SQL FLOAT value when it sends it to the database.
  115. *
  116. * @param parameterIndex the first parameter is 1, the second is 2, ...
  117. * @param x the parameter value
  118. * @exception SQLException if a database access error occurs
  119. */
  120. void setFloat(int parameterIndex, float x) throws SQLException;
  121. /**
  122. * Sets the designated parameter to a Java double value. The driver converts this
  123. * to an SQL DOUBLE value when it sends it to the database.
  124. *
  125. * @param parameterIndex the first parameter is 1, the second is 2, ...
  126. * @param x the parameter value
  127. * @exception SQLException if a database access error occurs
  128. */
  129. void setDouble(int parameterIndex, double x) throws SQLException;
  130. /**
  131. * Sets the designated parameter to a java.lang.BigDecimal value.
  132. * The driver converts this to an SQL NUMERIC value when
  133. * it sends it to the database.
  134. *
  135. * @param parameterIndex the first parameter is 1, the second is 2, ...
  136. * @param x the parameter value
  137. * @exception SQLException if a database access error occurs
  138. */
  139. void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException;
  140. /**
  141. * Sets the designated parameter to a Java String value. The driver converts this
  142. * to an SQL VARCHAR or LONGVARCHAR value (depending on the argument's
  143. * size relative to the driver's limits on VARCHARs) when it sends
  144. * it to the database.
  145. *
  146. * @param parameterIndex the first parameter is 1, the second is 2, ...
  147. * @param x the parameter value
  148. * @exception SQLException if a database access error occurs
  149. */
  150. void setString(int parameterIndex, String x) throws SQLException;
  151. /**
  152. * Sets the designated parameter to a Java array of bytes. The driver converts
  153. * this to an SQL VARBINARY or LONGVARBINARY (depending on the
  154. * argument's size relative to the driver's limits on VARBINARYs)
  155. * when it sends it to the database.
  156. *
  157. * @param parameterIndex the first parameter is 1, the second is 2, ...
  158. * @param x the parameter value
  159. * @exception SQLException if a database access error occurs
  160. */
  161. void setBytes(int parameterIndex, byte x[]) throws SQLException;
  162. /**
  163. * Sets the designated parameter to a java.sql.Date value. The driver converts this
  164. * to an SQL DATE value when it sends it to the database.
  165. *
  166. * @param parameterIndex the first parameter is 1, the second is 2, ...
  167. * @param x the parameter value
  168. * @exception SQLException if a database access error occurs
  169. */
  170. void setDate(int parameterIndex, java.sql.Date x)
  171. throws SQLException;
  172. /**
  173. * Sets the designated parameter to a java.sql.Time value. The driver converts this
  174. * to an SQL TIME value when it sends it to the database.
  175. *
  176. * @param parameterIndex the first parameter is 1, the second is 2, ...
  177. * @param x the parameter value
  178. * @exception SQLException if a database access error occurs
  179. */
  180. void setTime(int parameterIndex, java.sql.Time x)
  181. throws SQLException;
  182. /**
  183. * Sets the designated parameter to a java.sql.Timestamp value. The driver
  184. * converts this to an SQL TIMESTAMP value when it sends it to the
  185. * database.
  186. *
  187. * @param parameterIndex the first parameter is 1, the second is 2, ...
  188. * @param x the parameter value
  189. * @exception SQLException if a database access error occurs
  190. */
  191. void setTimestamp(int parameterIndex, java.sql.Timestamp x)
  192. throws SQLException;
  193. /**
  194. * Sets the designated parameter to the given input stream, which will have
  195. * the specified number of bytes.
  196. * When a very large ASCII value is input to a LONGVARCHAR
  197. * parameter, it may be more practical to send it via a
  198. * java.io.InputStream. JDBC will read the data from the stream
  199. * as needed, until it reaches end-of-file. The JDBC driver will
  200. * do any necessary conversion from ASCII to the database char format.
  201. *
  202. * <P><B>Note:</B> This stream object can either be a standard
  203. * Java stream object or your own subclass that implements the
  204. * standard interface.
  205. *
  206. * @param parameterIndex the first parameter is 1, the second is 2, ...
  207. * @param x the Java input stream that contains the ASCII parameter value
  208. * @param length the number of bytes in the stream
  209. * @exception SQLException if a database access error occurs
  210. */
  211. void setAsciiStream(int parameterIndex, java.io.InputStream x, int length)
  212. throws SQLException;
  213. /**
  214. * Sets the designated parameter to the given input stream, which will have
  215. * the specified number of bytes.
  216. * When a very large UNICODE value is input to a LONGVARCHAR
  217. * parameter, it may be more practical to send it via a
  218. * java.io.InputStream. JDBC will read the data from the stream
  219. * as needed, until it reaches end-of-file. The JDBC driver will
  220. * do any necessary conversion from UNICODE to the database char format.
  221. * The byte format of the Unicode stream must be Java UTF-8, as
  222. * defined in the Java Virtual Machine Specification.
  223. *
  224. * <P><B>Note:</B> This stream object can either be a standard
  225. * Java stream object or your own subclass that implements the
  226. * standard interface.
  227. *
  228. * @param parameterIndex the first parameter is 1, the second is 2, ...
  229. * @param x the java input stream which contains the
  230. * UNICODE parameter value
  231. * @param length the number of bytes in the stream
  232. * @exception SQLException if a database access error occurs
  233. * @deprecated
  234. */
  235. void setUnicodeStream(int parameterIndex, java.io.InputStream x,
  236. int length) throws SQLException;
  237. /**
  238. * Sets the designated parameter to the given input stream, which will have
  239. * the specified number of bytes.
  240. * When a very large binary value is input to a LONGVARBINARY
  241. * parameter, it may be more practical to send it via a
  242. * java.io.InputStream. JDBC will read the data from the stream
  243. * as needed, until it reaches end-of-file.
  244. *
  245. * <P><B>Note:</B> This stream object can either be a standard
  246. * Java stream object or your own subclass that implements the
  247. * standard interface.
  248. *
  249. * @param parameterIndex the first parameter is 1, the second is 2, ...
  250. * @param x the java input stream which contains the binary parameter value
  251. * @param length the number of bytes in the stream
  252. * @exception SQLException if a database access error occurs
  253. */
  254. void setBinaryStream(int parameterIndex, java.io.InputStream x,
  255. int length) throws SQLException;
  256. /**
  257. * Clears the current parameter values immediately.
  258. * <P>In general, parameter values remain in force for repeated use of a
  259. * Statement. Setting a parameter value automatically clears its
  260. * previous value. However, in some cases it is useful to immediately
  261. * release the resources used by the current parameter values; this can
  262. * be done by calling clearParameters.
  263. *
  264. * @exception SQLException if a database access error occurs
  265. */
  266. void clearParameters() throws SQLException;
  267. //----------------------------------------------------------------------
  268. // Advanced features:
  269. /**
  270. * <p>Sets the value of a parameter using an object. The second
  271. * argument must be an object type; for integral values, the
  272. * java.lang equivalent objects should be used.
  273. *
  274. * <p>The given Java object will be converted to the targetSqlType
  275. * before being sent to the database.
  276. *
  277. * If the object has a custom mapping (is of a class implementing SQLData),
  278. * the JDBC driver should call its method <code>writeSQL</code> to write it
  279. * to the SQL data stream.
  280. * If, on the other hand, the object is of a class implementing
  281. * Ref, Blob, Clob, Struct,
  282. * or Array, the driver should pass it to the database as a value of the
  283. * corresponding SQL type.
  284. *
  285. * <p>Note that this method may be used to pass datatabase-
  286. * specific abstract data types.
  287. *
  288. * @param parameterIndex the first parameter is 1, the second is 2, ...
  289. * @param x the object containing the input parameter value
  290. * @param targetSqlType the SQL type (as defined in java.sql.Types) to be
  291. * sent to the database. The scale argument may further qualify this type.
  292. * @param scale for java.sql.Types.DECIMAL or java.sql.Types.NUMERIC types,
  293. * this is the number of digits after the decimal point. For all other
  294. * types, this value will be ignored.
  295. * @exception SQLException if a database access error occurs
  296. * @see Types
  297. */
  298. void setObject(int parameterIndex, Object x, int targetSqlType, int scale)
  299. throws SQLException;
  300. /**
  301. * Sets the value of the designated parameter with the given object.
  302. * This method is like setObject above, except that it assumes a scale of zero.
  303. *
  304. * @param parameterIndex the first parameter is 1, the second is 2, ...
  305. * @param x the object containing the input parameter value
  306. * @param targetSqlType the SQL type (as defined in java.sql.Types) to be
  307. * sent to the database
  308. * @exception SQLException if a database access error occurs
  309. */
  310. void setObject(int parameterIndex, Object x, int targetSqlType)
  311. throws SQLException;
  312. /**
  313. * <p>Sets the value of a parameter using an object; use the
  314. * java.lang equivalent objects for integral values.
  315. *
  316. * <p>The JDBC specification specifies a standard mapping from
  317. * Java Object types to SQL types. The given argument java object
  318. * will be converted to the corresponding SQL type before being
  319. * sent to the database.
  320. *
  321. * <p>Note that this method may be used to pass datatabase-
  322. * specific abstract data types, by using a Driver-specific Java
  323. * type.
  324. *
  325. * If the object is of a class implementing SQLData,
  326. * the JDBC driver should call its method <code>writeSQL</code> to write it
  327. * to the SQL data stream.
  328. * If, on the other hand, the object is of a class implementing
  329. * Ref, Blob, Clob, Struct,
  330. * or Array, then the driver should pass it to the database as a value of the
  331. * corresponding SQL type.
  332. *
  333. * This method throws an exception if there is an ambiguity, for example, if the
  334. * object is of a class implementing more than one of those interfaces.
  335. *
  336. * @param parameterIndex the first parameter is 1, the second is 2, ...
  337. * @param x the object containing the input parameter value
  338. * @exception SQLException if a database access error occurs
  339. */
  340. void setObject(int parameterIndex, Object x) throws SQLException;
  341. /**
  342. * Executes any kind of SQL statement.
  343. * Some prepared statements return multiple results; the execute
  344. * method handles these complex statements as well as the simpler
  345. * form of statements handled by executeQuery and executeUpdate.
  346. *
  347. * @exception SQLException if a database access error occurs
  348. * @see Statement#execute
  349. */
  350. boolean execute() throws SQLException;
  351. //--------------------------JDBC 2.0-----------------------------
  352. /**
  353. * JDBC 2.0
  354. *
  355. * Adds a set of parameters to the batch.
  356. *
  357. * @exception SQLException if a database access error occurs
  358. * @see Statement#addBatch
  359. */
  360. void addBatch() throws SQLException;
  361. /**
  362. * JDBC 2.0
  363. *
  364. * Sets the designated parameter to the given <code>Reader</code>
  365. * object, which is the given number of characters long.
  366. * When a very large UNICODE value is input to a LONGVARCHAR
  367. * parameter, it may be more practical to send it via a
  368. * java.io.Reader. JDBC will read the data from the stream
  369. * as needed, until it reaches end-of-file. The JDBC driver will
  370. * do any necessary conversion from UNICODE to the database char format.
  371. *
  372. * <P><B>Note:</B> This stream object can either be a standard
  373. * Java stream object or your own subclass that implements the
  374. * standard interface.
  375. *
  376. * @param parameterIndex the first parameter is 1, the second is 2, ...
  377. * @param x the java reader which contains the UNICODE data
  378. * @param length the number of characters in the stream
  379. * @exception SQLException if a database access error occurs
  380. */
  381. void setCharacterStream(int parameterIndex,
  382. java.io.Reader reader,
  383. int length) throws SQLException;
  384. /**
  385. * JDBC 2.0
  386. *
  387. * Sets a REF(<structured-type>) parameter.
  388. *
  389. * @param i the first parameter is 1, the second is 2, ...
  390. * @param x an object representing data of an SQL REF Type
  391. * @exception SQLException if a database access error occurs
  392. */
  393. void setRef (int i, Ref x) throws SQLException;
  394. /**
  395. * JDBC 2.0
  396. *
  397. * Sets a BLOB parameter.
  398. *
  399. * @param i the first parameter is 1, the second is 2, ...
  400. * @param x an object representing a BLOB
  401. * @exception SQLException if a database access error occurs
  402. */
  403. void setBlob (int i, Blob x) throws SQLException;
  404. /**
  405. * JDBC 2.0
  406. *
  407. * Sets a CLOB parameter.
  408. *
  409. * @param i the first parameter is 1, the second is 2, ...
  410. * @param x an object representing a CLOB
  411. * @exception SQLException if a database access error occurs
  412. */
  413. void setClob (int i, Clob x) throws SQLException;
  414. /**
  415. * JDBC 2.0
  416. *
  417. * Sets an Array parameter.
  418. *
  419. * @param i the first parameter is 1, the second is 2, ...
  420. * @param x an object representing an SQL array
  421. * @exception SQLException if a database access error occurs
  422. */
  423. void setArray (int i, Array x) throws SQLException;
  424. /**
  425. * JDBC 2.0
  426. *
  427. * Gets the number, types and properties of a ResultSet's columns.
  428. *
  429. * @return the description of a ResultSet's columns
  430. * @exception SQLException if a database access error occurs
  431. */
  432. ResultSetMetaData getMetaData() throws SQLException;
  433. /**
  434. * JDBC 2.0
  435. *
  436. * Sets the designated parameter to a java.sql.Date value,
  437. * using the given <code>Calendar</code> object. The driver uses
  438. * the <code>Calendar</code> object to construct an SQL DATE,
  439. * which the driver then sends to the database. With a
  440. * a <code>Calendar</code> object, the driver can calculate the date
  441. * taking into account a custom timezone and locale. If no
  442. * <code>Calendar</code> object is specified, the driver uses the default
  443. * timezone and locale.
  444. *
  445. * @param parameterIndex the first parameter is 1, the second is 2, ...
  446. * @param x the parameter value
  447. * @param cal the <code>Calendar</code> object the driver will use
  448. * to construct the date
  449. * @exception SQLException if a database access error occurs
  450. */
  451. void setDate(int parameterIndex, java.sql.Date x, Calendar cal)
  452. throws SQLException;
  453. /**
  454. * JDBC 2.0
  455. *
  456. * Sets the designated parameter to a java.sql.Time value,
  457. * using the given <code>Calendar</code> object. The driver uses
  458. * the <code>Calendar</code> object to construct an SQL TIME,
  459. * which the driver then sends to the database. With a
  460. * a <code>Calendar</code> object, the driver can calculate the time
  461. * taking into account a custom timezone and locale. If no
  462. * <code>Calendar</code> object is specified, the driver uses the default
  463. * timezone and locale.
  464. *
  465. * @param parameterIndex the first parameter is 1, the second is 2, ...
  466. * @param x the parameter value
  467. * @param cal the <code>Calendar</code> object the driver will use
  468. * to construct the time
  469. * @exception SQLException if a database access error occurs
  470. */
  471. void setTime(int parameterIndex, java.sql.Time x, Calendar cal)
  472. throws SQLException;
  473. /**
  474. * JDBC 2.0
  475. *
  476. * Sets the designated parameter to a java.sql.Timestamp value,
  477. * using the given <code>Calendar</code> object. The driver uses
  478. * the <code>Calendar</code> object to construct an SQL TIMESTAMP,
  479. * which the driver then sends to the database. With a
  480. * a <code>Calendar</code> object, the driver can calculate the timestamp
  481. * taking into account a custom timezone and locale. If no
  482. * <code>Calendar</code> object is specified, the driver uses the default
  483. * timezone and locale.
  484. *
  485. * @param parameterIndex the first parameter is 1, the second is 2, ...
  486. * @param x the parameter value
  487. * @param cal the <code>Calendar</code> object the driver will use
  488. * to construct the timestamp
  489. * @exception SQLException if a database access error occurs
  490. */
  491. void setTimestamp(int parameterIndex, java.sql.Timestamp x, Calendar cal)
  492. throws SQLException;
  493. /**
  494. * JDBC 2.0
  495. *
  496. * Sets the designated parameter to SQL NULL. This version of setNull should
  497. * be used for user-named types and REF type parameters. Examples
  498. * of user-named types include: STRUCT, DISTINCT, JAVA_OBJECT, and
  499. * named array types.
  500. *
  501. * <P><B>Note:</B> To be portable, applications must give the
  502. * SQL type code and the fully-qualified SQL type name when specifying
  503. * a NULL user-defined or REF parameter. In the case of a user-named type
  504. * the name is the type name of the parameter itself. For a REF
  505. * parameter the name is the type name of the referenced type. If
  506. * a JDBC driver does not need the type code or type name information,
  507. * it may ignore it.
  508. *
  509. * Although it is intended for user-named and Ref parameters,
  510. * this method may be used to set a null parameter of any JDBC type.
  511. * If the parameter does not have a user-named or REF type, the given
  512. * typeName is ignored.
  513. *
  514. *
  515. * @param parameterIndex the first parameter is 1, the second is 2, ...
  516. * @param sqlType a value from java.sql.Types
  517. * @param typeName the fully-qualified name of an SQL user-named type,
  518. * ignored if the parameter is not a user-named type or REF
  519. * @exception SQLException if a database access error occurs
  520. */
  521. void setNull (int paramIndex, int sqlType, String typeName)
  522. throws SQLException;
  523. }