Ads 468x60px

Monday, July 25, 2011

Result Sets and the Meta-data Interfaces in JAVA


Result Sets and the Meta-data Interfaces

In simple applications such as the counter applet, there is no need to perform any tricks with the results from a query-the data is simply retrieved sequentially and processed. More commonly, however, an application will need to process the data in a more complex fashion. For example, a set of classes might want to deal with data on a more abstract level than the Database class from the counter example. Instead, such classes might not know exactly what data is being retrieved. They can query the meta-data interfaces to process intelligently such data that they would otherwise not know. Listing 15.2 shows a generic database view class that is populated with database objects based on a result set.


Listing 15.2. A generic database view class.
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Hashtable;
import java.util.Vector;


public class View {


    private Vector objects;
    public void populate(ResultSet result_set, String cl) {


        ResultSetMetaData meta_data;

        int i, maxi;



        try {

            objects = new Vector();

            meta_data = result_set.getMetaData();

            maxi = meta_data.getColumnCount();

            while( result_set.next() ) {


                Hashtable row = new Hashtable();
                DataObject obj;
                for(i=1; i<=maxi; i++) {

                    String key;

                    Object value;

                    int t;


                           key = meta_data.getColumnLabel(i);

                    t = meta_data.getColumnType(i);

                    value = result_set.getObject(i, t);
                                        row.put(key, value);
                        }

                          obj = (DataObject)Class.forName(cl);
                                     obj.restore(row);

                         objects.addElement(obj);

            }

         }
  catch ( java.sql.SQLException e ) {

                e.printStackTrace();

                objects = new Vector();

                return;

        }

    }

}

In the View class, reference is made to a DataObject class that implements a restore(java.util.Hashtable) method not listed.
Because many applications will use this generic class, the class knows nothing about the queries it is executing. Instead, it takes any random result set and assumes that each row corresponds to an instance of the class named by the second parameter to populate().
To get the information it needs for performing the data retrievals, the populate() method first obtains the meta-data object for this result set. This method is specifically interested in knowing how many columns, as well as the names of the columns, are in the result set.
To store the columns in a Hashtable object that the DataObject object can use for restoring itself, all data must be in the form of objects. Thus, for each column in the result set, the DataObject finds its data type from the meta-data and retrieves the column as an object. The final step is to store it in the Hashtable.

0 comments:

Post a Comment