The ResultSetMetadata Interface

The ResultSetMetadata Interface

When you retrieve data from the database it is to important to know as much extra information about the retrieved data itself as possible. This "extra" information lets developers know, for example, what the retrieved data's datatype is, how many columns the result set has, even the name of the column where the cursor is located in the ResultSet. All of this is made possible by the ResultSetMetadata interface available in the JDBC API. Metadata is the extra information available in the result set as well as other data that developers use to interpret and manipulate the data.

Because a result set metadata is always associated with a result set, a ResultSetMetaData object is created from the ResultSet object using the getMetaData() method of the ResultSet object. A code snippet follows:

 ResultSet myRSet = myStmt.executeQuery("SELECT * FROM BOOK_TBL");  ResultSetMetadata myRSetMetaData = myRSet.getMetaData(); 

Take a quick look at some of the important methods available in the ResultSetMetadata interface.

  • getColumnCount() Returns the number of columns in the result set. This method is useful when a SELECT * from TABLE_NAME query is executed and the number of columns is not known prior to the execution of the query. A code snippet follows:

     int colCount = myRSetMetaData.getColumnCount();  
  • getColumnName() Accepts a column number as a parameter and returns the name of the column represented by that column number in the result set. A code snippet follows:

     String colName = myRSetMetaData.getColumnName(colNum);  

    where colNum is 1, 2, 3, and so on, until the maximum number of columns in the result set, which can be determined by calling the getColumnCount() method.

  • getColumnType() Determines the database type of a column. The getColumnType() method takes a column number as a parameter. The database type returned by this method is an integer value that maps to one of the constants defined in the java.sql.Types interface. A code snippet follows:

     int colType = myRSetMetaData.getColumnType(colNum);  // determine the column's database type by comparing with the //java.sql.Types constants if(colType == java.sql.Types.XXX) {     System.out.println("The data type of the column number " +             colNum + " is " + XXX); } 

    where colNum is 1, 2, 3, and so on, until the maximum number of columns in the result set, which can be determined by calling the getColumnCount() method.

  • getTableName() Determines the name of the table to which the column belongs. This method is useful when a result set is generated after executing an SQL query with table joins. The method takes a column number as a parameter. A code snippet follows:

     String tblName = myRSetMetaData.getTableName(colNum);  

    where colNum is 1, 2, 3, and so on, until the maximum number of columns in the result set, which can be determined by calling the getColumnCount() method.

  • isNullable() Determines whether the particular column can contain NULL values or not. The isNullable() method takes a column number as a parameter. The method returns an integer value indicating whether the column is nullable or not, or if the nullable status is not known. A code snippet follows:

     int nullStatus = myRSetMetaData.getColumnName(colNum);  

    where colNum is 1, 2, 3, and so on, till the maximum number of columns in the result set, which can be determined by calling the getColumnCount() method.



Sams Teach Yourself BEA WebLogic Server 7. 0 in 21 Days
Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
ISBN: 0672324334
EAN: 2147483647
Year: 2002
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net