Download and install db4o
Db4o all the latest versions can be downloaded directly on the official website, into the db4o download page, we can see the latest for Java stable version, including JAR, source code, Getting Started document, API and other content of the complete packaging files only 6 mb,db4o There is an Object database management tool ObjectManager, the current version is 1.8.
Then create a new Java project in Eclipse and import the Db4o Object Database engine package Db4o-8.0.249.16098-all-java5.jar into the project. Since db4o supports multiple versions of the JDK, in addition to the Db4o-8.0.249.16098-all-java5.jar for JDK 5.0, there are jar packages for JDK 1.1, 1.2-1.4 to accommodate a variety of environments. Db4o is more natural than Hibernate, IBATIS SQL Maps, and does not require too many references to third-party support libraries.
The package structure of db4o
Db4o How to persist objects? Browsing through the directory can be found, as with traditional RDBMS, Db4o also has its own database files, in db4o the suffix name of the database file is "*.yap". Let's take a look at the main package structure of the Db4o object Database engine:
Com.db4o
The com.db4o contains the most commonly used features when using db4o. The two most important interfaces are com.db4o.Db4o and Com.db4o.ObjectContainer. The Com.db4o.Db4o factory is the starting point for running db4o, a static method in this class that can open a database file, start a server, or connect to an existing server, and configure the DB4O environment before the database is opened. The Com.db4o.ObjectContainer interface is important, it is used for 99% of the time during development, and Objectcontainer can be used as a DB instance in single-user mode or as a client for db4o servers. Each Objectcontainer instance has its own transaction. All operations are guaranteed by a transaction. When Objectcontainer is opened, the next transaction is started immediately when the transaction is entered, commit (), or rollback (). Each Objectcontainer instance maintains its own managed stored and instantiated objects, which remain open when needed objectcontainer, and once closed, objects referenced by the in-memory database are discarded.
Com.db4o.ext
You may wonder why only a few methods can be seen in Objectcontainer, for the following reasons: The Db4o interface provides two channels, respectively, in com.db4o and Com.db4o.ext packages. This is done first to enable developers to get started quickly, and secondly to make it easier for other products to replicate basic db4o interfaces, and developers can see from this point that db4o is fairly lightweight. Each Com.db4o.ObjectContainer object is also a Com.db4o.ext.ExtObjectContainer object. can be converted into Extobjectcontainer for more advanced features.
Com.db4o.config
The com.db4o.config contains all the classes required to configure DB4O.
Com.db4o.query
The Com.db4o.query package contains the predicate classes required to construct a "native query, NQ (Native Queries)". NQ is the most important query interface for DB4O.
Initializing the database
The DB4O provides two modes of operation, local mode and server mode, respectively. Local mode refers to opening the DB4O database file directly in the program to operate:
Db4oembedded.openfile (Db4oembedded.newconfiguration (), "Auto.yap");
In the server mode, the client accesses the server by IP address, port, and authorization password:
Server-side:
Objectserver Server = Db4oclientserver.openserver ("Auto.yap", 1212); Server.grantaccess ("admin", "1");
Client:
IP for the IP address of the server db4oclientserver.openclient (IP, 1212, "admin", "1");
db4o does not automatically create "/export/db4o "directory, so it is best to initialize the database directory when creating the database file:
private void initdbfile (String dbName ) { file file = new file (dbName); if (File.exists ()) { return; } List<File> fList = new ArrayList<File> (); file pfile = file.getparentfile (); while (!pFile.exists ()) { flist.add (pFile);p file = Pfile.getparentfile (); } for (int i = Flist.size () - 1; i >= 0; i--) {file f = flist.get (i); F.mkdir (); }}
Working with databases
The two methods described above can be Objectcontainer instances, in the current distributed application environment of Java EE, the server mode is more practical, and the local mode is more suitable for single server application. Because distributed mode is more commonly used, this article will use distributed mode in the following example.
In the following example, a Uservo object is used, and then the crud of the Db4o object is described.
Uservo Object
public class uservo implements serializable { private static final long serialversionuid = -9129148024922569814l; private string username; private string password; public uservo () {} public uservo (string username, string password) {this.userName = userName;this.password = password; } public string getpassword () { return password; } public void SetPassword (String password) {this.password = password; } public string getusername () {return username; } puBlic void setusername (String username) {this.userName = userName; } @Override public boolean equals (Object obj) { if (this == obj) {return true;} if (! ( Obj instanceof uservo)) {return false;} uservo other = (Uservo) obj;if (! ( Username == null ? other.username == null : username.equals ( Other.username))) {Return false;} if (! ( Password == null ? other.password == null : password.equals ( Other.password))) {Return false;} return true; } @Override public int hashcode () {final int prime = 31;int result = 17;result = prime * result + ((Username == null) ? 0 : username.hashcode ()); result = prime * result + ((password == null) ? 0 : Password.hashcode ());return result; } @Override public string tostring () {return "Uservo [username=" + username + ", password=" + password + "]"; }}Add (Create)
public static void Main (string[] args) {Uservo vo = new Uservo ("admin", "1"); Open the database, IP for server IP objectcontainer db = db4oclientserver.openclient (IP, 1212, "admin", "1"); try {//Store data Db.store (VO);//COMMIT Transaction db.commit (); } catch (Exception e) {//exception occurred rollback db.rollback (); } finally {//close connection db.close (); }}
Read (Retrieve)
public static void Main (string[] args) {//Open database Objectcontainer db = Db4oclientserver.openclient (IP, 1212, "admin" , "1"); The try {//Construct query object Queries query = Db.query ();//Set constrained instance Query.constrain (uservo.class);//Set the field and constraints of the constrained instance query.descend (" UserName "). Constrain (" admin ");//Query object objectset<uservo> list = Query.execute ();//do Something with list} finally {//close connection db.close (); }}
Updates (update)
Public static void main (String[] args) { // Open database objectcontainer db = db4oclientserver.openclient (ip, 1212, "admin", "1"); try {objectset<uservo> result = db.query (new Predicate<UserVO> () { private static final long Serialversionuid = 1554763863522546547l; public boolean match ( USERVO&NBSP;VO) {// match username to admin uservoreturn vo.getusername (). Equals ("admin"); });if (Result.size () != 1) { throw new RuntimeException ("size does not matched."); Uservo vo = result.next ();// modify Passportvo.setpassword ("0");d b.store (VO);d b.commit (); } catch (exception e) {db.rollback (); } finally {// Close Connection db.close (); }} Remove (delete)
public static void Main (string[] args) {Uservo vo = new Uservo ("admin", "1"); Open database Objectcontainer db = Db4oclientserver.openclient (IP, 1212, "admin", "1"); try {db.delete (VO);d b.commit (); } catch (Exception e) {db.rollback (); } finally {//close connection db.close (); }}
Conclusion
Through this series of articles, Db4o's advantages have been vividly reflected, its addition, update, deletion is so simple, as the slogan of Db4o-"Only one line of code to store complex structure objects, greatly reducing the development time and cost, provide efficient performance, without DBA intervention."
If this article is not exhaustive, we can refer to the official "User Guide", db4o Chinese community is growing hot!
Resources
Learn
Access to products and technologies
Object-oriented database db4o: Installing and using db4o