I use Data Access Objects (DAO) for access to a database, check them out at
java.sun.com/.../DataAccessObject.html
Read the context entry on that page and see if it is what you are looking for.
Hmmm, i'll add some clarification here, in Figure 9.2 on that page your
class using the datasource would be represented by the BusinessObject, in a
case when we use a database DataSource would be a class *using* JDBC (i.e.
not JDBC itself), but the pattern allows us to for instance use regular
java.io to access a file as well, the ValueObject is our class/bean/entity
that contains the actual data... say in a webshop it could be a customer
class, order class, product class or similar.
Some of you may comment that shouldnt it be possible to encapsulate a
database by using only JDBC but the problem is that different database
managers implements things differently, take the left outer join syntax for
SQL Server and Oracle:
SQL Server: SELECT * FROM A LEFT OUTER JOIN B ON A.pk = B.fk;
Oracle: SELECT * FROM A, B WHERE A.pk = (+) B.fk;
These differences puts us in a situation where we also need to be able to
write our SQL specifically for each given database our system is supposed
to interact with.
By using DAO's we wont just solve this problem, but we will also enable our
solution to use virtually any datasource implementation we wish to ranging
from LDAP to RDBMSes on to simple files of our own custom making etc.