Keyword: Java Embedded Development dB data address book Text Database micro-Record Data
(These mini programs are, by example, dropped on code. Google): http://code.google.com/p/greenvm/downloads/list
Lmini is one of the loonframework branch projects. It is a 55 KB pure Java Embedded text database that does not support SQL statements and performs data operations in an object-oriented manner, the loonframework MAIN package is mainly responsible for implementing the hard disk persistence function of cached data. However, as a project branch, lmini does not have to be integrated with the loonframework MAIN package, but can also exist as a separate micro-database.
The objective of lmini development is to provide a simple and fast local data persistence method, such as game records, File Cache, simple addition, deletion, modification, and query operations can all be achieved through lmini, its advantage lies in its full separation from Java. the SQL package dependency is smaller than db4o and sqllite, and more than the functions provided by properties.
Lmini performs database operations through the MDB interface, and according to the different instances obtained by the MDB interface, the operation mode can be divided into three types: memory mode, single file mode and multi-file mode.
Memory mode:
After instantiating this mode, a database simulation operation will be performed directly in the memory opening area. When the program is closed, the memory database disappears and cannot be saved persistently. The call method is engine. getmemdb.
Single file mode:
After this mode is instantiated, the database will exist as a file, and the program will automatically generate a unique physical file on the hard disk. All operations will also be performed based on this physical file, as long as the file is not deleted, the data will permanently exist and the call method is engine. getmdbonly.
Multi-file mode:
After this mode is instantiated, the database will exist as a folder, and the program will automatically generate multiple physical files based on the created table in this folder. All operations will be performed based on this folder, as long as the folder and its files are not damaged, the data will permanently exist and the call method is engine. getmdbmany.
The preceding three modes are all interface implementations, and the operation methods are identical. They are only different in the Save mode.
Basic operation method:
1. Create and open a database:
After the MDB interface is instantiated and the begin method is called, lmini automatically monitors whether a text database exists at a specified location, and loads the file content if any, no empty database is automatically created at the specified location.
Lmini uses the begin method to open the database connection and end the connection. The specific code is as follows:
MDB = engine. getmdbonly ("C: // test. DB"); <br/> MDB. Begin (); <br/> MDB. End ();
2. Password protection:
In addition to the memory mode, lmini provides a simple password protection function to prevent theft of database files by others. You only need to set it when you create a database in begin, if the file is opened again when the password is incorrect, an exception is displayed. The Code is as follows:
MDB = engine. getmdbonly ("test. DB "); <br/> MDB. openTable ("test", typebase. string); <br/> // set the password <br/> MDB. begin ("wt98ab"); <br/> MDB. end ();
3. add, delete, modify, and query:
Lmini opens or creates a specified table using the OpenTable method, but unlike other databases, each table in lmini can only save one data type to simplify the lmini file processing process; currently, it supports five types: Long, integer, String, byte [], and object. We can inject the required types through the typebase class provided by lmini. If this parameter is left blank, lmini sets the default table store type to object, that is, all serialized objects can be stored on the local disk.
Lmini provides the insert, select, update, and delete methods for crud to call. You only need to inject related objects to complete the operation. The specific code is as follows:
MDB = engine. getmdbonly ("test. DB "); <br/> MDB. openTable ("test", typebase. string); <br/> // set the password <br/> MDB. begin ("wt98ab"); <br/> // insert data <br/> MDB. insert ("Jiu Bao", ""); <br/> MDB. insert ("", "I want to become a Navy"); <br/> MDB. insert ("Shore Ben", "All dead"); <br/> // delete data <br/> MDB. delete ("shore-based"); <br/> // change data <br/> MDB. update ("", "I want to be a pirate"); <br/> // query <br/> system. out. println (MDB. select ("ODA"); <br/> MDB. end ();
4. Data traversal in a table:
Lmini provides the gettablekey method to obtain the names of all fields in the current table, and the gettablelist method to obtain all fields and data in the current table. The Code is as follows:
MDB = engine. getmdbonly ("test. DB "); <br/> MDB. openTable ("test", typebase. string); <br/> // set the password <br/> MDB. begin ("wt98ab"); <br/> // insert data <br/> MDB. insert ("Jiu Bao", ""); <br/> MDB. insert ("", "I want to become a Navy"); <br/> MDB. insert ("Shore Ben", "All dead"); <br/> // delete data <br/> MDB. delete ("shore-based"); <br/> // change data <br/> MDB. update ("", "I want to be a pirate"); <br/> // query <br/> system. out. println ("said ODA:" + MDB. select (""); </P> <p> for (iterator it = MDB. gettablekey (). iterator (); it. hasnext ();) {<br/> string name = (string) it. next (); <br/> system. out. println ("field name:" + name); <br/> system. out. println ("data:" + (string) MDB. gettablelist (). get (name); <br/>}</P> <p> MDB. end ();
Program example:
This example provides a simple lmini address book operation example, which is transformed by addressbook-swing provided by the open-source metawidget project, replace the data in the table with the hard-coded data to operate the lmini database and correct some English forms as Chinese. The ownership benefits of the metawidget project are irrelevant to me. In this example, only the interface is used, we hereby declare that. (PS: the source code is in jar, and the metawidget UI is quite free of charge. It uses a 5 MB support library ||| ......)
The lmini operation method is very simple. The game archive device of the tloh project I announced yesterday will also be implemented using lmini. Currently, lmini is still being improved, hope you can give support and correction, in addition to the current release of lmini than loonframework in the main package version of an update, the specific difference to the release of Loonframework-0.2.0 can be seen.
(These mini programs are, by example, dropped on code. Google): http://code.google.com/p/greenvm/downloads/list