Java Reference
In-Depth Information
To implement the interface I need to know the table structure. Again, to keep things simple,
assume I only have a single table, called
product
. For the purposes of this example the
table will be created in the DAO implementation class, using the H2 database.
The implementation class is
JDBCProductDAO
. A couple of excerpts are shown ahead.
Java developers will find both the code and the attendant painful verbosity quite familiar.
The following listing shows the beginnings of the implementation, including constants to
represent the URL and driver class.
Listing 8.3. A JDBC implementation of the DAO interface
public class
JDBCProductDAO
implements
ProductDAO {
private static final
String
URL
= "jdbc:h2:build/test";
private static final
String
DRIVER
= "org.h2.Driver";
public
JDBCProductDAO() {
try
{
Class.
forName
(
DRIVER
);
}
catch
(ClassNotFoundException e) {
e.printStackTrace();
return
;
}
createAndPopulateTable();
}
// ... More to come ...
}
The
import
statements have been mercifully omitted. The private method to create and
populate the table is shown in the next listing.