Oracle Exams Establish a Database Connection,Exams of Oracle,JDBC Escape Syntax,Oracle Certification Exam,The ResultSet Interface Using JDBC MetaData Interfaces – JDBC Applications and Design Considerations

Using JDBC MetaData Interfaces – JDBC Applications and Design Considerations



4.2.5   Using JDBC MetaData Interfaces

In addition to general and popular data information provided by three statement interfaces and execution methods, JDBC also provides useful and critical information and descriptions ofthe data-base, run result sets and parameters related to JDBC drivers and database applications. All of these properties, structures and descriptions can be categorized into three interfaces, so-called metadata interfaces, or

  1. ResultSetMetaData interface
  2. DatabaseMetaData interface
  3. ParameterMetaData interface

In the following sections, we will concentrate on these three interfaces to illustrate how to use them to retrieve detailed descriptions and structures as well as properties related to the data action components, such as ResultSet, database and parameters to facilitate database applications.

Let’s start with the ResultSetMetaData interface.

4.2.5.1   Using the ResultSetMetaData Interface

In Section 4.2.4, we discussed how to retrieve run results stored in a ResultSet object and important methods of this interface. By using different fetching methods, either fetching by rows or columns, we can easily retrieve a whole set of returned results stored in a ResultSet object. However, in some applications, we may need more detailed information about and properties of the returned result set, such as the total number of columns returned and each column’s name and data type, as well as other structure information related to the returned result set. By using this structure information and properties, we can get a clear and full picture of the returned ResultSet, which will enable us to retrieve our desired data more directly and conveniently. With the help of the meta-data provided by ResultSetMetaData, you can develop entire database applications without even knowing what RDBMS, Table or type of data is being accessed.

The ResultSetMetaData interface provides a collection of information about the structure and properties related to the returned ResultSet object, and this gives us the ability to perform the functions we described previously. The ResultSetMetaData interface contains more than 20 methods, and Table 4.10 shows the 16 most popular methods.

It can be seen from Table 4.10 that the top ten methods in a ResultSetMetaData object are mainly used to retrieve the structure and properties for the specified column with the column index as an argument. The rest of methods that return a Boolean value are used to determine some important properties that describe special functions provided by the database engine for the selected column. One of the advantages of using this metadata is that you can build dynamic applications that are independent of the data source. One possible way to achieve this is to remove the need for all direct column name references.

Because of the space limitation, we can only provide a brief discussion of some important meth-ods that are widely implemented in most database applications.

After a data query is executed and a ResultSet object is returned, before we can retrieve our desired data from the ResultSet, we may need to get some structure information and properties related to the columns we chose. One of the most important properties is the total number of columns returned in the ResultSet object. By using the getColumnCount() method, we can easily get not only the total number of columns but also the content of each column. Figure 4.17 shows example code to illustrate how to use this method to scan the entire ResultSet to retrieve each column from it. The first code line is used to create a ResultSet object by executing the executeQuery() method. Then a ResultSetMetaData object, rsmd, is created by calling the getMetaData() method defined by the ResultSet interface. To pick up each returned column, a while loop is used combined with the next() method. By using this piece of code, you do not even need to know

FIGURE 4.17   A code example using the getColumnCount() method.

how many columns were returned in that ResultSet or the name for each column; in other words, you do not have to have prior knowledge about the Table and database: you can retrieve all the col-umns with their exact names! Yes, that is easy and fancy.

In some applications, you may need to know some other useful information about the columns, such as the data type of each column, the width of each column and the precision and scale of the selected column if a floating point or double data is stored in that column. To get those properties, you can call the appropriate methods, such as getColumnType(), getColumnDisplaySize(), getPrecision() and getScale().

Also, to get some important information and properties about the returned ResultSet, sometimes we may need to get similar information for the connected database. In that case, you may need to use the DatabaseMetaData interface.

Leave a Reply

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

Related Post