Life Cycle of a JDBC Application

Java applications that use a database connection and the JDBC API have a distinct life cycle associated with them. Take a quick look at the different stages in a JDBC application:

Figure 8.1 shows the six stages in the life cycle of a JDBC application:

  1. The first step in a JDBC application is to load the JDBC driver and initialize it. Loading the JDBC driver is done by calling the "Class.forName().newInstance()" method. The JDBC driver is assumed to be in the CLASSPATH.

  2. After loading the JDBC driver, the JDBC application uses the JDBC API to communicate with the database. To do this, the application needs to open a connection to the database using the "connect()" method of the Driver class. A database connection can be opened only by authenticating with the database. The authentication information can be passed in two different ways: as separate parameters to the connect() method, or in the <jdbc URL> parameter of the connect() method. This depends on the JDBC driver implementation. The database connection can be stored in the Java class and can be reused at any point in time to interact with the database as long as the connection is alive.

  3. After obtaining a database connection, the JDBC application can use the Statement, PreparedStatement, or CallableStatement objects to execute SQL statements. The PreparedStatement and CallableStatement are special purpose statement interfaces and inherit from the Statement interface. The JDBC driver implements these interfaces to provide the underlying functions. The Statement, PreparedStatement, and CallableStatement can be created from the database connection. Any number of Statement, PreparedStatement, and CallableStatement objects can be created from a live database connection.

  4. After creating a Statement, PreparedStatement, or CallableStatement, SQL statements such as inserts, updates, and queries can be executed using the relevant methods provided in the Statement, PreparedStatement, and CallableStatement interfaces.

  5. If an SQL statement query is executed, the ResultSet and ResultSetMetaData interfaces can be used by the JDBC application to retrieve the data from the database.

  6. The final stage in the JDBC application is the closing of the database connection. Closing the database connection using the "close()" method of the Connection object frees up expensive resources on the database.

Figure 8.1. Life cycle of a JDBC application.

graphics/08fig01.gif



Sams Teach Yourself BEA WebLogic Server 7. 0 in 21 Days
Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
ISBN: 0672324334
EAN: 2147483647
Year: 2002
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net