Developer Notes - ITwelzel.biz
Developer Notes - ITwelzel.biz
Developer Notes - ITwelzel.biz
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
The DAO Implementation<br />
A DAO interface has at least one concrete class that implements the interface for a<br />
specific type of data source. The following is an implementation of the Contacts<br />
DAO interface for an hsqldb data source:<br />
public class HSQLDBContactsDAO implements ContactsDAO<br />
{<br />
public static String GET_PERSON_STATEMENT =<br />
"select * from person where personID = ?";<br />
public static String GET_ADDRESS_STATEMENT =<br />
"select * from address where addressID = ?";<br />
public static String GET_COMPANY_STATEMENT =<br />
"select * from company where companyID = ?";<br />
public HSQLDBContactsDAO()<br />
{<br />
}<br />
protected PersonDAO getPerson(String personID)<br />
throws DAOException<br />
{<br />
Connection conn = null;<br />
PreparedStatement ps = null;<br />
ResultSet rs = null;<br />
PersonDAO ret = null;<br />
try<br />
{<br />
conn = getDataSource().getConnection();<br />
ps = conn.prepareStatement(GET_PERSON_STATEMENT,<br />
ResultSet.TYPE_SCROLL_INSENSITIVE,<br />
ResultSet.CONCUR_READ_ONLY);<br />
ps.setString(1, personID);<br />
rs = ps.executeQuery();<br />
if (rs.first())<br />
{<br />
ret = new HSQLDBPersonDAO(personID, rs);<br />
}<br />
<strong>Developer</strong> <strong>Notes</strong><br />
}<br />
rs.close();<br />
ps.close();<br />
conn.close();<br />
return ret;<br />
}<br />
catch (SQLException se)<br />
{<br />
throw new DAOException("SQLException: " + se.getMessage());<br />
}<br />
protected AddressDAO getAddress(String addressID)<br />
throws DAOException<br />
{<br />
Connection conn = null;<br />
PreparedStatement ps = null;<br />
ResultSet rs = null;<br />
N o v e m b e r 2 0 0 2<br />
51