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