Advanced Database Persistence for Java

Examples & Reference Manual

 

Example 7 - Selecting Using Unique Indexes

This example shows how to select rows from a table that has unique indexes. For this purpose unique indexes act in a very similar way as a primary key.

Example Source Code

package examples;

import java.sql.SQLException;

import daos.ClientDAO;
import daos.VehicleDAO;

/**
 * Example 07 - Select by Unique Indexes
 * 
 * @author Vladimir Alarcon
 * 
 */
public class Example07 {

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

    System.out.println("=== Running Example 07 - Select by Unique Indexes ===");

    // 1. Select by Unique Index - Single Column
    // Example: Find the vehicle with VIN = VIN7492

    VehicleDAO v1 = VehicleDAO.selectByUIVin("VIN7492");
    if (v1 == null) {
      System.out.println("1. Select by Unique Index - Single Column - Vehicle not found.");
    } else {
      System.out.println("1. Select by Unique Index - Single Column - ID=" + v1.getId());
    }

    // 2. Select by Unique Index - Single Column
    // Example: Find the vehicle with ENGINE_NUMBER = EN102937

    VehicleDAO v2 = VehicleDAO.selectByUIEngineNumber("EN102937");
    if (v2 == null) {
      System.out.println("2. Select by Unique Index - Single Column - Vehicle not found.");
    } else {
      System.out.println("2. Select by Unique Index - Single Column - ID=" + v2.getId());
    }

    // 3. Select by Unique Index - Multiple Columns
    // Example: Find the client from California state, and driver's license
    // C535893758

    ClientDAO c1 = ClientDAO.selectByUIStateDriversLicense("CA", "C535893758");
    if (c1 == null) {
      System.out.println("3. Select by Unique Index - Multiple Columns - Client not found.");
    } else {
      System.out.println("3. Select by Unique Index - Multiple Columns - Client: " + c1.getName());
    }

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

  }

}

How to Run this example

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

Case #1: Using a single column

To retrieve a row use one of the out-of-the-box selectByUIColumns() methods that returns a single row.

This is demonstrated retrieving from the table VEHICLE .

Case #2: Using a single column (other index)

To retrieve a row use one of the out-of-the-box selectByUIColumns() methods that returns a single row.

This is demonstrated retrieving from the table VEHICLE .

Case #3: Using a multi-column index

To retrieve a row use one of the out-of-the-box selectByUIColumns() methods that returns a single row.

This is demonstrated retrieving from the table CLIENT where the columns STATE and DRIVERS_LICENSE are a composite unique index.