javax sql and java sql java sql Interface

  • Slides: 22
Download presentation
javax. sql and java. sql

javax. sql and java. sql

java. sql Interface Connection public interface Connection extends Wrapper A connection (session) with a

java. sql Interface Connection public interface Connection extends Wrapper A connection (session) with a specific database. SQL statements are executed and results are returned within the context of a connection. A Connection object's database is able to provide information describing its tables, its supported SQL grammar, its stored procedures, the capabilities of this connection, and so on. This information is obtained with the get. Meta. Data method.

Interface Connection • Note: When configuring a Connection, JDBC applications should use the appropritate

Interface Connection • Note: When configuring a Connection, JDBC applications should use the appropritate Connection method such as set. Auto. Commit or set. Transaction. Isolation. Applications should not invoke SQL commands directly to change the connection's configuration when there is a JDBC method available. By default a Connection object is in auto-commit mode, which means that it automatically commits changes after executing each statement. If auto-commit mode has been disabled, the method commit must be called explicitly in order to commit changes; otherwise, database changes will not be saved.

Connection property inspector get. Catalog String get. Catalog() throws SQLException Retrieves this Connection object's

Connection property inspector get. Catalog String get. Catalog() throws SQLException Retrieves this Connection object's current catalog name. Returns: the current catalog name or null if there is none Throws: SQLException - if a database access error occurs or this method is called on a closed connection

Interface Connection set. Holdability void set. Holdability(int holdability) throws SQLException Changes the default holdability

Interface Connection set. Holdability void set. Holdability(int holdability) throws SQLException Changes the default holdability of Result. Set objects created using this Connection object to the given holdability. The default holdability of Result. Set objects can be be determined by invoking Database. Meta. Data. get. Result. Set. Holdability(). Parameters: holdability - a Result. Set holdability constant; one of Result. Set. HOLD_CURSORS_OVER_COMMIT or Result. Set. CLOSE_CURSORS_AT_COMMIT Throws: SQLException - if a database access occurs, this method is called on a closed connection, or the given parameter is not a Result. Set constant indicating holdability SQLFeature. Not. Supported. Exception - if the given holdability is not supported

Interface Connection set. Read. Only void set. Read. Only(boolean read. Only) throws SQLException Puts

Interface Connection set. Read. Only void set. Read. Only(boolean read. Only) throws SQLException Puts this connection in read-only mode as a hint to the driver to enable database optimizations. Note: This method cannot be called during a transaction. Parameters: read. Only - true enables read-only mode; false disables it Throws: SQLException - if a database access error occurs, this method is called on a closed connection or this method is called during a transaction __________________________________ is. Read. Only boolean is. Read. Only() throws SQLException Retrieves whether this Connection object is in read-only mode. Returns: true if this Connection object is read-only; false otherwise Throws: SQLException - SQLException if a database access error occurs or this method is called on a closed connection

Interface Connection set. Transaction. Isolation void set. Transaction. Isolation(int level) throws SQLException Attempts to

Interface Connection set. Transaction. Isolation void set. Transaction. Isolation(int level) throws SQLException Attempts to change the transaction isolation level for this Connection object to the one given. The constants defined in the interface Connection are the possible transaction isolation levels. Note: If this method is called during a transaction, the result is implementation-defined. Parameters: level - one of the following Connection constants: Connection. TRANSACTION_READ_UNCOMMITTED, Connection. TRANSACTION_READ_COMMITTED, Connection. TRANSACTION_REPEATABLE_READ, or Connection. TRANSACTION_SERIALIZABLE. (Note that Connection. TRANSACTION_NONE cannot be used because it specifies that transactions are not supported. ) Throws: SQLException - if a database access error occurs, this method is called on a closed connection or the given parameter is not one of the Connection constants

Interface Connection set. Type. Map void set. Type. Map(Map<String, Class<? >> map) throws SQLException

Interface Connection set. Type. Map void set. Type. Map(Map<String, Class<? >> map) throws SQLException Installs the given Type. Map object as the type map for this Connection object. The type map will be used for the custom mapping of SQL structured types and distinct types. Parameters: map - the java. util. Map object to install as the replacement for this Connection object's default type map Throws: SQLException - if a database access error occurs, this method is called on a closed connection or the given parameter is not a java. util. Map object SQLFeature. Not. Supported. Exception - if the JDBC driver does not support this method

java. sql Interface Result. Set public interface Result. Set extends Wrapper • A table

java. sql Interface Result. Set public interface Result. Set extends Wrapper • A table of data representing a database result set, which is usually generated by executing a statement that queries the database. • A Result. Set object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the Result. Set object, it can be used in a while loop to iterate through the result set. • A default Result. Set object is not updatable and has a cursor that moves forward only. Thus, you can iterate through it only once and only from the first row to the last row. It is possible to produce Result. Set objects that are scrollable and/or updatable. The following code fragment, in which con is a valid Connection object, illustrates how to make a result set that is scrollable and insensitive to updates by others, and that is updatable. See Result. Set fields for other options.

Result Set types The JDBC 1. 0 API provided one result set type—forward-only. The

Result Set types The JDBC 1. 0 API provided one result set type—forward-only. The JDBC 2. 1 core API provides three result set types: forward-only, scroll-insensitive, and scrollsensitive. As their names suggest, the new result set types support scrolling, but they differ in their ability to make changes visible while they are open.

Result Set types A scroll-insensitive result set is generally not sensitive to changes that

Result Set types A scroll-insensitive result set is generally not sensitive to changes that are made while it is open. A scroll-insensitive result set provides a static view of the underlying data it contains. The membership, order, and column values of rows in a scroll-insensitive result set are typically fixed when the result set is created.

Result Set types On the other hand, a scroll-sensitive result set is sensitive to

Result Set types On the other hand, a scroll-sensitive result set is sensitive to changes that are made while it is open, and provides a ‘dynamic’ view of the underlying data. For example, when using a scroll-sensitive result set, changes in the underlying column values of rows are visible. The membership and ordering of rows in the result set may be fixed— this is implementation defined.

Examples • The example below illustrates creation of a result set that is forward-only

Examples • The example below illustrates creation of a result set that is forward-only and uses readonly concurrency. Connection con = Driver. Manager. get. Connection( "jdbc: my_subprotocol: my_subname"); Statement stmt = con. create. Statement(); Result. Set rs = stmt. execute. Query( "SELECT emp_no, salary FROM employees");

Examples • The next example creates a scrollable result set that is updatable and

Examples • The next example creates a scrollable result set that is updatable and sensitive to updates. • Rows of data are requested to be fetched twenty-five at-a-time from the database. Connection con = Driver. Manager. get. Connection( "jdbc: my_subprotocol: my_subname"); Statement stmt = con. create. Statement( Result. Set. TYPE_SCROLL_SENSITIVE, Result. Set. CONCUR_UPDATABLE); stmt. set. Fetch. Size(25); Result. Set rs = stmt. execute. Query( "SELECT emp_no, salary FROM employees");

Examples • The example below creates a result set with the same attributes as

Examples • The example below creates a result set with the same attributes as the previous example, however, a prepared statement is used to produce the result set. • JDBC 2. 1 Core API • Sun Microsystems Inc. 14 October 5, 1999 Prepared. Statement pstmt = con. prepare. Statement( "SELECT emp_no, salary FROM employees where emp_no = ? ", Result. Set. TYPE_SCROLL_SENSITIVE, Result. Set. CONCUR_UPDATABLE); pstmt. set. Fetch. Size(25); pstmt. set. String(1, "100010"); Result. Set rs = pstmt. execute. Query();

Due to differences in database implementations, the JDBC API does not specify an exact

Due to differences in database implementations, the JDBC API does not specify an exact set of SQL queries which must yield an updatable result set for JDBC drivers that support updatability. Developers can, however, generally expect queries which meet the following criteria to produce an updatable result set: 1. The query references only a single table in the database. 2. The query does not contain any join operations. 3. The query selects the primary key of the table it references. In addition, an SQL query should also satisfy the conditions listed below if inserts are to be performed. 4. The query selects all of the non-nullable columns in the underlying table. 5. The query selects all columns that don’t have a default value. http: //minitorn. tlu. ee/~jaagup/kool/java/abiinfo/docs/6/docs/api/java/sql/Result. Set. html

 • • • • javax. sql. Connection. Event. Listener javax. sql. Connection. Pool.

• • • • javax. sql. Connection. Event. Listener javax. sql. Connection. Pool. Data. Surce javax. sql. Data. Source javax. sql. Pooled. Connection javax. sql. Row. Set. Event javax. sql. Row. Set. Internal javax. sql. Row. Set. Listener javax. sql. Row. Set. Meta. Data javax. sql. Row. Set. Reader javax. sql. Row. Set. Writer javax. sql. XAConnection javax. sql. XAData. Source

javax. sql Interface Pooled. Connection • public interface Pooled. Connection • An object that

javax. sql Interface Pooled. Connection • public interface Pooled. Connection • An object that provides hooks for connection pool management. A Pooled. Connection object represents a physical connection to a data source. The connection can be recycled rather than being closed when an application is finished with it, thus reducing the number of connections that need to be made. • An application programmer does not use the Pooled. Connection interface directly; rather, it is used by a middle tier infrastructure that manages the pooling of connections. • When an application calls the method Data. Source. get. Connection, it gets back a Connection object. If connection pooling is being done, that Connection object is actually a handle to a Pooled. Connection object, which is a physical connection. • The JDBC driver implementing Connection. Pool. Data. Source creates a new Pooled. Connection object and returns a handle to it.

Interface Pooled. Connection • When an application closes a connection, it calls the Connection

Interface Pooled. Connection • When an application closes a connection, it calls the Connection method close. When connection pooling is being done, the connection pool manager is notified because it has registered itself as a Connection. Event. Listener object using the Connection. Pool method add. Connection. Event. Listener. The connection pool manager deactivates the handle to the Pooled. Connection object and returns the Pooled. Connection object to the pool of connections so that it can be used again. Thus, when an application closes its connection, the underlying physical connection is recycled rather than being closed. • The physical connection is not closed until the connection pool manager calls the Pooled. Connection method close. This method is generally called to have an orderly shutdown of the server or if a fatal error has made the connection unusable.

Interface Pooled. Connection Method Summary void add. Connection. Event. Listener(Connection. Event. Listener listener) Registers

Interface Pooled. Connection Method Summary void add. Connection. Event. Listener(Connection. Event. Listener listener) Registers the given event listener so that it will be notified when an event occurs on this Pooled. Connection object. ________________________________ void add. Statement. Event. Listener(Statement. Event. Listener listener) Registers a Statement. Event. Listener with this Pooled. Connection object. ________________________________ void close() Closes the physical connection that this Pooled. Connection object represents. ________________________________ Connection get. Connection() Creates and returns a Connection object that is a handle for the physical connection that this Pooled. Connection object represents.

Interface Pooled. Connection Method Summary void remove. Connection. Event. Listener(Connection. Event. Listener listener) Removes

Interface Pooled. Connection Method Summary void remove. Connection. Event. Listener(Connection. Event. Listener listener) Removes the given event listener from the list of components that will be notified when an event occurs on this Pooled. Connection object. ________________________________ void remove. Statement. Event. Listener(Statement. Event. Listener listener) Removes the specified Statement. Event. Listener from the list of components that will be notified when the driver detects that a Prepared. Statement has been closed or is invalid.