The ParameterMetaData Interface – Querying Data from Databases

6.3.7.2.3   The ParameterMetaData Interface

Detailed information about parameters passed into or from the database can be obtained by calling the getParameterMetaData() method that belongs to the PreparedStatement class. Although this interface is not as popular as ResultSetMetaData and DatabaseMetaData, it is useful in some special applications.

In this section, we will use the DatabaseMetaData and the ResultSetMetaData interfaces to illus-trate how to improve data queries for our Faculty Table.

Now let’s develop code for the Select button Click event handler to perform a data query.

6.3.7.3  Use Java executeQuery() with the DatabaseMetaData Interface to Query the Faculty Table

Open the Select button Click event handler if it has not been opened, and enter the code shown in Figure 6.23 into this event handler as the first part of code.

Let’s take a closer look at this added piece of code to see how it works.

A. First, an Oracle image BLOB object, fimgBlob, is generated, and it is used to hold the retrieved faculty image to be displayed later on the canvas.
B. Then a TextField array is created, since we need to combine all seven TextField objects, FacultyIDField, FacultyNameField, TitleField, OfficeField, PhoneField, CollegeField and EmailField, in this array and assign the queried results to the TextField objects one by one later to improve the assignment efficiency. A point to be noted is that the definition for this TextField class is different in the basic java.awt package and the javax.swing package, and the TextField used here belongs to the latter.
C. The query string is declared here to query all columns from the Faculty Table based on the selected faculty name that works as a position parameter.
D. An if selection structure is used to identify which query method will be used for this query. If the Java executeQuery Method is selected by the user, the related code will be built in this section. Otherwise, the program will be branched to another query
method.
E. A try-catch block is utilized to perform adatabase-related information query using the DatabaseMetaData interface. A DatabaseMetaData object, dbmd, is created by calling the getMetaData() method that belongs to the Connection class, and detailed information about the connected database is returned and assigned to the dbmd object.
F. Two system methods, getDriverName() and getDriverVersion(), are executed to pick up the retrieved driver name and version and assign them to the associated String vari-ables, drName and drVersion, respectively. Both variables’ values will be displayed

FIGURE 6.23   The first part of the code for the Select button event handler.

in the Output window by calling the system method System.out.println() as a debug message as the project runs.
G. A prepared query statement is then created to query the detailed information for the
selected faculty member.

H. The setString() method is used to set up the dynamic positional parameter in the preparedquery statement. The actual value of this parameter, which is the faculty member
selected by the user from the ComboName combo box, can be obtained by calling the getSelectedItem() method.
I. The executeQuery() method is executed to perform the prepared statement to get the queried result, which is assigned to the ResultSet object rs.

J. The getMetaData() method is executed to query detailed information about the struc-ture of the Faculty Table and properties of the columns in that Table. The returned result is assigned to the ResultSetMetaData object rsmd.

K. As we discussed in Section 6.3.3.4 for fetching by row, a while loop is used with the next() method as the argument to move the data Table cursor from the initial position to the first row position in the ResultSet object. This allows us to pick up each record one by one.

L. A for loop is used to pick up the first seven columns and assign them to the related TextFields to display the queried faculty information, which really happensin step M, until it gets to the last column by using the system method getColumnCount(), which is the faculty image column. Then we need to use the getBlob() method to retrieve this image as a binary large object and assign it to our local object fimgBlob. A break occurs to finish this for loop when the desired faculty image is obtained.

N. Any possible exception or error will be caught by the catch block and displayed in our msgDlg dialog.

Now let’s handle the query to the Faculty Table by using another method, the Java execute() method.

Leave a Reply

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


© 2024 vogafloat, LLC ,About, Careers, Contact us,Cookies, Terms, Privacy