This version: 0.1
Date: 8 May 2003

HowTo Use MySQL Database System with Jena2

What is MySQL

MySQL is an open-source SQL database system available without fee under GPL (Gnu General Public License). It combines good performance with a wide feature set and comes in a variety of configurations to support difference application requirements. See http://www.mysql.com/ for more information. Jena2 has been tested with the MySQL Standard binary release. Jena tables are created using InnoDB tables which provide ACID transaction support. Modifications to Jena database models are immediately written to the database. If the application has an open transaction (see model.begin()), the changes are committed (u.e. visible to other Jena applications) when the application does a commit. Otherwise, the changes areimmediately committed to the database (i.e., each modification is its own transaction). Jena communicates with MySQL through a JDBC driver.

Download and Installation of MySQL

MySQL software and the JDBC driver may be downloaded from http://www.mysql.com/downloads/mysql-4.0.html. The JDBC driver is available from the same web site from http://www.mysql.com/downloads/api-jdbc-stable.html. If these links are not found, go to the MySQL home page and search for downloads for the latest version of the server and JDBC driver. Jena2 has been tested with the binary distributions of the MySQL server and driver on Windows 2000 and Linux RedHat 7.2 as listed below. The download package is approximately 20 MB.

    Server:  MySQL 4.0.12           Driver:  mysql-connector-java-3.0.7-stable.jar      

Depending on the platform, there may be a few post-installation steps required to properly configure MySQL. See the MySQL documentation for details (Post-Installation Setup and Testing in Chapter 2). The MySQL documentation should be included with the distribution.

Connecting Your Jena Program to MySQL

Jena supports both memory models and database models. In general, a Jena program may use both types of models identically. However, there are some differences in how the models are created. Creating a memory model can be done with a single Jena call. Creating a database model, or opening a previously created one, requires several steps as as follows.

  1. Load the JDBC driver. This enables the Jena program to communicate with the database instance.
  2. Create a database connection. This creates a Java object for a database connection.
  3. Create or open a model on the connection. This connects to the database and creates an empty model or opens a previously created model.

These steps are illustrated in the following Java code.

// Load the Driver
String className = "com.mysql.jdbc.Driver"      // path of driver class
Class.forName (className);                      // load driver
 
String DB_URL = "jdbc:mysql://localhost/test";  // URL of database server
String DB_USER = "test";                        // database user id
String DB_PASSWD = "";                          // database password
String DB = "MySQL";                            // database type
 
// Create database connection
IDBConnection conn = new DBConnection ( URL, DB_USER, DB_PASSWD, DB );
// Create a model in the database
ModelRDB m = ModelRDB.createModel ( conn );
 
< your Jena code to work with the model goes here >
 
// Remove the model. NOTE: this deletes the model from the database.
// Subsequent attempts to open this model will fail.
m.remove();   // NOTE: remove deletes the model from the database
 
// Close the database connection                                                                                conn.close();                                                                                               
 
// Alternative to creating an unnamed model ...
// Create a named model
ModelRDB m = ModelRDB.createModel(conn, "myName");
 
...  later, or in another Jena application ...
 
// Open a named model.
ModelRDB m = ModelRDB.open(conn, "myName");

 

Performance and Usage Notes

Jena stores all models in a single database using whatever database (name) specified on the database connection. See the Jena Database Release Notes for details on the physical layout.

MySQL Server Status

After installation, you may want to check that the MySQL database server is up and running with the following command..

mysqladmin version status proc

On Windows, the MySQL server should be automatically started as a service. On Linux, the server may need to be started manually. See the MySQL documentation (Starting the MySQL Server in Chapter 2) for details.

MySQL Utility Commands

Some Jena users may wish to occasionally access the MySQL database directly to determine if changes have taken effect. Some useful MySQL commands are listed here (replace the names in italics by names for your database instance). To use them, start the MySQL command line interface: mysql [database]

List All Databases Available: show databases;

Use a Specific Database: use databaseName;

List All Tables in the Database: show tables;

List Summary Information about Tables:  show table status; (includes the row count)

List All Rows in a Table: select * from tableName;