Before the report can be generated, we will need to store some data to our database. We will use the following Pilot class:
01public class Pilot { 02
private String name; 03
04
private int points; 05
06
public Pilot(String name, int points) { 07
this.name = name; 08
this.points = points; 09
} 10
11
public String getName() { 12
return name; 13
} 14
15
public void setName(String name) { 16
this.name = name; 17
} 18
19
public int getPoints() { 20
return points; 21
} 22
23
public boolean equals(Object obj) { 24
if (obj instanceof Pilot) { 25
return (((Pilot) obj).getName().equals(name) && 26
((Pilot) obj).getPoints() == points); 27
} 28
return false; 29
} 30
31
public String toString() { 32
return name + "/" + points; 33
} 34
35
public int hashCode() { 36
return name.hashCode() + points; 37
} 38
}
Pilot class has name
and points
fields, which can be obtained through getName()
and getPoints()
methods.
Let's store some pilots to the database:
01private static void storePilots() { 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("Professional Pilot #" + (i + 10), i + 10); 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
}