Storing and Retrieving Data from a Database
- Applications running on both Connected Limited Device Configuration (CLDC) and Connected Device Configuration (CDC) devices use RMS for local data management.
- J2ME applications that run on CDC devices are also capable of utilizing a Relational DataBase Management System (DBMS)
- The DBMS is typically located on a server connected to the device over a network, although some CDC devices might have the DBMS stored locally on a hard drive.
- A CDC-based J2ME application interacts with commercial DBMSs such as Oracle, DB2, Sybase, and Microsoft Access by using a combination of Java data objects that are defined in the Java Database Connection (JDBC) specification and by using the Structured Query Language (SQL).
- SQL is the language used to construct the message (called a query) that is sent to the DBMS to request, update, delete, and otherwise manipulate data in the DBMS.
Java DataBase Connection (JDBC) API
- JDBC is an API provided in J2ME for its developers to write high-level code that accesses all popular DBMSs. It acts as a communication link between the application and a DBMS.
- A J2ME application can have database access using the Java Database Connection (JDBC) interface
- The JDBC interface has methods that perform the following database related operations:
- Open a connection to a database management system (DBMS)
- Transmit messages (queries) to insert, retrieve, modify, or delete data stored in a database.
- Additional JDBC interfaces are used to interact with data that is returned to the J2ME application by the DBMS.
JDBC Driver and JDBC API
- JDBC Driver is a system software, which is implemented as a part of Java Runtime Environment (JRE) for doing the conversion of high-level JDBC API calls into a low-level API calls that can be further processed by the DBMS.
- Java programmers generally use high-level JDBC interfaces that are defined in the JDBC API to write a routine that interacts with the DBMS.
- The JDBC driver translates the routine into low-level messages that are understood and processed by the DBMS.
- Open a connection between the DBMS and the J2ME application
- Translate low-level equivalents of SQL statements sent by the J2ME application into messages that can be processed by the DBMS
- Return data that conforms to the JDBC specification to the JDBC driver
- Return information, such as error messages, that conforms to the JDBC specification to the JDBC driver
- Provide transaction management routines that conform to the JDBC specification
- Close the connection between the DBMS and the J2ME application
JDBC Packages
- The JDBC API is contained in two packages. The first package is called java.sql and is part of the J2SE.
- java.sql contains core JDBC interfaces of the JDBC API. It include the JDBC interfaces that provide the basics for connecting to the DBMS and interacting with data stored in the DBMS.
- The other package that contains the JDBC API is javax.sql, which extends java.sql and is in the J2ME.
- Included in the javax.sql package is the JDBC interface that interacts with Java Naming and Directory Interface (JNDI) and the JDBC interface that manages connection pooling, among other advanced JDBC features.
Overview of the JDBC Process
- Although each J2ME application is different, J2ME applications use a similar process for interacting with a DBMS.
- This process is divided into five routines:
- Loading the JDBC driver,
- Connecting to the DBMS,
- Creating and executing a statement,
- Processing data returned by the DBMS, and
- Terminating the connection with the DBMS.
Load the JDBC Driver
- The JDBC driver must be loaded before the J2ME application can connect to the DBMS.
- The Class.forName() method is used to load the JDBC driver.
- The driver is loaded by calling the Class.forName() method and passing it the name of the driver, as shown in the following code segment:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connect to the DBMS
- Once the driver is loaded, the J2ME application must connect to the DBMS using the DriverManager.getConnection() method.
- The java.sql.DriverManager class is the highest class in the java.sql hierarchy and is responsible for managing driver information.
- The DriverManager.getConnection() method is passed the URL of the database, along with the user ID and password if required by the DBMS.
- The URL is a String object that contains the driver name and the name of the database that is being accessed by the J2ME application.
- The DriverManager.getConnection() returns a Connection interface that is used throughout the process to reference the database.
- The java.sql.Connection interface is another member of the java.sql package that manages communications between the driver and the J2ME application.
- It is the java.sql.Connection interface that sends statements to the DBMS for processing.
The below code snippet illustrates the use of the DriverManager.getConnection() method:
String url = "jdbc:odbc:CustomerInformation";
String userID = "jim";
String password = "keogh";
private Connection Db;
try {
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver");
Db = DriverManager.getConnection(url,userID,password);
}
Create and Execute an SQL Statement
- The next step is to send an SQL query to the DBMS for processing. This is done with the help of a Statement object that can contain an SQL Query for execution.
- An SQL query refers to a SQL command that directs the DBMS to do something, such as return rows of data to the J2ME application.
- The Connect.createStatement() is used to create a Statement object.
- The Statement object is then used to execute a query and return a ResultSet object that contains the response from the DBMS, which is usually one or more rows of information requested by the J2ME application.
- Typically, the query is assigned to a String object, which is passed to the Statement object’s executeQuery() method.
- Once the ResultSet is received from the DBMS, the close() method is called to terminate the statement.
The code snippet given below retrieves all the rows and columns from the Customers table:
Statement DataRequest;
ResultSet Results;
try {
String query = "SELECT * FROM Customers";
DataRequest = Db.createStatement();
Results = DataRequest.executeQuery (query);
DataRequest.close();
}
Process Data Returned by the DBMS
- The java.sql.ResultSet object is assigned the results received from the DBMS after the query is processed.
- The java.sql.ResultSet object consists of methods used to interact with data that is returned by the DBMS to the J2ME application.
The code snippet given below demostrates this:
ResultSet Results;
String FirstName;
String LastName;
String printrow;
boolean Records = Results.next();
if (!Records ) {
System.out.println( "No data returned");
return;
}
else
{
do {
FirstName = Results.getString (FirstName) ;
LastName = Results.getString (LastName) ;
printrow = FirstName + " " + LastName;
System.out.println(printrow);
} while ( Results.next() );
}
- The result returned by the DBMS is already assigned to the ResultSet object called Results.
- The first time that the next() method of the ResultSet is called, the ResultSet pointer is positioned at the first row in the ResultSet and returns a boolean value. If false, this indicates that no rows are present in the ResultSet.
- A true value returned by the next() method means at least one row of data is present in the ResultSet, which causes the code to enter the do...while loop.
- The getString() method of the ResultSet object is used to copy the value of a specified column in the current row of the ResultSet to a String object.
- The getString() method is passed the name of the column in the ResultSet whose content needs to be copied, and the getString() method returns the value from the specified column.
Terminate the Connection to the DBMS
- The connection to the DBMS is terminated by using the close() method of the Connection object once the J2ME application is finished accessing the DBMS.
- The close() method throws an exception if a problem is encountered when disengaging the DBMS.
- The following is an example of calling the close() method.
Db.close();
- Although closing the database connection automatically closes the ResultSet, it is better to close the ResultSet explicitly before closing the connection.