Developer Documentation

Since there are many good tutorials out there for using JDBC and they should work with any JDBC driver, I will point novice JDBC developers to them rather than re-invent the wheel.

The rest of this document explains some of the driver-specific things you need to know in order to use the mm JDBC drivers.

To use the mm JDBC drivers you should have a pretty firm grasp of the following subjects:

  • The Java programming language and core class libraries
  • SQL-92 database query language
  • Relational database concepts and how they relate to the MySQL server
  • The structure of the JDBC classes as provided by Javasoft
Step by Step Guide

There are three steps you need to follow when using any JDBC driver to connect to a database.

First you need to register the driver. There are three ways to do this:

	      /* 
	       * Register the driver using Class.forName() 
               */
              
	      Class.forName("org.gjt.mm.mysql.Driver") 



              /* 
               * Register the driver, avoiding a bug in some JDK1.1 
               * (like Netscape) implementations
               */

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



              /*
               * Register the driver via the system properties variable 
               * "jdbc.drivers"
               */ 
              
              Properties P = System.getProperties(); 
              P.put("jdbc.drivers", "org.gjt.mm.mysql.Driver"); 
              System.setProperties(P);

Second, you need to ask the DriverManager for a connection to the database:

              Connection Conn = null; 
                          
              try { 
                  Conn = DriverManager.getConnection("jdbc:mysql://hostname:port/db?user=name;password=secret"); 
              } 
              catch (Exception E) { 
                  System.out.println("Connection failed : " + E.getMessage()); 
                              throw E; 
              }

The URL string for the getConnection() method has the following format:

              jdbc:mysql://hostname[:port]/database[?extra_param1=value][;extra_param2=value]

For now, the parameters you can pass in the URL string are user and password.

For the later versions of this driver, the port defaults to 3306.

After you have a connection you can create a Statement and execute simple queries:

              Statement Stmt = Conn.createStatement();
              Stmt.executeUpdate("INSERT INTO some_table (col1, col2) VALUES (1,2)");

For more complicated examples, see the tutorials listed above.

Driver Notes

Retrieving IDs of Updated Rows with AUTO_INCREMENT Fields

To retrieve update ids from inserted rows, create a Statement, and then cast it to an org.gjt.mm.mysql.MySQLStatement.

When you do an executeUpdate() on the MySQLStatement instead of the original Statement, the updated rows will be returned in the SQLWarning chain for that statement.

The data will be in the format "LAST_INSERTED_ID = 'some number', COMMAND = 'your sql'".


Questions and comments to mmatthew@worldserver.com

$Id: README.html,v 1.1 1998/08/24 19:49:21 mmatthew Exp $