I'm going to rephrase your question and then answer it as I've interpreted it. Correct me if I'm wrong in the interpretation. You a) know how to make a stored proc on the DB server, and b) want to "objectize" the results from that proc in Java objects, but you c) aren't sure how to implement the materialization process, or the class whose instances will hold the results.
Since you are using a stored procedure approach, you are not like using a persistence architecture, like iBatis, Hibernate (Object-to-Relational mapping packages). So you would be implementing this the "old-fashioned" way, with factories, etc.
Here's how I've done this in the past. Let's say you have the simple case, where your class maps 1-1 with one table. Say your table is "Person" with columns "FName" and "LName"... something simple. You have two tasks:
1) create the business class
2) create the factory
I will omit discussion surrounding custom exceptions and exception translation within the factory, and stick to just answering the basic question.
--- 1) create the business class ---
CODE
public class Person
{
private string firstName = null;
private string lastName = null;
public Person()
{
}
public String getFirstName()
{
return this.firstName;
}
public void setFirstName(string aValue)
{
this.firstName = aValue;
}
public String getLastName()
{
return this.lastName;
}
public void setLastName(string aValue)
{
this.lastName = aValue;
}
}
--- 2) create the factory ---
I have found that the easiest way to do this is to structure the factory so that you can easily add methods that retrieve single objects or Collections. All methods are static, because you don't need an instance of the factory. The public (client) methods are whatever you need them to be, and the non-public methods facilitate the process.
CODE
import java.sql.Connection;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import [your Person class];
public abstract class PersonFactory
{
public static ArrayList getPersons(String aLastName)
throws SQLException
{
ArrayList result = null;
Connection con = null;
CallableStatement stmt = null;
ResultSet rs = null;
try
{
con = [get a connection];
stmt = con.prepareCall("{call proc_name[(?)]}");
stmt.setString(1, aLastName);
rs = stmt.executeQuery();
result = materialize(rs);
}
finally
{
try
{
rs.close();
}
catch (SQLException closeException)
{
}
try
{
stmt.close();
}
catch (SQLException closeException)
{
}
try
{
con.close();
}
catch (SQLException closeException)
{
}
}
return result;
}
/**
* Materializes all rows in ResultSet.
*/
private static Collection materialize(ResultSet rs)
throws SQLException
{
Person person = null;
// ensure result is not null;
ArrayList result = new ArrayList();
while (rs.next())
{
person = materializeSingle(rs);
result.add(person);
}
return result;
}
/**
* Materializes each row in ResultSet, creating
* an object from the row at the current cursor position.
*/
private static Person materializeSingle(ResultSet rs)
throws SQLException
{
Person person = new Person();
person.setFirstName(rs.getString("FName));
person.setLastName(rs.getString("LName));
return person;
}
}
In this way, you can add further methods easily, because all you have to do is code the connection/statement part. you can even go further and create a non-public method that will run any statement you send it, etc. You can also create a "FactoryUtil" class with static methods that will close connections, statements, and resultsets, squelching the SQLException that you can't do anything about. You can also include rollbacks in that util class. If you organize this correctly, it's a pretty clean implementation. And if you're going to do this manually, you want it to be clean & easy to maintain.
Hope this helps... feel free to ask if you have questions or if I have not correctly understood the question.