Oracle Exams Establish a Database Connection,Exams of Oracle,Executing Statements,Oracle Certification Exam Using the Statement Object – JDBC Applications and Design Considerations

Using the Statement Object – JDBC Applications and Design Considerations



4.2.3.2   Using the Statement Object

As we discussed in the last section, three separate statement objects can be created based on three different data actions: Statement, PreparedStatement and CallableStatement. Let’s discuss how to create a Statement object first.

4.2.3.2.1   Creating the Statement Object

The Statement object is the most common type of object and is easy to use in a static data query. The shortcoming of using this object is that all Oracle statements must be pre-defined with defi-nite parameters when a Statement object is created. In other words, by using a Statement object to execute an Oracle statement, no parameter can be passed into or from the database.

The Statement object is created by using the createStatement() method defined in the Connection interface (refer to Table 4.3). Figure 4.5 shows an example of creation of a Statement object. The codeline that is used to create a Statement object is in bold. All other lines are prerequisite code used to load and register a driver, establish a connection using the URL and build an Oracle query string.

4.2.3.2.2   Executing the Statement Object

To execute the created Statement object to perform a data action, you need to call one of the exe-cution methods defined in the Statement interface shown in Table 4.4. Figure 4.6 shows example code for the execution of an Oracle query with the Statement object.

The codeline that is used to execute the Statement object is in bold. All other lines are prereq-uisite code used to load and register a driver, establish a connection using the URL, build an Oracle query string and create a Statement object. It can be seen from this piece of code that no param-eter can be passed to or from the database when this query is executed. Therefore, the Statement object can only be used to perform static queries.

To overcome this shortcoming, we need to use PreparedStatement objects to perform dynamic queries with varied input parameters.

4.2.3.3   Using the PreparedStatement Object

To perform dynamic Oracle statements, we need to use a PreparedStatement object. Generally, using a PreparedStatement object to perform a dynamic Oracle statement includes the following steps:

1) Create a PreparedStatement object.

2) Set data types and values to the associated input parameters in the query string.

3) Call an appropriate execution method to perform this dynamic query.

Let’s first concentrate on the creation of a PreparedStatement object.

FIGURE 4.5   Example code for the creation of a Statement object.

FIGURE 4.6   Example code for the execution of a Statement object.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post