1. query all
DB = db4o. openfile ("Customer. yap");// Query all customersCustomer allcus =NewCustomer (); objectset set = dB. Get (allcus );While(Set. hasnext () {system. Out. println (customer) set. Next (). getname ());}
2. Update the object
DB = db4o. openfile ("Customer. yap"); Objectset set = dB. Get (NewCustomer ("Persia2"); Customer C1 = (customer) set. Next (); c1.setphonenumber ("82839681"); DB. Set (C1 );
Note:
The query operation creates a customer object in the memory and maintains contact with the object in the database by ID.
Db4o uses the cached OID as a reference to keep in touch. The connection will be stopped when the database is closed.
To update an object in a database, make sure that the latest version is in the memory. Otherwise, another new object will be saved:
Make sure that you have stored or retrieved objects in the same transaction (that is, opened from the last objectcontainer), and then your updates.
If not, db4o assumes that this is a new object, rather than a previously stored object.
The tostring method in the re-object is as follows:
PublicString tostring (){Return"["+This. Name +";"+This. Phonenumber +"]";}
You can directly print the object system. Out. println (set. Next ());
3. delete an object
DB = db4o. openfile ("Customer. yap"); Objectset set = dB. Get (NewCustomer ("Persia2"); Customer C1 = (customer) set. Next (); DB. Delete (C1 );
4. Database Configuration
You can use db4o. Configure (). messagelevel (INT level) to perform log operations.
There are a total of four log options:
0 No messages
1 Open and Close messages
2 messages for new, update, and delete
3 messages for activate and deactivate
For example:
Public ClassTestdelete {
Public Static VoidMain (string [] ARGs) {db4o. Configure (). messagelevel (2); objectcontainer DB =Null;Try{DB = db4o. openfile ("Customer. yap"); Objectset set = dB. Get (NewCustomer ("Customer12"); Customer C1 = (customer) set. Next (); DB. Delete (C1 );}Finally{If(DB! =Null) {DB. Close ();}}}}
Output:
[Db4o 6.4.54.11278 22:57:04]'Customer. yap'Opened o. k. [db4o 6.4.54.11278 22:57:04] 2817 Delete com. firstdb4o. entity. Customer [db4o 6.4.54.11278 22:57:04]'Customer. yap'Close request [db4o 6.4.54.11278 22:57:04]'Customer. yap'Closed
5. A complete curd example:
Package Com. firstdb4o. test; Import Java. Io. file; Import Com. db4o. db4o; Import Com. db4o. objectcontainer; Import Com. db4o. objectset; Import Com. firstdb4o. entity. address; Import Com. firstdb4o. entity. customer; Public Class Completeexample { /*** @ Param ARGs */ Public Static Void Main (string [] ARGs ){ New File (" Customer. yap "). Delete (); db4o. Configure (). messagelevel (2); objectcontainer DB = db4o. openfile (" Customer. yap "); Try { // Create DB. Set ( New Customer (" Customer1 "," Phone1 "); DB. Set ( New Customer (" Customer2 "," Phone2 "); Objectset result = (objectset) dB. Get ( New Customer (); listresult (result ); // Update Customer c = New Customer (); C. setname (" Customer1 "); Objectset r2 = (objectset) dB. Get (c); customer C1 = (customer) r2.next (); c1.setphonenumber ("Phone1modified "); R2.reset (); listresult (R2 ); // Delete DB. Delete (C1); objectset R3 = (objectset) dB. Get ( New Customer (); listresult (r3 );} Finally { If (DB! = Null ) {DB. Close ();}}} Private Static Void Listresult (objectset result ){ While (Result. hasnext () {system. Out. println (result. Next ();} system. Out. println (" ----------------- ");}}
Console output:
[Db4o 6.4.54.11278 23:16:25] File not found :' Customer. yap 'Creating new file [db4o 6.4.54.11278 23:16:26] 336 new COM. db4o. Ext. db4odatabase [db4o 6.4.54.11278 23:16:26]' Customer. yap 'Opened O. k. [db4o 6.4.54.11278 23:16:26] 173 new COM. firstdb4o. entity. customer [db4o 6.4.54.11278 23:16:26] 181 new COM. firstdb4o. entity. customer [customer1; phone1] [customer2; phone2] ----------------- [Customer1; phone1modified] ----------------- [Db4o 6.4.54.11278 23:16:26] 173 Delete com. firstdb4o. entity. Customer [customer2; phone2] ----------------- [Db4o 6.4.54.11278 23:16:26]' Customer. yap 'Close request [db4o 6.4.54.11278 23:16:26]' Customer. yap 'Closed