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