As previously mentioned, the ResultSet class makes a lot of useful information available via its getResultSetMetaData method, which returns a
ResultSetMetaData object. Figure 12.5 shows the window of the MetaData applet. It lets you enter a query and then describes the result set the query returns. Let’s analyze how the applet works.
Listing 12.3 shows the source code of the MetaData applet, which resembles the source code of the PassengerList applet. As before, the class includes fields that identify the
database and the logon user ID and password. Another field holds a reference to the database Connection object. As before, the interesting part of the program is the inner class that handles ActionEvents.
Figure 12.5: Result set meta data describes the contents of a result set.
LISTING 12.3 MetaData.java —LEARNING ABOUT A ResultSet
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class MetaData extends Applet {
static final String DB = "jdbc:odbc:airline";
static final String USER = "";
static final String PASSWORD = "";
TextField theQuery = new TextField();
TextArea theResult = new TextArea(20, 64);
Connection theConnection;
public void init() {
setLayout(new BorderLayout());
Panel p1 = new Panel();
add(p1, BorderLayout.NORTH);
add(theResult, BorderLayout.CENTER);
p1.setLayout(new GridLayout(0, 1));
p1.add(new Label("Query:"));
p1.add(theQuery);
p1.add(new Label("Result:"));
theResult.setEditable(false);
Font f = new Font("Monospaced", Font.PLAIN, 12);
theQuery.setFont(f);
theResult.setFont(f);
theQuery.addActionListener(new QueryHandler());
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
theConnection =
DriverManager.getConnection(DB, USER, PASSWORD);
}
catch (ClassNotFoundException ex1) { fatalError(ex1); } catch (SQLException ex2) { fatalError(ex2); }
}
public void fatalError(Exception ex) {
ex.printStackTrace();
}
// Inner class omitted }
Listing 12.4 shows the MetaData class, the inner class of the MetaData applet. The actionPerformed method closely resembles that of the PassengerList applet;
however, this QueryHandler class defines the displayResult method differently.
LISTING 12.4 MetaData.java —EXECUTING A QUERY
class QueryHandler implements ActionListener {
public void actionPerformed(ActionEvent evt) {
try {
String query = theQuery.getText();
Statement stmt = theConnection.createStatement();
ResultSet rs = stmt.executeQuery(query);
displayResult(rs);
stmt.close();
}
catch (SQLException ex) { fatalError(ex); } }
public void displayResult(ResultSet rs) {
try {
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
println("Columns=" ++ cols);
println("");
for (int col = 1; col <= cols; col++) {
println("Name=" ++
rsmd.getColumnName(col));
println("Label=" ++
rsmd.getColumnLabel(col));
println("Max Width=" ++
rsmd.getColumnDisplaySize(col));
println("DB Type=" ++
rsmd.getColumnTypeName(col));
println("JDBC Type=" ++
getJDBCTypeName(rsmd.getColumnType(col)));
println("Signed=" ++
rsmd.isSigned(col));
println("Precision=" ++
rsmd.getPrecision(col));
println("Scale=" ++
rsmd.getScale(col));
println("Currency Value=" ++
rsmd.isCurrency(col));
println("Auto Increment=" ++
rsmd.isAutoIncrement(col));
println("Case Sensitive=" ++
rsmd.isCaseSensitive(col));
println("Read Only=" ++
rsmd.isReadOnly(col));
int nullable =
rsmd.isNullable(col);
switch (nullable) {
case rsmd.columnNoNulls:
println("Null OK=No");
break;
case rsmd.columnNullable:
println("Null OK=Yes");
break;
default:
println("Null OK=Unknown");
}
println("Searchable=" ++
rsmd.isSearchable(col));
println("Writable=" ++
rsmd.isWritable(col));
println("Definitely Writable=" ++
rsmd.isDefinitelyWritable(col));
println("");
}
rs.close();
}
catch (SQLException ex) { fatalError(ex); } }
// Utility methods omitted }
Rather than display the contents of the result set rows, this method describes the result set table by using various ResultSetMetaData messages. Table 12.2 summarizes these methods and their functions. The QueryHandler class includes two utility methods (not shown here) in order to conserve space; they’re included on the CD-ROM.
The getJDBCTypeName method works with the getColumnType method, which returns an int corresponding to the JDBC data type of a column. The getColumnType
method simply returns a String that describes the data type corresponding to its int argument.
The println is not the familiar System.out.println , although it does have a similar purpose. The println utility method appends its String argument to the contents of the TextArea called theResult, and then appends a line termination sequence.
TABLE 12.2 KEY ResultSetMetaData METHODS
Method Meaning
int
getColumnDisplaySize(int col)
Returns the maximum display size of the specified column
String
getColumnLabel(int col)
Returns the suggested column heading of the specified column
String getColumnName(int col) Returns the name of the specified column
int getColumnType(int
col) Returns the JDBC type of the specified column
String
getColumnTypeName(int col)
Returns the vendor-specific name of the type of the specified column
int getPrecision(int col)
Returns the number of digits (numeric column) or characters (nonnumeric column) of the specified column
int getScale(int col) Returns the implied decimal point of the specified column
boolean
isAutoIncrement(int col)
Returns true if the value of the specified column is generated by the database, which assigns consecutive values
boolean
isCaseSensitive(int col)
Returns true if the specified column is case sensitive
boolean isCurrency(int
col) Returns true if the specified column holds a
currency value
boolean
isDefinitelyWritable(int col)
Returns true if the specified column is writable
int isNullable(int
col) Returns a value that indicates whether the
specified column can contain SQL null as its value
boolean isReadOnly(int
col) Returns true if the specified column is read-only boolean
isSearchable(int col) Returns true if the specified column can be used in a Where clause
isSigned(int col) Returns true if the specified column can accommodate a signed numeric value
isWritable(int col) Returns true if a new value can be written to the specified column