edu.jhu.tmaj.database
Class DatabaseQuery
java.lang.Object
edu.jhu.tmaj.database.DatabaseQuery
public abstract class DatabaseQuery
- extends java.lang.Object
A class for executing SELECT statement. Implementors must overwrite handleResultSet() thus
handling each row returned by the query properly. This class takes care of the overhead of
executing a sql statement such as getting a connection and returning it.
Example:
This code is used to populate a List with all the names of all the Projects.
(Assume the name of the Project is stored in a field called "ProjectName" in the "Projects"
table.)
final List projectNamesList=new ArrayList();
AbstractDatabaseQuery d = new AbstractDatabaseQuery("SELECT ProjectName FROM Projects", sqlMaker, cm, logger){
protected void handleResultSet(ResultSet rs) throws SQLException{
String projectName = rs.getString("ProjectName");
projectNamesList.add(projectName);
}
};
d.execute();
System.out.println("The names of the projects: " + d.projectNamesList);
Method Summary |
void |
execute()
|
protected abstract void |
handleResultSet(java.sql.ResultSet rs)
Called for every row the SELECT statement generates. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
DatabaseQuery
protected DatabaseQuery(java.lang.String sql,
DatabaseLink databaseLink)
- Creates this object.
- Parameters:
sql
- the SQL statement this class will execute.
DatabaseQuery
public DatabaseQuery(java.lang.String sql)
execute
public final void execute()
throws java.sql.SQLException
- Throws:
java.sql.SQLException
handleResultSet
protected abstract void handleResultSet(java.sql.ResultSet rs)
throws java.sql.SQLException
- Called for every row the SELECT statement generates. Subclasses overwrite this class and
handle the data from the ResultSet appropriately.
- Throws:
java.sql.SQLException