Simple application program life cycle - IBM DB2

Below it shows different life cycles for a query in an application program. An application program starts by getting the Connection object for the database using DriverManager. A Connection object can be used to create three different kinds of SQL statements: Statement, PreparedStatement, and CallableStatements. Connection objects provide methods to create any of these objects. A Statement object is used to execute the SQL statement with a parameter, and the PreparedStatement can be used to run the SQL statement with a parameter. CallableStatement is used to call the user defined functions (UDFs).Below it depicts different paths for an SQL statement in a JDBC program.

Different path for an SQL statement in a JDBC program

Different path for an SQL statement in a JDBC program

Each of the three classes, Statement, PreparedStatement, and CallableStatement, provides three different methods to execute a query:

  • executeQuery: This method is used to select the rows and return a resultSet object.
  • executeUpdate: This method is used to update the database and return the update count.
  • execute method: This method returns a Boolean value of true if the output of the query is a ResultSet object ( SELECT ) and false if its an update statement ( INSERT, UPDATE, or DELETE).

The getUpdateCount method can be used to get the number of rows updated in the database by the SQL statement. The getXXX method of the ResultSet object can be used to fetch the individual rows. For CallableStatement, each of these methods has a slightly different meaning. The executeQuery method is used when the stored procedure returns a ResultSet, where execute is used when the stored procedure returns multiple ResultSets.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

IBM DB2 Topics