The following examples show how to select objects matching a specific criteria. Store Pilots function is used to fill in the database.
Select only those pilots, which have 5 points.
01private static void selectPilot5Points() { 02
ObjectContainer container = database(); 03
if (container != null) { 04
try { 05
List<Pilot> result = container.query(new Predicate<Pilot>() { 06
public boolean match(Pilot pilot) { 07
// pilots with 5 points are included in the 08
// result 09
return pilot.getPoints() == 5; 10
} 11
}); 12
listResult(result); 13
} catch (Exception ex) { 14
System.out.println("System Exception: " + ex.getMessage()); 15
} finally { 16
closeDatabase(); 17
} 18
} 19
}
Select all pilots, whose name includes "Test".
01private static void selectTestPilots() { 02
ObjectContainer container = database(); 03
if (container != null) { 04
try { 05
List<Pilot> result = container.query(new Predicate<Pilot>() { 06
public boolean match(Pilot pilot) { 07
// all Pilots containing "Test" in the name 08
// are included in the result 09
return pilot.getName().indexOf("Test") >= 0; 10
} 11
}); 12
listResult(result); 13
} catch (Exception ex) { 14
System.out.println("System Exception: " + ex.getMessage()); 15
} finally { 16
closeDatabase(); 17
} 18
} 19
}
Select those pilots, whose name (number) ends with 6.
01private static void selectPilotsNumberX6() { 02
ObjectContainer container = database(); 03
if (container != null) { 04
try { 05
List<Pilot> result = container.query(new Predicate<Pilot>() { 06
public boolean match(Pilot pilot) { 07
// all Pilots with the name ending with 6 will 08
// be included 09
return pilot.getName().endsWith("6"); 10
} 11
}); 12
listResult(result); 13
} catch (Exception ex) { 14
System.out.println("System Exception: " + ex.getMessage()); 15
} finally { 16
closeDatabase(); 17
} 18
} 19
}