You are on page 1of 14

Lesson 14

Accessing MySQL Databases with JDBC

• JDBC and the MySQL Connector/J


• Registering MySQL Connector/J in BlueJ
• Accessing the MySQL Database with a
simple Java Program
JDBC and the
MySQL Connector/J
• JDBC stands for Java DataBase Connectivity. It is a
Java software that you can use to connect to a
database

• MySQL Connector/J is the recommended driver for


MySQL. It is freely available for download at

http://www. mysql.com/products/connector-j
JDBC and the
MySQL Connector/J
You need a JDBC driver in order to enable
communication between your Java program and the
database.

JDBC drivers are freely available and


downloadable over the Internet. Just pick the one you
need.
http://www. mysql.com/products/connector-j
As of this writing, the latest and recommended
JDBC driver for MySQL is MySQL Connector/J 5.1.
Just download the file.

Once you are in this page, select the version that


runs on Windows: Source and Binaries (zip).
Don’t forget to click the Download link for the zip
file.

Extract the zip file on desired location.


Registering
MySQL Connector/J in BlueJ
Since we will be creating our Java programs in
BlueJ, we need to copy the MySQL driver file (i.e.,
mysql-connector-java-5.1.7-bin.jar) in BlueJ’s
extension folder.
Registering
MySQL Connector/J in BlueJ
You need a JDBC driver in order to enable communication
between your Java program and the database.
JDBC drivers are freely available and downloadable over the
Internet. Just pick the one you need.

Find the path/folder location of BlueJ. Look for the


lib/extension folder from there and copy the MySQL driver file
named mysql-connector-java-5.1.7-bin.jar there
1

3
Next, we need to tell BlueJ that we have already added
the JDBC driver for MySQL so that our Java programs can use
them. We need to do this step in order to tell BlueJ where to
find the JDBC driver to enable Java programs to access
MySQL. In BlueJ, we do this by

1. clicking Tools --> Preferences --> Libraries.


2. Click the Add button so you can register in BlueJ the
location of our JDBC driver for MySQL which is mysql-
connector-java-5.1.7-bin.jar; Browse for its location
3. Click OK button to finalize the MySQL driver registration. ,
4. To see Changes, restart BlueJ and goback again to this
page : “Status = Loaded” clearly indicates this.
Accessing the MySQL Database
with a Simple Java Program

• We are now ready to create our first Java program


that accesses a MySQL database. To keep things
more organized, let’s put all the classes we’re going
to make in one folder.

• The objective of this project is to verify that our Java


program can indeed access MySQL.
Inside this folder we’ll put all our Java
projects. Let’s call our first Java project
TestingMySQLConnection . Do the same
procedure as what we have done in creating
the first project.

Create Project Name:


TestingMySQLConnection
Create Class Name: TestMySQL
1

3
1. This statement registers the MySQL driver class with this Java program, in order to make
this Java program access data on the MySQL server. "com.mysql.jdbc. Driver" enables a
Java program to communicate successfully with the MySQL database server.

2. This statement defines the URL of the MySQL database named friends residing on our
computer named localhost.

3. This statement gets a connection to the database at the specified URL for a user named
peter with a password=peter. In effect, this statement logs this Java program onto the
MySQL database server using peter’s account.

Take note that we grant Peter with password peter all the privilege to manipulate the
database friend.

Make sure you start the XAMPP.


Go to the folder where you installed XAMPP (i.e., C:\xampp) then click the xampp-start
icon.
Compile the Program and Exeute tne main.

We should properly shutdown the XAMPP by clicking the xampp-stop icon


-EJLM

You might also like