Java Syntax

This topic applies to Java version only.

  1. The following example shows how to use Native Query to retrieve all the objects of the specified type. This syntax can be used with or without generics.
    NQSyntaxExamples.java: querySyntax1
    01private static void querySyntax1() { 02 ObjectContainer container = database(); 03 if (container != null) { 04 try { 05 List<Pilot> result = container.query(Pilot.class); 06 container.ext().configure().freespace(); 07 listResult(result); 08 } catch (Exception ex) { 09 System.out.println("System Exception: " + ex.getMessage()); 10 } finally { 11 closeDatabase(); 12 } 13 } 14 }
  2. In this example an anonymous predicate class is used to specify the query parameter:
    NQSyntaxExamples.java: querySyntax2
    01private static void querySyntax2() { 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 // each Pilot is included in the result 08 return true; 09 } 10 }); 11 listResult(result); 12 } catch (Exception ex) { 13 System.out.println("System Exception: " + ex.getMessage()); 14 } finally { 15 closeDatabase(); 16 } 17 } 18 }
  3. This example shows how to use NQ to sort the query results; anonymous predicate and anonymous comparator are used:
    NQSyntaxExamples.java: querySyntax3
    01private static void querySyntax3() { 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 // each Pilot is included in the result 08 return true; 09 } 10 }, new Comparator<Pilot>() { 11 public int compare(Pilot pilot1, Pilot pilot2) { 12 return pilot1.getPoints() - pilot2.getPoints(); 13 } 14 }); 15 listResult(result); 16 } catch (Exception ex) { 17 System.out.println("System Exception: " + ex.getMessage()); 18 } finally { 19 closeDatabase(); 20 } 21 } 22 }
  4. The following example shows a Native Query using external comparator and predicate. This can be useful when comparator and predicate are widely used and logically do not belong to the querying class:
    NQSyntaxExamples.java: PilotPredicate
    1private static class PilotPredicate extends Predicate<Pilot> { 2 public boolean match(Pilot pilot) { 3 // each Pilot is included in the result 4 return true; 5 } 6 }
    NQSyntaxExamples.java: PilotComparator
    1private static class PilotComparator implements Comparator<Pilot> { 2 public int compare(Pilot pilot1, Pilot pilot2) { 3 return pilot1.getPoints() - pilot2.getPoints(); 4 } 5 }
    NQSyntaxExamples.java: querySyntax4
    01private static void querySyntax4() { 02 ObjectContainer container = database(); 03 if (container != null) { 04 try { 05 List<Pilot> result = container.query(new PilotPredicate(), 06 new PilotComparator()); 07 listResult(result); 08 } catch (Exception ex) { 09 System.out.println("System Exception: " + ex.getMessage()); 10 } finally { 11 closeDatabase(); 12 } 13 } 14 }
  5. In java versions without generics syntax is similar:
    NQSyntaxExamples.java: querySyntax5
    01private static void querySyntax5() { 02 ObjectContainer container = database(); 03 if (container != null) { 04 try { 05 List result = container.query(new Predicate() { 06 public boolean match(Object obj) { 07 // each Pilot is included in the result 08 if (obj instanceof Pilot) { 09 return true; 10 } 11 return false; 12 } 13 }, new Comparator() { 14 public int compare(Object object1, Object object2) { 15 return ((Pilot) object1).getPoints() 16 - ((Pilot) object2).getPoints(); 17 } 18 }); 19 listResult(result); 20 } catch (Exception ex) { 21 System.out.println("System Exception: " + ex.getMessage()); 22 } finally { 23 closeDatabase(); 24 } 25 } 26 }
  6. For java versions that do not provide Comparator interface (<JDK1.2) db4o provides QueryComparator interface with the same functionality:
    NQSyntaxExamples.java: querySyntax6
    01private static void querySyntax6() { 02 // this example will only work with java versions without 03 // generics support 04 ObjectContainer container = database(); 05 if (container != null) { 06 try { 07 List result = container.query(new Predicate() { 08 public boolean match(Object obj) { 09 // each Pilot is included in the result 10 if (obj instanceof Pilot) { 11 return true; 12 } 13 return false; 14 } 15 }, new QueryComparator() { 16 public int compare(Object pilot1, Object pilot2) { 17 return ((Pilot) pilot1).getPoints() 18 - ((Pilot) pilot2).getPoints(); 19 } 20 }); 21 listResult(result); 22 } catch (Exception ex) { 23 System.out.println("System Exception: " + ex.getMessage()); 24 } finally { 25 closeDatabase(); 26 } 27 } 28 }