See which of your colleagues or former colleagues are already on Java Link: Check out the Contact Finder
News »Browse Articles » Using the MySQL Connector/J JDBC Driver with the Java SE Platform
+1
Vote Vote

Using the MySQL Connector/J JDBC Driver with the Java SE Platform

Views 5 Views    Comments 0 Comments    Share Share    Posted 03-05-2009  
This tutorial will show you the essential steps to install and configure the Connector/J JDBC driver, with simple examples to connect to a MySQL database. Because the focus is on the database driver and the database connectivity from the Java programming language, this document assumes that some kind of MySQL database is already up and accessible from the client machine.

Application developers who are new to the Java programming language, but not to MySQL databases, are the target audience of this article.
Contents

* Access MySQL With Java/JDBC
* MySQL Connector/J JDBC Driver
* Install MySQL Connector/J
* Test the MySQL Database Connectivity Using the Connector/J JDBC Driver
* For More Information

The code samples in this tutorial show you how to retrieve the data from the City table in the MySQL test database. The table structure and the data from the City table are shown here by using the mysql client. The MySQL server is running on default port 3306.

bash# mysql -u root -p
Enter password: admin
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.24-rc-standard Source distribution

Type `help;` or `\h` for help. Type `\c` to clear the buffer.

mysql> USE test;
Database changed

mysql> DESCRIBE City;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| CityName | varchar(30) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
1 row in set (0.07 sec)

mysql> SELECT * FROM City;

+--------------------+
| CityName |
+--------------------+
| Hyderabad, India |
| San Francisco, USA |
| Sydney, Australia |
+--------------------+
3 rows in set (0.17 sec)

Access MySQL With Java / JDBC

The Java Database Connectivity (JDBC) API is a Java technology API for executing SQL statements. JDBC provides cross-DBMS connectivity to a wide range of SQL databases. JDBC enables developers to write platform-independent Java applications that can manipulate the data in a variety of SQL databases without the requirement to modify or recompile the Java classes when moving from one platform or DBMS to another. On the other hand, MySQL is supported on a large number of platforms. Because both JDBC and MySQL are freely available, the combination of JDBC and MySQL should be of interest to developers who create everything from Web 2.0 applications to e-commerce applications.

In order to achieve the portability of an application, the developer must provide the Java application with a programming interface that is specific to the intended database server. The implementation of such a programming interface to a database server is called a database driver, and the database driver must be installed on the client machine for successful database connectivity. The MySQL Connector/J driver allows the Java applications to connect to a MySQL database.
MySQL Connector/J JDBC Driver

MySQL provides connectivity for client applications developed in the Java programming language through a JDBC driver called MySQL Connector/J or simply Connector/J. Connector/J is a native Java driver that converts the JDBC calls into the network protocol that the MySQL database uses. In other words, Connector/J is a JDBC Type 4 driver with no dependency on the MySQL client libraries. Hence, it is not necessary to have the MySQL client programming support installed on the client machine to connect to the MySQL database programmatically.

As of this writing, Connector/J 5.1 is the latest Type 4 pure Java JDBC driver, which conforms to the JDBC 3.0 and 4.0 specifications. Connector/J 5.1 provides compatibility with all the functionality of MySQL, including 4.1, 5.0, 5.1, and the 6.0 alpha release. Connector/J 5.1 is compatible with Java Platform, Standard Edition (Java SE) versions 1.4, 1.5, and 1.6, also known as J2SE 1.4, J2SE 5.0, and Java SE 6, respectively.
Install MySQL Connector/J

The examples in the subsequent sections show the installation of the Connector/J 5.1 driver on the Solaris 10 Operating System. However, the installation steps are very similar for the Connector/J driver on all OS platforms, including the OpenSolaris OS.

You can install the Connector/J driver either from the binary or from the source distribution. For the sake of simplicity, the focus of this section will be on the binary distribution only.

1. Download the binary distribution of the Connector/J 5.1 driver in either the tar.gz or zip file format.
2. Extract the distribution archive to the directory of your choice.
..............

Source:
http://java.sun.com/developer/technicalArticles/mysql_java/
+1
Vote  Vote
Enter your comment:
No Comments For This News

Search News

What's the News?

Post a link to something interesting from another site, or submit your own original writing for the Java community to read.

Most Popular News

Most Recent User Submitted News