Advanced Database Persistence for Java Examples & Reference Manual |
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.
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 ==="); } }
The Example 07 is included in the download package. To run this example please refer to the section How to Run the Examples above.
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
.
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
.
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.