YOUR FEEDBACK
the usr wrote: So... how about your prediction that SCO would prevail? 11/20/2008 565 - FINAL...


2008 East
DIAMOND SPONSOR:
Data Direct
Frontiers in Data Access: The Coming Wave in Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
Intel
Virtualization – Path to Predictive Enterprise
Green Hills
IT Security in a Hostile World
JBoss / freedom oss
Practical SOA Approach
GOLD SPONSORS:
Software AG
The Art & Science of SOA: How Governance Enables Adoption
PlateSpin
Effective Planning for Virtual Infrastructure Growth
Fujitsu
Automated Business Process Discovery & Virtualization Service
Ceedo
Workspace Virtualization
Click For 2007 West
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TOP THREE LINKS YOU MUST CLICK ON


MM.MySQL and the MySQL Database System
MM.MySQL and the MySQL Database System

MySQL, and MM.MySQL, a JDBC driver for MySQL, are open-source tools that allow Java developers on Linux and other UNIX-like systems to develop full-featured applications. Both have similar histories - they started out as internal tools to fix a particular problem and grew to be much larger projects than the developers originally intended.

I started developing MM.MySQL in May of 1998, to learn socket and JDBC development and to fix some problems with the original JDBC driver for MySQL, GWE. GWE development had ceased at that point and some features I required in a JDBC driver for MySQL weren't present.

Since that time MM.MySQL has developed into a fairly capable Type IV (all Java) JDBC driver for MySQL. Depending on the JVM you're using, it supports the JDBC-1.2 or JDBC-2.0 API. It's been used with a majority of open-source Java tools like JBoss, Enhydra, and Tomcat, as well as Cocoon and Turbine. It's also supported by popular development environments such as Forté, JBuilder, IBM VisualAge for Java, and Macromedia UltraDev.

The MySQL Database System
MySQL is an open-source relational database management system developed by MySQL-AB. It's fast, reliable, and easy-to-use with a full client/server protocol. MySQL also has a very practical set of features developed in close cooperation with MySQL's users. Originally developed to handle large databases much faster than existing solutions, it's been successfully used in highly demanding production environments for several years. Though under constant development, MySQL offers a rich and useful set of features such as:

  • Full multithreaded operation using operating system-level threads and SMP if available and supported by the operating system
  • C, C++, Eiffel, Java, Perl, PHP, Python, and Tcl APIs

  • The server runs on many different platforms, including Windows, Linux, FreeBSD, and Solaris, as well as many common UNIX variants
  • Rich data-type support, including signed/unsigned integers 1, 2, 3, 4, and 8 bytes long, FLOAT, DOUBLE, CHAR, VARCHAR, TEXT, BLOB, DATE, TIME, DATETIME, TIMESTAMP, YEAR, SET, and ENUM types
  • Full support for SQL GROUP BY and ORDER BY clauses. Support for group functions (COUNT(), COUNT(DISTINCT), AVG(), STD(), SUM(), MAX(), and MIN())
  • Support for LEFT OUTER JOIN with ANSI SQL and ODBC syntax
  • Ability to mix tables from different databases in the same query (as of Version 3.22)
  • A privilege and password system that's flexible and secure and allows host-based verification; secure passwords because all password traffic is encrypted when you connect to a server
  • Handles large databases; installations of MySQL with some databases contain 50,000,000 records
  • Full support for several different character sets including ISO-8859-1 (Latin1), big5, ujis, and more
  • Current versions of MySQL support the creation of full text indexes on tables

Installing MySQL
If you're using a Red hat-based Linux distribution or your Linux distribution has support for Red hat Package Management (RPMs), it's easier to install MySQL from RPMs. The RPM files you want to download from www.mysql.com/ are MySQL-VERSION.i386.rpm and MySQL-client-VERSION.i386.rpm, where VERSION is the version number of the server you want to install. These RPMs contain the MySQL server and the MySQL command-line client utilities that allow you to administer and query your databases. Once you've downloaded the RPMs, issue the following command as root:

rpm -i MySQL-VERSION.i386.rpm MySQL-client-VERSION.i386.rpm

This will install the MySQL server and client and also place entries in /etc/rc.d/ to start the MySQL server at boot up.

If your system doesn't support installations via RPM, you'll need to download a binary distribution of MySQL. Their names are similar to mysql-VERSION-OS.tar.gz where VERSION is the version of the MySQL server and OS is the operating system the binary distribution has been compiled for. Once you've downloaded the binary distribution for your operating system, perform the following steps as root:

shell> groupadd mysql
shell> useradd -g mysql mysql
shell> cd /usr/local
shell> gunzip < /path/to/mysql-VERSION-OS.tar.gz | tar xvf -
shell> ln -s mysql-VERSION-OS mysql
shell> cd mysql
shell> scripts/mysql_install_db
shell> chown -R mysql /usr/local/mysql
shell> chgrp -R mysql /usr/local/mysql
shell> bin/safe_mysqld --user=mysql &

After installing MySQL using RPMs or from a binary distribution, you need to create logins for users in MySQL. If you have Perl and DBI installed on your system as many standard Linux distributions do, you can use the "mysql_setpermissions" script to create users and passwords for your MySQL server. If you don't have Perl or DBI installed, read Chapter 6 in the manual that comes with the MySQL server since the topic of MySQL security could take up an entire magazine article!

Installing MM.MySQL
The latest versions of MM.MySQL are always available at the MM.MySQL distribution Web site, www.worldserver.com/mm.mysql. There's usually a stable and a beta release.

If you're using a Java-2 JDK, the easiest installation method is to place the mysql-bin JAR file in the jre/lib/ext subdirectory that exists in your JDK's home directory. The JVM will then automatically load the driver when needed.

If you're using a Java-1 JDK or don't have access to the jre/lib/ext subdirectory of your Java-2 JDK, add mysql-bin.jar to your CLASSPATH environment variable. For example, in UNIX with CSH you'd do the following:

setenv CLASSPATH $CLASSPATH:/path/to/mysql-bin.jar
where /path/to/mysql-bin.jar is the full path to where you've placed the mysql-bin JAR file.

Often application servers don't refer to the CLASSPATH variable when determining where to load third-party libraries from. For example, with any J2EE-compliant application server you're deploying JSPs on, you can place the mysql-bin JAR file in the WEB-INF/lib sub-directory of your application. The application server will automatically load the driver from there. Other application servers require CLASSPATH to be set via their configuration files. Details for this configuration option can be found in the documentation for your application server.

Typical Usage Patterns
In most cases, to use a JDBC driver from your code you should follow these steps:

  1. Call Class. for Name ("org.gjt.mm. mysql.Driver").newInstance()
  2. Get a java.sql.Connection by calling DriverManager.getConnection("some jdbc URL")

    MM.MySQL's JDBC URL syntax is the following (items in square brackets are optional):

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

Where the hostname is the host that the database server is running on (defaults to localhost), the port is the TCP/IP port the server is listening on (only if it's listening on a nonstandard port), and dbname is the name of the database to connect to. The parameters (see Table 1) after the "?" allow you to pass more configuration information to the driver.

  1. Create a java.sql.Statement by calling Connection.createStatement() or Connection.prepareStatement() on the Connection instance you created in Step 2.
  2. Issue a query on the statement from Step 3 by calling executeQuery() for queries that select rows, or executeUpdate() for queries that insert/update/delete rows.
  3. Use Connection.prepareStatement() to create a PreparedStatement that allows you to issue parameterized queries that also take care of quoting special characters and dealing with binary data.

Using MySQL-Specific Functionality
MySQL has some features that can't be accessed from the methods provided in the standard JDBC API. To access them you need to cast the Statement or PreparedStatement object you're using to org.gjt.mm.mysql.Statement or org. gjt.mm.mysql.PreparedStatement respectively.

From either of these classes you can call the methods getLastInsertID() to get the value created for any AUTO_INCREMENT field and getLongUpdateCount() to get the larger update count that MySQL can produce as a long. Listing 1 shows how to do this.

The JDBC API version 3, under development by JavaSoft, will have a portable way of retrieving AUTO_INCREMENT values; MM.MySQL will support that method when it becomes available.

Storing/Retrieving Serialized Java Objects
Because MySQL has support for BLOBs, it's relatively easy to store serialized Java objects in the database. The easiest way to do this is to use java.sql.PreparedStatement's setObject() method to get the objects into the database. If you're using a version of MM.MySQL earlier than 2.0.3, you'll need to use org.gjt. mm.mysql.Util's readObject() method to read serialized objects from the database. If you have MM.MySQL version 2.0.3 or newer, use java.sql.ResultSet's getObject() method to retrieve serialized objects from the database. Listing 2 provides an example of how to do this, assuming you have a table named "serObject" in your database with a column named "user" and one named "cert" (e.g., to store a cryptographic certificate).

As you've seen, MM.MySQL makes it extremely easy to store and retrieve Java objects. Because MySQL requires no special syntax to work with BLOBs, it's easy to manipulate data that contains them, which makes it easier to write queries that deal with BLOBs.

Conclusion
I hope you've seen that MM.MySQL and MySQL are simple to use, yet robust and powerful. They're both flexible tools to use in developing Web-based applications, but simple enough for beginning Java developers to learn.

As MM.MySQL is an open-source product, I'm always looking for suggestions for further development and assistance with development, so please contact me at mmatthew@thematthews.org for more information.

About Mark Matthews
Mark Matthews is a senior consultant for marchFIRST where he helps clients develop solutions using the Java platform.

LATEST JAVA STORIES & POSTS
Transmeta, the uppity microprocessor wannabe that pushed Intel to create low-power chips before Intel crushed it, is getting bought by Novafora Inc for $255.6 million in cash, roughly what Transmeta has in the bank thanks largely to an IP licensing deal with Intel. Novafora is a ...
JavaScript is pretty much everywhere you look these days, reaching far beyond your desktop browser. Adobe AIR lets you use JavaScript to create desktop installed HTML and AJAX apps. Apple uses it in its gadgets and in the iPhone's browser. And Nokia recently announced support for...
The Java Community Process (JCP) Program Management Office has announced the final results of the 2008 JCP Executive Committees (EC) elections. After two ballot rounds – for ratified and elected seats – the winners are Ericsson, SpringSource, SAP, Intel, and Werner Keil for t...
If you think your network is safe from the new strains of content security threats, think again. Today’s cybercriminals use sophisticated attacks that multiply quickly and thwart traditional defenses, rendering conventional security ineffective and unmanageable. To protect your...
Tidal Software has announced Intersperse 8.0, a product that monitors J2EE and .NET applications and their transaction component performance to produce meaningful metrics for managing applications and high-level business processes. The product leverages a combination of lightwei...
ILOG has announced ILOG JViews 8.5, the latest version of ILOG’s Java-based visualization suite, with new features that enhance the creation of Rich Internet Applications as well as desktop applications. ILOG JViews 8.5 adds support for the Eclipse platform including the new IL...
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


SYS-CON FEATURED WHITEPAPERS

SPONSORED BY INFRAGISTICS
In every field of design one of the first things students do is learn from the work of others. They ...
There are many forces that influence technological evolution. After a decade of building enterprise ...
2008 is going to be an important year for Rich Internet Applications. Most organizations are deliver...
The OpenAjax Alliance is developing an Ajax industry wishlist for future browsers, using a dedicated...
Infragistics announced the availability of two Community Technology Preview (CTP) User Interface (UI...
The YUI development team has released version 2.5.2; you can download the new release from SourceFor...
ADS BY GOOGLE