Using JDBC with MySQL, Getting Started
Java Programming Notes # 662
The purpose of this tutorial is to get you beyond the initial hurdles involved in:
- Downloading and installing a MySQL database server.
- Preparing that database for use with JDBC.
- Writing and testing your first JDBC programs to administer the database and to manipulate the data stored in the MySQL database.
Use the navigation links below to jump to the section that best addresses your information needs, or read to entire article to master the use of MySQL with JDBC.
- Installation and Preparation of MySQL
- Discussion and Sample Code for JDBC Programs
- Run the Programs
- Complete Program Listings
What is JDBC?
JDBC technology is an API (included in both J2SE and J2EE) that provides cross-DBMS connectivity to a wide range of SQL databases and access to other tabular data sources, such as spreadsheets or flat files.
What is MySQL?
The MySQL database server is probably the world's most popular open source database software, with more than five million active installations as of September 2004.
The database server software from MySQL is available under a "dual licensing" model. Under this model, users may choose to use MySQL products under the free software/open source GNU General Public License (commonly known as the "GPL") or under a commercial license.
A powerful combination
Simply stated, JDBC makes it possible to write platform independent Java programs that can be used to manipulate the data in a wide range of SQL databases without the requirement to modify and/or recompile the Java programs when moving from platform to platform or from DBMS to DBMS.
MySQL is available for a wide variety of platforms.
Since both JDBC and MySQL are freely available for many purposes, the combination of JDBC and MySQL is a powerful combination that should be of interest for a wide variety of applications.
You may find it useful to open another copy of this lesson in a separate browser window. That will make it easier for you to scroll back and forth among the different listings and figures while you are reading about them.
I recommend that you also study the other lessons in my extensive collection of online Java tutorials. You will find those lessons published at the Developer.com Java section. However, as of the date of this writing, Gamelan doesn't maintain a consolidated index of my Java tutorial lessons, and sometimes they are difficult to locate there. You will find a consolidated index at www.DickBaldwin.com.
Download, install, and prepare the database server
It is often possible to use the same Java program to manipulate the data in a wide variety of SQL databases without a requirement to modify and/or recompile the Java program.
However, the installation and preparation procedures for different SQL databases vary widely. A large part of the battle in using JDBC with a particular database is getting the database installed and properly prepared for use with JDBC.
In this lesson, I will show you how to download, install, and prepare a MySQL database as a localhost server on a Windows platform for use with JDBC. Then I will show you how to write three simple JDBC programs to administer the database server and to manipulate data stored on the database server after it is installed.
(I will assume that you already have Java SDK v1.4.2 or later, which includes JDBC, installed on your computer.)
URLs and version numbers for downloading
I will provide specific URLs and version numbers for downloading MySQL software and documentation as of September 2004. The version numbers will certainly change over time as new versions of the software are released. Hopefully, the folks at MySQL will preserve the integrity of the URLs.
At least one URL, http://www.mysql.com/ should remain constant over time. If the other links to MySQL in this lesson become broken with time, you should revert to the main MySQL URL given above and begin your search for the software and documentation from that point.
If you are a newcomer to the installation of database server software and the preparation of that software for use with JDBC, the information that I will provide in this lesson should save you several days of effort in pouring through documentation trying to figure out how to download, install, and tie everything together.
Even if you are experienced in these matters, this information should save you several hours of effort.