Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Using JDBC to Create Database Objects

By James W. Cooper
2003-05-24


Building Higher Level JDBC Objects

It is plain from the foregoing example that it would be very helpful if we could encapsulate some of the behavior we have been using in a few higher-level objects. Not only could we enclose the try blocks, but we could make access to the ResultSet methods a little simpler.

In this section, we'll build a new resultSet object which encapsulates the JDBC ResultSet object and returns the data in a row as an array of Strings. We discovered that you always needed to obtain the number of columns and the names of the columns from the ResultSetMetaData object, and thus it seems quite reasonable to create a new object that also encapsulates the metadata.



class resultSet
{
//this class is a higher level abstraction
//of the JDBC ResultSet object
ResultSet rs;
ResultSetMetaData rsmd;
int numCols;
public resultSet(ResultSet rset)
{
rs = rset;
try
{
//get the meta data and column count at once
rsmd = rs.getMetaData();
numCols = rsmd.getColumnCount();
}
catch (Exception e)
{System.out.println("resultset error"
+e.getMessage());}
}
//--
public String[] getMetaData()
{
//returns an array of all the column names
//or other meta data
String md[] = new String[numCols];
try
{
for (int i=1; i<= numCols; i++)
md[i-1] = rsmd.getColumnName(i);
}
catch (Exception e)
{System.out.println("meta data error"+
e.getMessage());}
return md;
}
//--
public boolean hasMoreElements()
{
try{
return rs.next();
}
catch(Exception e){return false;}
}
//--
public String[] nextElement()
{
//copies contents of row into string array
String[] row = new String[numCols];
try
{
for (int i = 1; i <= numCols; i++)
row[i-1] = rs.getString(i);
}
catch (Exception e)
{System.out.println("next element error"+
e.getMessage());}
return row;
}
//--
public String getColumnValue(String columnName)
{
String res = "";
try
{
res = rs.getString(columnName);
}
catch (Exception e)
{System.out.println("Column value error:"+
columnName+e.getMessage());}
return res;
}
//--
public String getColumnValue(int i)
{
String res = "";
try
{
res = rs.getString(i);
}
catch (Exception e)
{System.out.println("Column value error:"+
columnName+e.getMessage());}
return res;
}
//--
public void finalize()
{
try{rs.close();}
catch (Exception e)
{System.out.println(e.getMessage());}
}
}


ResultSet results = .. //get a ResultSet as usual
//create this more useful object from it
resultSet rs = new resultSet(results);


Tutorial Pages:
» What is a Database?
» Getting Data out of Databases
» Kinds of Databases
» ODBC
» What Is JDBC?
» Installing and Using JDBC
» Types of JDBC Drivers
» Two-Tier and Three-Tier Models
» Writing JDBC Code to Access Databases
» Registering Your Database with ODBC
» Connecting to a Database
» Accessing the Database
» The ResultSet
» ResultSetMetaData
» DatabaseMetaData
» Getting Information on Tables
» Executing SQL Queries, Printing out ResultSets, A Simple JDBC Program
» Building Higher Level JDBC Objects
» Building a Database Object, A Visual Database Program
» Executing a Query
» The Query Result Dialog
» Example Files
» Summary


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» All about JAXP, Part 1
» Make Database Queries Without the Database
» Load List Values for Improved Efficiency
» 2 Ways To Implement Session Tracking
» A Simple Way to Read an XML File in Java
» Develop Aspect-Oriented Java Applications with Eclipse and AJDT