home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeManaging and Using MySQLSearch this book

Chapter 13. Java

Java is one of the simplest languages in which you can write MySQL applications. Its database access API, Java Database Connectivity (JDBC), is one of the more mature database-independent APIs for database access in common use. Most of what we cover in this chapter can be applied to Oracle, Sybase, MS SQL Server, mSQL, and any other database engine, as well as MySQL. In fact, almost none of the MySQL-specific information in this chapter has anything to do with coding. Instead, the "proprietary" information relates only to downloading MySQL support for JDBC and configuring the runtime environment. Everything else is largely independent of MySQL.

In this chapter, we assume you have a basic understanding of the Java programming language and Java concepts. If you do not already have this background, we strongly recommend taking a look at Learning Java, by Pat Niemeyer and Jonathan Knudsen (O'Reilly). For more details on how to build the sort of three-tier database applications we discussed in Chapter 8, take a look at Database Programming with JDBC and Java, by George Reese (O'Reilly).

13.1. The JDBC API

Like all Java APIs, JDBC is a set of classes and interfaces that work together to support a specific set of functionality. In the case of JDBC, this functionality is database access. The classes and interfaces that make up the JDBC API are thus abstractions from concepts common to database access for any kind of database. A Connection, for example, is a Java interface representing a database connection. Similarly, a ResultSet represents a result set of data returned from an SQL SELECT. Java combines the classes that form the JDBC API in the java.sql package, which Sun introduced in JDK 1.1.

The underlying details of database access naturally differ from vendor to vendor. JDBC does not actually deal with those details. Most of the classes in the java.sql package are in fact interfaces with no implementation details. Individual database vendors provide implementations of these interfaces in the form of something called a JDBC driver. As a database programmer, however, you need to know only a few details about the driver you are using—the rest you manage via the JDBC interfaces.

The first database-dependent thing you need to know is what drivers exist for your database. Different people provide different JDBC implementations for a variety of databases. As a database programmer, you should select a JDBC implementation that will provide the greatest stability and performance for your application. Though it may seem counterintuitive, JDBC implementations provided by the database vendors are generally at the bottom of the pack when it comes to stability and flexibility. As an open source project, however, MySQL relies on drivers provided by other developers in the community.

Sun has created four classifications of JDBC drivers based on their architectures. Each JDBC driver classification represents a trade-off between performance and flexibility.

Type 1
Type 1 drivers use a bridging technology to access a database. The JDBC-ODBC bridge that comes with JDK 1.2 is the most common example of this kind of driver. It provides a gateway to the ODBC API. Implementations of the ODBC API, in turn, perform the actual database access. Though useful for learning JDBC and quick testing, bridging solutions are rarely appropriate for production environments.

Type 2
Type 2 drivers are native API drivers. "Native API" means that the driver contains Java code that calls native C or C++ methods provided by the database vendor. In the context of MySQL, a Type 2 driver is one that uses MySQL's C API under the hood to talk to MySQL on behalf of your application. Type 2 drivers generally provide the best performance, but they require the installation of native libraries on clients that need to access the database. Applications using Type 2 drivers have a limited degree of portability.

Type 3
Type 3 drivers provide a client with a pure Java implementation of the JDBC API in which the driver uses a network protocol to talk to middleware on the server. This middleware, in turn, performs the actual database access. The middleware may or may not use JDBC for its database access. The Type 3 architecture is actually more of a benefit to driver vendors than application architects since it enables the vendor to write a single implementation and claim support for any database that has a JDBC driver. Unfortunately, it has weak performance and unpredictable stability.

Type 4
Using network protocols built into the database engine, Type 4 drivers talk directly to the database using Java sockets. This is a pure Java solution. Because these network protocols are almost never documented, most Type 4 drivers come from the database vendors. The open source nature of MySQL, however, has enabled several independent developers to write different Type 4 MySQL drivers.

Practically speaking, Type 2 and Type 4 drivers are the only viable choices for a production application. At an abstract level, the choice between Type 2 and Type 4 comes down to a single issue: is platform independence critical? By platform independence, we mean that the application can be bundled up into a single jar and run on any platform. Type 2 drivers have a hard time with platform independence since you need to package platform-specific libraries with the application. If the database access API has not been ported to a client platform, your application will not run on the platform. On the other hand, Type 2 drivers tend to perform better than Type 4 drivers.

Knowing the driver type provides only a starting point for making a decision about which JDBC driver to use in your application. The decision really comes down to knowing the drivers that exist for your database of choice and how they compare to each other. Table 13-1 lists the JDBC drivers available for MySQL. Of course, you can use any ODBC bridge to talk to MySQL as well—but we do not recommend it under any circumstance for MySQL developers.

Table 13-1. JDBC drivers for MySQL

Driver name[28]

OSI[29] license

JDBC version

Home page

mm (GNU)

LGPL

1.x and 2.x

http://mmmysql.sourceforge.net/

twz

None

1.x

http://www.voicenet.com/~zellert/tjFM/

Caucho

QPL

2.x

http://www.caucho.com/projects/jdbc-mysql/index.xtp

[28] These are all Type 4 drivers.
[29] This stands for Open Source Initiative (http://www.opensource.org). For drivers released under an OSI-approved license, the specific license is referenced.

Of the three MySQL JDBC drivers, twz sees the least amount of development and thus does not likely serve the interests of most programmers these days. The GNU driver (also known as mm MySQL), on the other hand, has been under constant development and is the most mature of the three JDBC drivers. Not to be outdone, Caucho claims significant performance benefits over the GNU driver.

13.1.2. Connecting to MySQL

JDBC represents a connection to a database through the Connection interface. Thus, connecting to MySQL requires you to get an instance of the Connection interface from your JDBC driver. JDBC supports two ways of getting access to a database connection:

  • Through a JDBC data source

  • Using the JDBC driver manager

The data source method is preferred for connecting to a database. Data sources come from the Optional Package, so support for them is still spotty. No matter what environment you are in, you can rely on driver manager connectivity.

13.1.2.1. Data source connectivity

Data source connectivity is very simple. In fact, the following code makes a connection to any database; it is not specific to MySQL:

Context ctx = new InitialContext( );
DataSource ds = (DataSource)ctx.lookup("jdbc/myds");
Connection conn = ds.getConnection("userid", "password");

The first line in this example actually comes from the Java Naming and Directory Interface (JNDI) API. JNDI is an API that provides access to naming and directory services.[30] Naming and directory services are specialized data stores that enable you to associate related data under a familiar name. In a Windows environment, for example, a network printer is stored in Microsoft ActiveDirectory under a name. To print to the networked color printer, a user does not need to know all the technical details about the printer. Those details are stored in the directory. The user simply needs to know the name of the printer. The directory, in other words, stores all the details about the printer in a directory where an application can access these details by name.

[30]A full discussion of JNDI is way beyond the scope of this chapter. At the very least, you need a JNDI service provider (analogous to a JDBC driver), and you must set some environment variables to support that service provider. You also need a directory service to talk to. If you do not have access to a directory service, you can always practice using the filesystem service provider available on the JNDI home page at http://java.sun.com/products/jndi or use the driver manager approach.

Though data source connectivity does not require that a data source be stored in a directory, you will find that a directory is the most common place you will want to store data source configuration details. As a result, you can simply ask the directory for the data source by name. In the above example, the name of the data source is jdbc/myds. JNDI enables your application to grab the data source from the directory by its name without worrying about all the configuration details.

Though this sounds simple enough, you are probably wondering how the data source got in the directory in the first place. Someone had to put it there. Programmatically, putting the data source in the directory can be as simple as the following code:

SomeDataSourceClass ds = new SomeDataSourceClass( );
Context ctx = new InitialContext( );

// configure the DS by setting configuration attributes
ctx.bind("jdbc/myds", ds);

We have two bits of "magic" in this code. The first bit of magic is the SomeDataSourceClass class. In short, it is an implementation of the javax.sql.DataSource interface. In some cases, this implementation may come from the JDBC vendor—but not always. In fact, none of the MySQL drivers currently ship with a DataSource implementation. If you are using an application server such as Orion or WebLogic, it will provide a DataSource implementation for you that will work with MySQL.

Configuring your data source depends on the properties demanded by the data source implementation class. In most cases, a data source implementation will want to know the JDBC URL and name of the java.sql.Driver interface implementation for the driver. We will cover these two things in the section on driver manager connectivity.

Though we have been vague about configuring a JDBC data source programmatically, do not despair. You should never have to configure a JDBC data source programmatically. The vendor that provides your data source implementation should provide you with a configuration tool capable of publishing the configuration for a data source to a directory. All application servers come with such a tool. A tool of this sort will prompt you for the values to enter a new data source in a directory, then allow you to save that configuration to the directory. Your application can then access the data source by name, as shown earlier in the chapter.

13.1.2.2. Driver manager connectivity

One of the few implementation classes in the java.sql package is the DriverManager class. It maintains a list of implementations of the JDBC java.sql.Driver class and provides you with database connections based on the JDBC URLs you provide. A JDBC URL is in the form of jdbc:protocol:subprotocol. It tells a DriverManager which database engine you wish to connect to and provides the DriverManager with enough information to make a connection.

TIP: JDBC uses the word "driver" in multiple contexts. When lowercase, a JDBC driver is the collection of classes that together implement all the JDBC interfaces and provide an application with access to at least one database. When uppercase, the Driver is the class that implements java.sql.Driver. Finally, JDBC provides a DriverManager that can be used to keep track of all the different Driver implementations.

The protocol part of the URL refers to a given JDBC driver. The protocol for the Caucho MySQL driver, for example, is mysql-caucho, while the GNU driver uses mysql. The subprotocol provides the implementation-specific connection data. Every MySQL driver requires a hostname and database name to make a connection. It also requires a port if your database engine is not running on the default port. Table 13-2 shows the configuration information for the MySQL JDBC drivers.

Table 13-2. Configuration information for MySQL JDBC drivers

Driver

Implementation

URL

Caucho

com.caucho.jdbc.mysql.Driver

jdbc:mysql-caucho://HOST[:PORT]/DB

GNU

org.gjt.mm.mysql.Driver

jdbc:mysql://[HOST][:PORT]/DB[?PROP1=VAL1][&PROP2=VAL2]...

twz

twz1.jdbc.mysql.jdbcMysqlDriver

jdbc:z1MySQL://HOST[:PORT]/DB[?PROP1=VAL1][&PROP2=VAL2]...

As you can see, the URLs for the GNU driver and twz driver are very different from that of the Caucho driver. As a general rule, the format of the Caucho driver is preferred, because it allows you to specify properties separately.

Your first task is to register the driver implementation with the JDBC DriverManager. There are two key ways to register a driver:

For portability's sake, we recommend that you put all configuration information in some sort of configuration file, such as a properties file, then load the configuration data from that configuration file. By taking this approach, your application will not rely on MySQL or the JDBC driver you are using. You can simply change the values in the configuration file to move from the GNU driver to Caucho or from MySQL to Oracle.

Once you have registered your driver, you can ask the DriverManager for a Connection by calling the getConnection( ) method in the driver with the information identifying the desired connection. This information minimally includes a JDBC URL, user ID, and password. You may optionally include a set of parameters:

Connection conn = DriverManager.getConnection("jdbc:mysql-caucho://carthage/Web", 
"someuser", "somepass");

This code returns a connection associated with the database Web on the MySQL server on the machine carthage using the Caucho driver under the user ID someuser and authenticated with somepass. Though the Caucho driver has the simplest URL, connecting with the other drivers is not much more difficult. They just ask that you specify connection properties such as the user ID and password as part of the JDBC URL. Table 13-3 lists the URL properties for the GNU driver, and Table 13-4 lists them for the twz driver.

Table 13-3. URL properties for the GNU (mm) JDBC driver

Name

Default

Description

autoReconnect

false

Causes the driver to attempt a reconnect when the connection dies.

characterEncoding

None

The Unicode encoding to use when Unicode is the character set.

initialTimeout

2

The initial time between reconnects in seconds when autoReconnect is set.

maxReconnects

3

The maximum number of times the driver should attempt a reconnect.

maxRows

0

The maximum number of rows to return for queries. Zero means return all rows.

password

None

The password to use in connecting to MySQL.

useUnicode

false

Specifies Unicode as the character set to be used for the connection.

user

None

The user to use for the MySQL connection.

Table 13-4. URL properties for the twz JDBC driver

Name

Default

Description

autoReX

true

Manages automatic reconnect for data update statements.

cacheMode

memory

Dictates where query results are cached.

cachePath

.

The directory to which result sets are cached if cacheMode is set to disk.

connectionTimeout

120

The amount of time, in seconds, that a thread will wait for action by a connection before throwing an exception.

db

mysql

The MySQL database to which the driver is connected.

dbmdDB

<connection>

The MySQL database to use for database metadata operations.

dbmdMaxRows

66536

The maximum number of rows returned by a database metadataoperation.

dbmdPassword

<connection>

The password to use for database metadata operations.

dbmdUser

<connection>

The user ID to use for database metadata operations.

dbmdXcept

false

Causes exceptions to be thrown on unsupported database metadata operations instead of the JDBC-compliant behavior of returning an empty result.

debugFile

None

Enables debugging to the specified file.

debugRead

false

When debugging is enabled, data read from MySQL is dumped to the debug file. This will severely degrade the performance of the driver.

debugWrite

false

When debugging is enabled, data written to MySQL is dumped to the debug file. This will severely degrade the performance of the driver.

host

localhost

The host machine on which MySQL is running.

maxField

65535

The maximum field size for data returned by MySQL. Any extra data is silently truncated.

maxRows

Integer.MAX_VALUE

The maximum number of rows that can be returned by a MySQL query.

moreProperties

None

Tells the driver to look for more properties in the named file.

multipleQuery

true

Will force the caching of the result set, allowing multiple queries to be open at once.

password

None

The password used to connect to MySQL.

port

3306

The port on which MySQL is listening.

socketTimeout

None

The time in seconds that a socket connection will block before throwing an exception.

user

None

The user connected to MySQL.

RSLock

false

Enables locking of result sets for a statement for use in multiple threads.

As a result, connections for the GNU driver commonly look like:

Connection conn = DriverManager.getConnection("jdbc:mysql://carthage/
Web?user=someuser&password=somepass");

or for twz:

Connection conn = 
  DriverManager.getConnection("jdbc:z1MySQL://carthage
  /Web?user=someuser&password="somepass");

Instead of passing the basic connection properties of user and password as a second and third argument to getConnection( ), GNU and twz pass them as part of the URL. In fact, you can pass any of the properties as part of the URL. JDBC, however, has a standard mechanism for passing driver-specific connection properties to getConnect():

Properties p = new Properties( );
Connection conn;

p.put("user", "someuser");
p.put("password", "somepass");
p.put("useUnicode", "true");
p.put("characterEncoding", "UTF-8");
conn = DriverManager.getConnection(url, p);

Unfortunately, the way in which MySQL supports these optional properties is a bit inconsistent. So it is best to go with the preferred manner for your driver, however unwieldy it makes the URLs.

Example 13-1 shows how to make a connection to MySQL using the GNU driver.

Example 13-1. A complete sample of making a JDBC connection

import java.sql.*;

public class Connect {
    public static void main(String argv[]) {
        Connection con = null;

        try {
            // here is the JDBC URL for this database
            String url = "jdbc:mysql://athens.imaginary.com/
                        Web?user=someuser&password=somepass";
            // more on what the Statement and ResultSet classes do later
            Statement stmt;
            ResultSet rs; 

            // either pass this as a property, i.e.
            // -Djdbc.drivers=org.gjt.mm.mysql.Driver
            // or load it here as we are doing in this example
            Class.forName("org.gjt.mm.mysql.Driver");
            // here is where the connection is made   
            con = DriverManager.getConnection(url); 
        }
        catch( SQLException e ) {
            e.printStackTrace( );
        }
        finally {
            if( con != null ) {
                try { con.close( ); }
                catch( Exception e ) { }
            }
        }
    }
}

The line con = DriverManager.getConnection(url) makes the database connection in this example. In this case, the JDBC URL and Driver implementation class names are actually hardcoded into this application. The only reason this is acceptable is because this application is an example driver. As we mentioned earlier, you want to get this information from a properties file or the command line in real applications.

13.1.3. Maintaining Portability Using Properties Files

Though our focus is on MySQL, it is good Java programming practice to make your applications completely portable. To most people, portability means that you do not write code that will run on only one platform. In the Java world, however, the word "portable" is a much stronger term. It means no hardware resource dependencies, and that means no database dependencies.

We discussed how the JDBC URL and Driver name are implementation dependent, but we did not discuss the details of how to avoid hardcoding them. Because both are simple strings, you can pass them on the command line as runtime arguments or as parameters to applets. While that solution works, it is hardly elegant since it requires command-line users to remember long command lines. A similar solution might be to prompt the user for this information; but again, you are requiring that the user remember a JDBC URL and a Java class name each time he runs an application.

13.1.3.1. Properties files

A more elegant solution than either of those mentioned would be to use a properties file. Properties files are supported by the java.util.ResourceBundle and its subclasses to enable an application to extract runtime-specific information from a text file. For a JDBC application, you can stick the URL and Driver name in the properties file, leaving the details of the connectivity up to an application administrator. Example 13-2 shows a properties file that provides connection information.

Example 13-3 shows the portable Connect class.

Example 13-3. Using a properties file to maintain portability

import java.sql.*;
import java.util.*;

public class Connect {
    public static void main(String argv[]) {
        Connection con = null;
        ResourceBundle bundle = ResourceBundle.getBundle("SelectResource");

        try {
            String url = bundle.getString("URL");
            Statement stmt;
            ResultSet rs; 

             Class.forName(bundle.getString("Driver"));
            // here is where the connection is made   
            con = DriverManager.getConnection(url); 
        }
        catch( SQLException e ) {
            e.printStackTrace( );
        }
        finally {
            if( con != null ) {
                try { con.close( ); }
                catch( Exception e ) { }
            }
        }
    }
}

We have gotten rid of anything specific to MySQL or the GNU driver in the sample connection code. One important issue still faces portable JDBC developers. JDBC requires that all drivers support the SQL2 entry level standard. This is an ANSI standard for minimum SQL support. As long as you use SQL2 entry level SQL in your JDBC calls, your application will be 100% portable to other database engines. Fortunately, MySQL is SQL2 entry level, even though it does not support many of the advanced SQL2 features.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.