-2

I'm looking for a tutorial / best practice example on how to implement DAO layer in pure Java/JDBC.

Are most DAOs based on Data Mapper pattern ?

Is Active Record pattern also used ?

How do You do it ?

Sebastian Dusza
  • 2,302
  • 2
  • 24
  • 49
  • 1
    I have seen DAO's mostly using Data Mapper. You can do it by converting resultset(obtained from querying) into your domain object. – Ankit Bansal Mar 26 '13 at 15:56
  • Ok, how do I allow other layers of my App to efficiently iterate over a 100k table using only API provided by my library ? And what if those results have to be filtered/sorted/paginated ? Do I create a dedicated Iterator class ? – Sebastian Dusza Mar 26 '13 at 16:08
  • 1
    i think your dao interface should be such that, you always return domain object or List or Set(from java util) of domain object. To achieve this, you have to parse resultset and convert it appropriately. Then your other layers talk to dao, get your domain objects back and work with them. – Ankit Bansal Mar 26 '13 at 16:12
  • So in other words to iterate over 100k table I have to load all 100k rows into some List/Set implementation and return it to the user ? – Sebastian Dusza Mar 26 '13 at 16:15
  • 1
    No that's not my point. My point was your other layers never talk to any of JDBC component, and work with standard java libraries. If you want to return lesser number of record(like pagination), you can create dao method accepting lower and upper bound that you can pass to query to return appropriate records. – Ankit Bansal Mar 26 '13 at 16:23
  • Ok, I think I understand the concept. – Sebastian Dusza Mar 26 '13 at 16:34

3 Answers3

4

The only thing you need for a DAO is an interface and an implementation. Your interface defines the operations, and the implementation provides the implementation.

The thing that makes a DAO special is that the purpose of the interface and implementation is specifically data access. The way your application handles data access is hidden behind the abstraction of your DAO.

Lets say you see some jdbc code online. You want to use that code in a dao.

Statement st = conn.createStatement();
st.executeUpdate("INSERT INTO sometable " +
                   "VALUES (...)");

So create an interface

interface ThingDao {
   public void save(Thing thing);
}

and your implementation

public ThingDaoImpl implements ThingDao {

    ThingDaoImpl() {
        // do setup around connections
    }

    public void save(Thing thing) {
        // put the jdbc code here
    }

}

Now lets say you want to make your life easy and use something like Spring's JdbcTemplate. The DAO pattern doesn't change. The only change is that in your implementation, you would use the template to perform the operations. That's important because it means that any code that used your Dao doesn't have to change since you did not change the interface. That is the point of the DAO abstraction.

hvgotcodes
  • 109,621
  • 25
  • 195
  • 231
2

If you want painless JDBC here is my favorite solution...Spring JDBC Template documentation

You will still need to learn about Spring though

Michael Técourt
  • 3,157
  • 1
  • 25
  • 43
  • that's not what he asked. – hvgotcodes Mar 26 '13 at 15:59
  • 1
    your description of how to implement a DAO is included in Spring's JDBC documentation in the "best practice" chapter... The point was to say "hey you don't have to reinvent a JDBC DAO from scratch, you can use Spring's JDBC template instead of reinventing stuff Spring already did better" – Michael Técourt Mar 26 '13 at 16:21
1

There are good books, e.g.:

See the answer for How do I implement a DAO manager using JDBC and connection pools?

Community
  • 1
  • 1
Paul Vargas
  • 38,878
  • 15
  • 91
  • 139