Using Sample Code

Db4o reference documentation accompanies theoretical material with example code, which can be used for the material evaluation and better understanding.

The examples are provided in java, c# and VB.NET. In the online version, you can use filters on the right pane to display the relevant content. Offline version provides the content depending on the downloaded version of db4o.

Most of the examples are console applications except for those clearly mentioning the environment (web-applications, windows forms etc.).

Java examples are JDK5 compatible.

Variables

The in-text code snippets are mainly used to show the syntax of a particular method. The following variables are used:

objectContainer - references and open ObjectContainer/IObjectContainer or ObjectClient/IobjectClient. For more information see Object Container and Embedded.

configuration variable is used to reflect Configuration object. See Configuration.

Embedded Code

The examples usually show only the method code demonstrating the functionality discussed. In this case, additional code (opening and closing database methods, listing results, global variable definitions) and persistent class definitions are not visible. In order to see them you can download the whole example as a *.zip file using the button in the top-right corner of the example code block. Note: usually the *.zip file contains all the examples for the current topic.

In the majority of the examples, objectContainer is opened and closed in each method. This is done to keep examples atomic. In the real-world application, the best technique would be to open an objectContainer on the first request and keep it open for the application lifetime.

The downloaded code can be easily used to run the examples.

In Eclipse (using JDK5):

  • unzip the contents in /Example/com/db4odoc/[example_name] folder (check the correct package name in the code files);
  • select File/New/Project/Java Project;
  • choose the project name as "ExampleProject";
  • select "Create project from existing source" and browse to the /Example folder;
  • press "Next";
  • in the "Libraries" tab add the required db4o libraries (db4o-x.x-java5.dll, db4o-x.x-nqopt.jar, db4o-x.x-db4ounit.jar, bloat.jar);
  • press "Finish";
  • run the example.