Database and Result Set Metadata

“Meta-anything” is a higher or second-order version of the anything. Metadata is data about data. The classic example of metadata is file and directory information on your disk drive. You don't directly put it there, but you need it to keep track of your real data, and it is maintained by the system on your behalf. Databases have a large amount of metadata describing their particular capabilities and configuration.

The database metadata is going to be different for each database, and JDBC lets you get hold of it through the java.sql.DatabaseMetaData interface. You get an instance of the Metadata class by invoking a method of Connection. There you will find 100 or so fields and methods that you can use to find out specific details on the database. For example, it can tell you if the database supports transactions, and if so, to what level.

You use the database metadata when you know your code is going to run against several different databases. By looking at the metadata, your code can discover the individual features of a database, and perhaps take advantage of performance-related options. Often, but not always, there is a slower more standard way to achieve an effect, and you may prefer to write your database application code that uses that, instead of querying the database about its advanced features. Using database metadata is an advanced technique, beyond the scope of this book. The javadoc documentation is extensive if you want to pursue this topic further.

Result sets also have metadata. An object of type java.sql.ResultSetMetaData can get information about the columns in a ResultSet object. Here is an example. The following code fragment creates a ResultSet and gets the corresponding ResultSetMetaData object from it. The code then uses that object to find out two pieces of information about the result set. It calls two methods, one to find out how many columns the result has, and one to learn whether the first column in the result set can be used in a WHERE clause (i.e., it is a “searchable” column).

ResultSet result = statement.executeQuery(
   "SELECT c1, c2 FROM myTable; "

ResultSetMetaData rsmd = result.getMetaData();
int numberCols = rsmd.getColumnCount();
boolean b = rsmd.isSearchable(1);

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.179.85