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. When using InnoDB tables, Jena provides full ACID transaction support.

Download and Installation of MySQL

MySQL software and the JDBC driver may be downloaded from http://www.mysql.com/downloads/. The JDBC driver is available from the same Web page (see MySQL Connector/J). (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.)

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.

Persistent models are created in the same way for any database system:

  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 a ModelMaker for the database
  4. Create a Model for existing or new data.

These steps are illustrated in the following Java code.

String className = "com.mysql.jdbc.Driver";         // path of driver class
Class.forName (className);                          // Load the Driver
String DB_URL =     "jdbc:mysql://localhost/test";  // URL of database 
String DB_USER =   "????";                          // database user id
String DB_PASSWD = "????";                          // database password
String DB =        "MySQL";                         // database type

// Create database connection
IDBConnection conn = new DBConnection ( DB_URL, DB_USER, DB_PASSWD, DB );
ModelMaker maker = ModelFactory.createModelRDBMaker(conn) ;

// create or open the default model
Model model = maker.createDefaultModel();

// Close the database connection
conn.close();

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 and character sets

To handle the full range of characters, you can use the UTF-8 character set: in the server configuration file:

default-character-set=utf8

Note: the character set name must be exactly "utf8".  It must not be "utf-8" (an alias name) - the server will not start if the character set name is wrong.

Alternatively, if the server needs to run with some other character set, create the database as a UTF-8 database and force the JDBC driver to use Unicode regards of negotiation with the server:

Database creation:

create database jenatest character set utf8 ;

JDBC URL arguments:

?useUnicode=yes&characterEncoding=utf8

MySQL Driver issues

The situations where the 5.0.0-beta will deadlock are rare.  The 5.0.0-beta driver can be used by setting "Query Only Asserted" (see options).