Advanced Database Persistence for Java

Examples & Reference Manual

 

Example 11 - Custom DAOs

This example shows how to use custom DAOs. That is, DAO Java classes not directly related to any table or view.

Custom DAOs can be used to group any arbitrary number of related or unrelated database sequence retrieval as well as Regular SQL queries.

Example Source Code

package examples;

import java.sql.Date;
import java.sql.SQLException;

import daos.primitives.DailyOperations;

/**
 * Example 11 - Custom DAOs
 * 
 * @author Vladimir Alarcon
 * 
 */
public class Example11 {

  public static void main(String[] args) throws SQLException {

    System.out.println("=== Running Example 11 - Custom DAOs ===");
    System.out.println(" ");

    // 1. Retrieve Sequence Value from Custom DAO DailyOperations
    // Example: Retrieve a value from the sequence DATA_FILE_SEQ

    long value = DailyOperations.selectSequenceDataFileSeq();
    System.out
        .println("1. Retrieve Sequence Value from Custom DAO DailyOperations - Retrieved DATA_FILE_SEQ value=" + value);

    // 2. Execute Regular Query
    // Example: Close the day summarizing all sales

    int rows = DailyOperations.closeDay(Date.valueOf("2017-02-28"));
    System.out.println("2. Execute Regular Query - Close the day summarizing all sales. Affected rows=" + rows);

    System.out.println(" ");
    System.out.println("=== Example 11 Complete ===");

  }

}

How to Run this example

The Example 11 is included in the download package. To run this example please refer to the section How to Run the Examples above.

Case #1: Retrieve a database sequence value

A value is retrieved from the sequence DATA_FILE_SEQ using the method selectSequenceDataFileSeq() on the DAO DailyOperations that is not directly related to any table or view.

Case #2: Run a SQL update

All the complexity of the SQL update is hidden from the Java code. It's simply exposed as the closeDay(java.sql.Date closingDate) method on the DailyOperations Java class.