|
What is J2MESDLIB?
It is a library, that belongs to the J2MEMicroDB project, developed in the Universitat Politècnica de Catalunya by the teachers Mª José Casany, Marc Alier and Pablo Casado, in colabration with the student of the Facultat d’Informàtica de Barcelona Nuria Lara and the students of the Postgrau en desenvolupament d’aplicacions amb PDAs I smartphones of the Fundació Politècnica de Catalunya Juan Antonio Sosa I Mariona Fecúndez. J2MESDLIB includes new functions that allow the use of relational tables in mobile devices. This API includes the necessary methods to create, modify, delete, and manage tables in a relational database.
How to use J2MESDLIB? (Programmers guide)
In this section we are going to discuss how to use the API in order to manage tables in a database. For this purpose we will use the example application that is distributed with the API (MobileApplication).
Before starting managing tables it is necessary to create a database which will be the table container. In order to do so we call the method generateDB. If the database does not exist this method generate the internal catalog tables that will store data about the user tables. This method receives one parameter that indicates if debugger information must be shown during execution This option can be useful when debugging the application.
After creating the database structure we can start to manage the tables in the database. To start with, we can create new tables using the method createTable. This method receives as first parameter the name of the table we want to create, second a vector with the data fields of the table and finally an integer that indicates the number of columns that form the primary key.
As an example, let’s assume we would like to create a contact database. We will create a table of contacts which stores the name and phone of each contact. The name of the contact will be identifier. The following code example, shows how to create the contact table.
J2MESDLIB.generateDB(false); //don't generete debug information
J2MEColumn c1=new J2MEColumn("name",true,J2MEColumn.ColumnType.VARCHAR,J2MEColumn.ColumnType.CHAR_SIZE20);
J2MEColumn c2=new J2MEColumn("telephone",false,J2MEColumn.ColumnType.VARCHAR,J2MEColumn.ColumnType.CHAR_SIZE15);
Vector columns=new Vector ();
columns.addElement(c1);
columns.addElement(c2);
J2METable t=J2MESDLIB.createTable("contactsData",columns,1);
To create the table fields we use the J2MEColumn class. The constructor of this class needs the name of the field, a boolean that indicates whether or not the field will be part of the primary key, the data type (BOOL, DOUBLE, INT and VARCHAR are the available data types in this version) and the size of the field.
After defining the columns of the table they have to be stored in a vector, and this vector is the second parameter of the createTable method.
Once the contact table has been created we can add new contacts to the table. Let’s see how to add a new contact on the table.
J2MESDLIB.generateDB(false);
t=J2MESDLIB.loadTable("contactsData");
//add a new empty row to the table
J2MERow row=t.createRow();
//add values to the new row
row.setColValue("Juan",0);
row.setColValue("93-444-44-44",1);
// add row to the table t.addRow(row);
//save changes and close table and database
t.saveTable();
t.closeTable();
J2MESDLIB.closeDB();
First of all we open the database by calling the generateDB method. After that it is necessary to load the table by calling the loadTable method. Then we can add a new empty row by calling the createRow method of the J2MERow class. After that we add values to the row by calling the method setColValue. The first parameter is the data we want to store and the second parameter the field number in the table. In our example, the contact name is the field number 0 and the phone the field number 1. Finally we must add the row to the table by using the method addRow and we must save the changes calling the method saveTable.
In the following example we are going to seach data in the table. The current version of the API only allows to search efficiently data by key. So we are going to assume we want to find the phone of the contact called “Juan”. The following code shows the required steps.
J2METable t=null; J2MERow row=null;
J2MESDLIB.generateDB(false);
t=J2MESDLIB.loadTable("contactsData");
if (t.rowCount()>0){
//Create a new empty object of the J2MEKey class
J2MEKey key=new J2MEKey();
//add the name of the contact to seach in the table
key.addValueKey("Juan");
//search row in a table by key (name)
if (t.seek(key)){
//If the contact identified by name exists read data
row=t.read();
}
}
t.close();
J2MESDLIB.closeDB();
First of all we must open the database and load the table by calling the generateDB and loadTable method. Next we use the J2MEKey class to seach the contact by primary key (name). To do this we must create an object of the J2MEKey class and add the name of the contact we would like to seach by using the addValueKey method. In our example we will seach the contact called “Juan”. By calling the seek method the contact “Juan” will be seached in the table. If found, we can retrieve his data calling the read method.
Next we will see how to update the phone of a contact named “Juan”. For this purpose we will use the following code:
J2MESDLIB.generateDB(false);
t=J2MESDLIB.loadTable("contactsData"); //create a new empty J2MEKey object
J2MEKey key=new J2MEKey(); //add the name of the contact to find
key.addValueKey("Juan"); //search the contact in the table by name
if (t.seek(key)){
//if found read contact data
J2MERow row=t.read();
//modify phone number
row.setColValue("93-333-33-33",1);
//update the current row of the table with the values stored in var. row
t.update(row);
//save changes
t.saveTable();
}
//close table and database
t.closeTable();
J2MESDLIB.closeDB();
After opening the database and loading the table we must create an object of the J2MEKey class in order to seach the contact we want to update. We seach the contact in the table using the seek method. If found, we will modify his phone by using the update method. The new data are stored in the parameter row. Finally we must store the changes in the table calling the saveTable method.
In the current version of the library the primary key can not by updated. That is why in the previous example we only changed the phone number.
As our final example we are going to delete a contact from the table. The following code show the steps:
J2METable t=null;
J2MESDLIB.generateDB(false);
t=J2MESDLIB.loadTable("contactsData");
//create a new empty J2MEKey object
J2MEKey key=new J2MEKey();
//add the name of the contact to find
key.addValueKey("Juan");
//search the contact in the table by name
if (t.seek(key))
//if found delete the contact
t.delete();
//close table and database
t.closeTable();
J2MESDLIB.closeDB();
We want to delete the contact named “Juan”. To do so we must first open the database and load the table. After that we create an object of the J2MEKey class to find the desired contact. Once found we can delete the contact by calling the delete method.
Demo application (MobileApplication)
In this section we would like to provide the user with an application guide, that shows how to use the demo application distributed with the library. This application manages a list of contacts although many error verifications have not been included (it is just a demo application that shows how to use the J2MESDLIB library).
To execute the application double click the MobileApplication.jad file. After that a screen with the list of available midlets must appear. Select the “contactMidlet” and press “launch”.Then a screen with the list of available contacts appears.
To add a new contact select the button “add contact” from the menu. Then the following screen appears.
Introduce the name and phone of the new contact and press “add”. Then the screen with the list of contacts appears. The new contact appears in the list.
In order to seach a contact, write the name of the contact in the Find Contact text box and select the “find” option from the menu. If the contact is found his or her data appers on the screen.
Then the user can either edit the contact data or delete it. To edit the data select “edit” from the menu. Then a new screen where the user can modify the phone number appers.
To delete the contact from the list select “delete” from the menu as shown in the previous figure.
Back
|