This group of examples shows how to select ranges of objects. Store Pilots function is used to fill in the database.
Select "Test" pilots with the points range of more than 6.
01private static void selectTestPilots6PointsMore() { 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
// and 6 point are included in the result 09
boolean b1 = pilot.getName().indexOf("Test") >= 0; 10
boolean b2 = pilot.getPoints() > 6; 11
return b1 && b2; 12
} 13
}); 14
listResult(result); 15
} catch (Exception ex) { 16
System.out.println("System Exception: " + ex.getMessage()); 17
} finally { 18
closeDatabase(); 19
} 20
} 21
}
Select all pilots, who have points in [6,12] range.
01private static void selectPilots6To12Points() { 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 having 6 to 12 point are 08
// included in the result 09
return ((pilot.getPoints() >= 6) && (pilot.getPoints() <= 12)); 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 pilots randomly: random array of point values is generated, those pilots who have points values within this array are included in the result set.
01private static void selectPilotsRandom() { 02
ObjectContainer container = database(); 03
if (container != null) { 04
try { 05
List<Pilot> result = container.query(new Predicate<Pilot>() { 06
private ArrayList randomArray = null; 07
08
private List getRandomArray() { 09
if (randomArray == null) { 10
randomArray = new ArrayList(); 11
for (int i = 0; i < 10; i++) { 12
randomArray.add((int) (Math.random() * 10)); 13
System.out.println(randomArray.get(i)); 14
} 15
} 16
return randomArray; 17
} 18
19
public boolean match(Pilot pilot) { 20
// all Pilots having points in the values of 21
// the randomArray 22
return getRandomArray().contains(pilot.getPoints()); 23
} 24
}); 25
listResult(result); 26
} catch (Exception ex) { 27
System.out.println("System Exception: " + ex.getMessage()); 28
} finally { 29
closeDatabase(); 30
} 31
} 32
}
Select pilots with even points.
01private static void selectPilotsEven() { 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 having even points 08
return pilot.getPoints() % 2 == 0; 09
} 10
}); 11
listResult(result); 12
} catch (Exception ex) { 13
System.out.println("System Exception: " + ex.getMessage()); 14
} finally { 15
closeDatabase(); 16
} 17
} 18
}
Select one pilot and quit.
01private static void selectAnyOnePilot() { 02
ObjectContainer container = database(); 03
if (container != null) { 04
try { 05
List<Pilot> result = container.query(new Predicate<Pilot>() { 06
boolean selected = false; 07
08
public boolean match(Pilot pilot) { 09
// return only first result (first result can 10
// be any value from the resultset) 11
if (!selected) { 12
selected = true; 13
return selected; 14
} else { 15
return !selected; 16
} 17
} 18
}); 19
listResult(result); 20
} catch (Exception ex) { 21
System.out.println("System Exception: " + ex.getMessage()); 22
} finally { 23
closeDatabase(); 24
} 25
} 26
}
01private static void storeDuplicates() { 02
new File(DB4O_FILE_NAME).delete(); 03
ObjectContainer container = database(); 04
if (container != null) { 05
try { 06
Pilot pilot; 07
for (int i = 0; i < OBJECT_COUNT; i++) { 08
pilot = new Pilot("Test Pilot #" + i, i); 09
container.set(pilot); 10
} 11
for (int i = 0; i < OBJECT_COUNT; i++) { 12
pilot = new Pilot("Test Pilot #" + i, i); 13
container.set(pilot); 14
} 15
container.commit(); 16
} catch (Db4oException ex) { 17
System.out.println("Db4o Exception: " + ex.getMessage()); 18
} catch (Exception ex) { 19
System.out.println("System Exception: " + ex.getMessage()); 20
} finally { 21
closeDatabase(); 22
} 23
} 24
}
01private static void selectDistinctPilots() { 02
ObjectContainer container = database(); 03
if (container != null) { 04
try { 05
DistinctPilotsPredicate predicate = new DistinctPilotsPredicate(); 06
List<Pilot> result = container.query(predicate); 07
listResult(predicate.uniqueResult); 08
} catch (Exception ex) { 09
System.out.println("System Exception: " + ex.getMessage()); 10
} finally { 11
closeDatabase(); 12
} 13
} 14
}
1