Chapter 3. Programming Information

Table of Contents
Establishing a Connection
Working with Statements and PreparedStatements
Using MySQL specific functionality

This chapter contains information about using MM.MySQL in your software. It is assumed that the driver has been installed as outlined earlier, and that you understand how to use Java development tools as well as the JDBC API.

This chapter will not cover the JDBC API in detail. It stands to illustrate how you might use some of the simpler concepts in JDBC, along with the information you need to know to use JDBC with MySQL.

To learn more about Java or the JDBC API in detail, please consult the following resources:

Establishing a Connection

The first thing that needs to be done in order to work with JDBC is to obtain a Connection object that represents the network connection to the database that contains the information you want to work with.

With JDBC, the DriverManager manages the establishment of Connections. The DriverManager needs to be told which JDBC drivers it should try to make Connections with. The easiest way to do this is to use Class.forName() on the Class that implements the java.sql.Driver interface. With MM.MySQL, the name of this Class is org.gjt.mm.mysql.Driver.

Example 3-1. Registering the Driver with the DriverManager

         import java.sql.*;

         // Notice, do not import org.gjt.mm.mysql.*
         // or you will have problems!

         public class LoadDriver
         {
             public static void main(String[] Args) 
             {
                try {

                    // The newInstance() call is a work around for some
                    // broken Java implementations

                    Class.forName("org.gjt.mm.mysql.Driver").newInstance(); 

                }
                catch (Exception E) {
                    System.err.println("Unable to load driver.");
                    E.printStackTrace();
                }

                .....
         

Once the driver is registered, a Connection can be established. Obtaining a Connection requires a URL for the database. This URL is constructed using the following syntax, where items contained in sqaure brackets are optional:

             jdbc:mysql://[hostname][:port]/dbname[?param1=value1][=value2]...
         

The following parameters can be specified after the "?" in the URL:

Table 3-1. URL Parameters

NameUseDefault Value
userThe user to connect asnone
passwordThe password to use when connectingnone
autoReconnectshould the driver attempt to re-connect if the connection dies? (true/false)false
maxReconnectsif autoReconnect is enabled, how many times should the driver attemt to reconnect?3
initialTimeoutif autoReconnect is enabled, the initial time to wait between re-connects (seconds)2
maxRowsThe maximum number of rows to return (0 means return all rows)0
useUnicodeshould the driver use Unicode character encodings when handling strings? (true/false)false
characterEncodingif useUnicode is true, what character encoding should the driver use when dealing with strings?none

Once the URL has been specified, it is passed to the DriverManager.getConnection() method to obtain a Connection Object:

Example 3-2. Obtaining a Connection from the DriverManager

            ...

            try {
                Connection C = DriverManager.getConnection(
                  "jdbc:mysql://localhost/test?user=monty=greatsqldb");
                
                // Do something with the Connection
                
                ....
            }
            catch (SQLException E) {
              System.out.println("SQLException: " + E.getMessage());
              System.out.println("SQLState:     " + E.getSQLState());
              System.out.println("VendorError:  " + E.getErrorCode());
            }
          

Once a Connection is established, it can be used to create Statements and PreparedStatements, as well as retrieve metadata about the database. These functions are explained in the next section.