Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Interfacing to a database

  Asked By: Jeff    Date: Jun 13    Category: Java    Views: 795
  

I was wondering what the best way to interface to a
database table is e.g.
1) create an entity bean to represent the table;
2) create a regular bean to represent the table;
3) create a single class for the table with a bunch of
getters and setters;
4) create a class that handles more than one table;
5) other???

Also, how might XML play a role in this? Can an XML
file represent a table or just results from a table??

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Wade Jordan     Answered On: Jun 13

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.

 
Didn't find what you were looking for? Find more on Interfacing to a database Or get search suggestion and latest updates.




Tagged: