helpinghand
search
needassistance
 
How To
Java programming for mobile phones with Mimer SQL
Categories: JDBC, Mobile, Programming Examples
Introduction

This article explains how to access Mimer SQL databases in MIDlets when programming with the Sun Wireless Toolkit, both from a general point and by example. This article applies to the Sun Wireless Toolkit 1.0.4 or later.


About the Mimer JDBC driver for MIDlets

The Mimer JDBC/CLDC/MIDP driver is a strict subset of the regular JDBC for CDC/Foundation Profile API. Applications developed with Mimer JDBC/MIDP will run with little change in a regular JDBC environment. Programming skills obtained using regular JDBC programming may be applied to programming within the MIDP environment as well.

The Mimer JDBC driver allow MIDlets to access Mimer SQL servers just like any other application.

The following should be considered when working with Mimer JDBC for MIDP:

  • All methods requiring floating point data types (such as ResultSet.getDouble) are omitted. See below on floats, doubles and BigDecimals for guidelines on how to handle these cases.

  • All methods requiring a java.sql.Date, java.sql.Time and java.sql.Timestamp data type are omitted. When working with SQL date, time and timestamp data, please consider using ResultSet.getString, PreparedStatement.setString and other string getter and setter methods instead.

  • The JDBC driver has no finalize() methods. This means that it is even more important for applications to explicitly close database objects when done with them.

  • Since the java.math library is omitted from MIDP, no BigDecimals may be used. Please consider using the appropriate java.lang.String getter or setter method.

  • There is no java.sql interface. Programs interact directly against the com.mimer.jdbc classes instead. See below for details in this matter.



Importing the JDBC classes
Because the K Virtual Machine per default does not allow loading system packages (java.*), applications accessing Mimer SQL databases should import the com.mimer.jdbc.* classes instead. This makes the application less portable than a regular JDBC application, but these are the constraints set by the environment.

The following Mimer classes correspond to the named interface classes in the regular JDBC API.
java.sql.BatchUpdateException  com.mimer.jdbc.BatchUpdateException
java.sql.Blob com.mimer.jdbc.Blob
java.sql.CallableStatement com.mimer.jdbc.CallableStatement
java.sql.Clob com.mimer.jdbc.Clob
java.sql.Connection com.mimer.jdbc.Connection
java.sql.DatabaseMetaData com.mimer.jdbc.DatabaseMetaData
java.sql.DataSource com.mimer.jdbc.MimerDataSource
java.sql.DataTruncation com.mimer.jdbc.DataTruncation
java.sql.PreparedStatement com.mimer.jdbc.PreparedStatement
java.sql.ResultSet com.mimer.jdbc.ResultSet
java.sql.ResultSetMetaData com.mimer.jdbc.ResultSetMetaData
java.sql.SQLException com.mimer.jdbc.SQLException
java.sql.SQLWarning com.mimer.jdbc.SQLWarning
java.sql.Statement com.mimer.jdbc.Statement
java.sql.Types com.mimer.jdbc.Types

See also the javadoc for the Mimer JDBC/MIDP driver.


FLOAT and DOUBLE PRECISION
Databases with FLOAT, DOUBLE PRECISION and INTEGER(n) columns where it is a requirement that these numbers should be exposed in a MIDlet, can use one of the following tricks to go around the float and BigDecimal limitation in the K Virtual Machine.

1. It is always possible to get the SQL FLOAT or DOUBLE PRECISION value into a java.lang.String. This will work particularly well if the data is only to be displayed.

2. To handle fractions so that they may be used to some calculations, make sure to always return integers and reserving the last digits of the integer for decimals at the SQL level. If the database has a FLOAT column and we are interested in 4 decimals, do the following SQL:
select cast(OUR_FLOAT*10000 as bigint) from OUR_TABLE

At the application level, place the result in an int or a long, but assume that the last 4 digits are decimals when displaying values to the user. Similarly, decimals entered by the user must be handled in the same way by the application before being inserted to the database. The following SQL statement may be used to make the float appear correctly in a table.
insert into OUR_TAB(OUR_FLOAT) values (cast(? as float)/10000);


DATE, TIME and TIMESTAMP
Date, Time and Timestamp system classes (java.util.Date) are not included in the MIDP specification. Therefore the Mimer client cannot return Date, Time or Timestamp objects. Please use string getter and setter methods instead.


Installing the Mimer MIDP driver

To make the Mimer JDBC/MIDP driver available in all MIDlet projects, copy the midjdbc2.jar file to the directory {j2mewtk.dir}/apps/lib (where {j2mewtk.dir} is the installation directory for the Sun Wireless Toolkit).

To make the Mimer JDBC/MIDP driver available only in a specific project, copy the midjdbc2.jar file to the lib directory of your project. For example {j2mewtk.dir}/apps/MimerDemo/lib.


Packaging MIDlets

The ktoolbar application is normally used to compile and package MIDlets. This application automatically selects libraries from either the global or local lib directory for inclusion in the MIDlet.

For production releases it is important to use an obfuscator. The obfuscator tool will minimize the size of the packaged MIDlet.


Obfuscation and minimizing code

The Sun Wireless Toolkit 1.0.4 is shipped only with a plug-in for the Retroguard obfuscator. The newer verison 2.0 is shipped with a plug-in for the ProGuard obfuscator, which is used by default. Although RetroGuard obfuscates well, it is inferior to ProGuard when it comes to minimizing code size.

The Mimer JDBC/MIDP driver is shipped with most JDBC features available. Some JDBC classes have dozens of methods, which may never be used. The ProGuard obfuscator does a pretty good job in removing methods that arenít used. Although the Mimer JDBC/MIDP driver is almost 100 KB in size, unless you use every available piece of the API, the resulting MIDlet will be considerably smaller.


The sample application

Provided here is a sample MIDlet package (right-click and 'Save Link Target As...'), that queries a backend server for phone numbers. Do the following to setup the demo.

1. Unzip the mimermidpdemo.zip in the {j2mewtk.dir}/apps directory.

2. The file {j2mewtk.dir}/apps/MimerDemo/src/schema.sql contains an SQL script that creates the database. The created table contains three fields; name, group, phone number. If the phone directory makes you curious, these are the names, countries (for group) and score (for phone numbers) of the top 200 tennis players as of 2003-05-01. Run the script, for example:

C:\WTK20\apps\MimerDemo\src>bsql mimersql


MMMMM MMMMM MMMMM MMMMM MMMMM MMMMMMMMMM MMMMMMMM
MMMMMM MMMMMM MMMMM MMMMMM MMMMMM MMMMMMMMMM MMMMMMMMM
MMMMMM MMMMMM MMM MMMMMM MMMMMM MMM MMM MMM MMM
MMMMMMMMMMMMM MMM MMMMMMMMMMMMM MMMMM MMMMMMM
MMM MMMMM MMM MMM MMM MMMMM MMM MMM MMM MMM MMM
MMMM MMM MMMM MMMMM MMMM MMM MMMM MMMMMMMMMM MMMM MMMM
MMMM M MMMM MMMMM MMMM M MMMM MMMMMMMMMM MMMM MMMM



(C) Copyright Upright Database Technology AB. All rights reserved.


M I M E R / B S Q L
Version 9.1.3C


Username: SYSADM
Password:

SQL>read 'schema.sql';


3. Change the below three lines of BrowsePhoneNumbers.java to correspond to your server, node and port number.
mds.setServerName("gere.mimer.se");
mds.setDatabaseName("server92");
mds.setPortNumber(4712);

4. Start ktoolbar, load the application. Compile and run it normally.


Problems

If you are using Microsoft Windows and encounter a "Wrong name" message while preverifying a package, this may be because the 2.0 version loses a couple of Mimer classes since Windows filenames are case insensitive. See the article Packaging Mimer JDBC MIDlets for a solution.


Links

Sun J2ME Wireless Toolkit.

Mobile Information Device Profile (MIDP).

ProGuard obfuscator.

RetroGuard obfuscator.



Last updated: 2007-03-23

 

Powered by Mimer SQL

Powered by Mimer SQL