all about programming, daily activities, information and technology
Acquainted with db4o using Java Programming Language :)
Dear Visitor
Now, i want to share how to starting develop application between db4o and java programming language. Db4o is one of open source database object for java. It’s not only work with java programming language, you can using .Net too.
Db4o contain function to store object with single command, great embeddable database, and support for client server mode.
We can using Native Query with db4o. okay for more information about db4o you can visit it’s official site.
1. Preparation
The first you must import db4o library. If you do not have it yet, you could download db4o library at their official site.
There are many jar file in /lib folder from file that have been downloaded. Just Import file that named with db4o-[version]-all-java[java-version].jar
That file included all classes that can used in db4o.
After that create file Pilot.java in your project. I am using Netbeans IDE for this tutorial.
Click File- NewProject and then fill each of parameter.
After that Import db4o library into your project –> Right click on your libraries folder
Choose *.jar file that has name db4o-[version]-all-java[java-version].jar, because i am using db4o version 7.12 and java version 6 , so i am importing file with name db4o-7.12.126.14142-all-java5.jar
2. Write your code
Okay, our preparation to set up db4o has been finished. Now it’s time to write our code.
Create a new file Pilot.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/</p>
/**
*
* @author wahyusumartha
*/
public class Pilot {
private String name;
private int point;
public Pilot(String name, int point){
this.name = name;
this.point = point;
}
public Pilot() {
}
public String getName() {
return name;
}
public int getPoint() {
return point;
}
public void setName(String name) {
this.name = name;
}
public void setPoint(int point) {
this.point = point;
}
@Override
public String toString(){
return name + "/" + point;
}
}
We has been created Pilot.java. This file using as object that stored in db4o.
Create file Main.java to show how to store this object.
import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import java.util.List;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author wahyusumartha
*/
public class Main {
public static void main(String[] args) {
ObjectContainer db = Db4oEmbedded.openFile("test.db");
try {
Pilot pilot = new Pilot();
pilot.setName("Lewis Hamilton");
pilot.setPoint(22);
db.store(pilot);
System.out.println("Store " + pilot);
List<Pilot> pilots = db.queryByExample(Pilot.class);
System.out.println("" + pilots.size());
for (Pilot pilot1 : pilots) {
System.out.println("" + pilot1.getName());
System.out.println("" + pilot1.getPoint());
}
} finally {
db.close();
}
}
}
Output :
Store Lewis Hamilton/22
1
Lewis Hamilton
22
In Main.java, we can see the simple command to store object into db4o.
Statement db.store(pilot) is used to store object, this statement same like INSERT operation while we using RDBMS.
So,, this is very simple..
So, don’t miss it for next article..:)
Keep Spirit
Happy Coding
| Print article | This entry was posted by wahyusumartha on April 6, 2010 at 10:00 pm, and is filed under Java Programming. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |


about 3 months ago
Wah, pernah cobain db4o juga mas.. Aku dulu belajar itu juga. Sampai mana belajar db4o-nya? Memang simple banget bikin aplikasi dengan db4o. Tapi masih baru berkembang dan orang2 masih menyukai SQL sepertinya.