Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Implementing Data Access Object Pattern in Java Applications

Notes 1

DAO (Data Access Object) defines an abstraction layer for database interactions, separating business logic from data persistence mechanisms. This pattern centralizes data operations into a dedicated API, shielding application code from direct database dependencies.

In practice, a DAO interface declares atomic operations like create, read, update, and delete methods. Concrete implementations handle the actual database communication, while entity classes mirror database table structures.

Typical project organization includes:

  • Entity classes in packages like model or entity
  • DAO interfaces and implementations in dao or repository packages
  • Business logic consuming DAO interfaces rather than concrete implementations

Consider this entity representing a user:

public class User implements Serializable {
    private Long userId;
    private String username;
    private String email;
    private LocalDate registrationDate;
    private Boolean activeStatus;
    
    // Getters and setters omitted for brevity
}

A corresponding DAO interface defines operations:

public interface UserDao {
    int insertUser(User user);
    int removeUser(Long userId);
    User findUserById(Long userId);
    List<User> findAllUsers();
}

The implementation handles database connectivity:

public class UserDaoImpl implements UserDao {
    private final String dbDriver = "com.mysql.cj.jdbc.Driver";
    private final String connectionUrl = "jdbc:mysql://localhost:3306/appdb?useSSL=false";
    private final String dbUser = "appuser";
    private final String dbPassword = "securepass";
    
    @Override
    public int insertUser(User user) {
        Connection conn = null;
        PreparedStatement stmt = null;
        int affectedRows = 0;
        
        try {
            Class.forName(dbDriver);
            conn = DriverManager.getConnection(connectionUrl, dbUser, dbPassword);
            String sql = "INSERT INTO users VALUES (NULL, ?, ?, ?, ?)";
            stmt = conn.prepareStatement(sql);
            
            stmt.setString(1, user.getUsername());
            stmt.setString(2, user.getEmail());
            stmt.setDate(3, Date.valueOf(user.getRegistrationDate()));
            stmt.setBoolean(4, user.getActiveStatus());
            
            affectedRows = stmt.executeUpdate();
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            closeResources(stmt, conn);
        }
        return affectedRows;
    }
    
    @Override
    public int removeUser(Long userId) {
        Connection conn = null;
        PreparedStatement stmt = null;
        int result = 0;
        
        try {
            Class.forName(dbDriver);
            conn = DriverManager.getConnection(connectionUrl, dbUser, dbPassword);
            String deleteQuery = "DELETE FROM users WHERE user_id = ?";
            stmt = conn.prepareStatement(deleteQuery);
            stmt.setLong(1, userId);
            
            result = stmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResources(stmt, conn);
        }
        return result;
    }
    
    private void closeResources(PreparedStatement ps, Connection c) {
        try {
            if (ps != null) ps.close();
            if (c != null) c.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    // Other interface method implementations would follow similar patterns
}

Frameworks like MyBatis or Spring Data JPA provide higher-level abstractions that reduce boilerplate code while maintaining the separation of concerns that DAO promotes.

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.