Java JDBC Tutorial – Part 1: Connect to MySQL database with Java

Hello. In this session we’re going to learn
how to connect a MySQL database with Java. Before we get started there are some prep
work that needs to take place. One is that you need to have a MySQL database already
installed. If not, you can download one from the site listed here on the slide. Next, we’ll need to download and install
a MySQL JDBC driver. This driver will allow the Java program to connect to the database.
You can download it from this URL, and I’ll do that now. I’ll just take this URL, drop
it into my favorite browser and here I am at the page. I’m on this page, this is for
downloading the Connector/J browser. I’ll select the platform, platform independent.
Now move down and I’ll select download and it will download a zip archive. Make sure
you download the most recent version. In this case it’s 5.1.29. Make sure you download
something at that version or later. Download, scroll down, select, “No thanks,
just start my download.” This will start downloading the file for you. Now what we
can do is show it in the finder. I’ll double click it to extract it. At this point I have
the driver extracted. There’s a file in here called MySQL connector.jar that we’ll
use later. Next, what I need to do is create a Java project
in Eclipse. Let’s do that. On Eclipse I’ll say I’ll save file, new, Java project. This
will be JDBC demo and I will select finish. What I’ll do is I will add a new class.
New, class, this will be my driver and it will have a public static void main region.
At this point we have a shell for our project created. Next, what we’ll do is we will talk to the
actual development process and I really like this slide because it outlines exactly what
we need to do. The first thing we’ll do is add the database driver to the classpath.
Next, we’ll get a connection, we’ll submit a SQL query and then we’ll process the result
set. Let’s start with step one, adding a MySQL
database driver to the classpath. In our Eclipse project I’m going to add a new folder called
lib, lib for library. In this lib folder I’m going to add the driver that I downloaded
earlier. What I’ll do is I’ll just grab this driver. It’s the MySQL connector bin.jar
and I’ll just drag and drop it to this lib folder. It will prompt me and I’ll say go
ahead and copy the file. Next, I need to add the jar file, the driver
to the classpath. I’ve set this up with properties, choose Java build path and then
I’ll choose add jars. Expand this folder, that lib, I’ll grab that jar file and I’ll
have it setup. Great. Now we can start coding. We need to get a
connection, submit a SQL query and process a result set. Let me just sketch this out
on a slide over here. The first thing I’ll do up top is I’ll just do an import on java.sql.*
because all of the classes and interfaces that we need are in this package. I’ll also just setup a try catch block just
to handle for any exception that may occur. Small typo there. Inside of this section I’m going to first
get a connection to database. Then next I will create a statement. Then from here I
will execute a SQL query and then I will process the result set.
That’s my basic game plan, let’s fill it in with some real code. Here I’ll say
connection, myConn=DriverManager.getConnection. I’ll give the jdbc:mysql:localhost3306demostudentstudent.
Student is the user ID and student is also the password. Now, let me digress for one second here. We’re
connecting to a database called … it’s on local host, demo. On my system, I already
have MySQL installed. I’m using a MySQL workbench, it allows me to query or look at
the actual database. I have a database out here called demo and I have a table called
employees. This employees table has three columns. It has a column for the ID, the last
name, first name and the e-mail address for that employee. I’m going to want to do something very similar
here in my actual Java code. The big things that I have are database called demo and a
table called employees. That gives me the portion here of connecting to this database
demo and then now I can go ahead and start writing some of MySQL statements. First up I’ll start with statement, my statement
equals myConn.createstatement. Then I execute the SQL query. I’ll say result set myRs
equals my statement.executeQuery, select star from employees. Next I’ll process the result set,
then I’ll say system.out.printline and I will print line myRs.getString. I’m going
to retrieve the person’s last name and then I’ll also say … I’ll pin this with myRs.getString
first name and I’ll continue the loop. That’s pretty much it as far as the actual
coding. Now, let’s try and run this application and see how things work out for us. We can
all just right click and I’ll say run as Java application. Then note, the output here,
John Doe, Mary Public, Susan Queue, and if we look at our database same values occur.
We are successful in connecting to MySQL database with Java. That’s it, thank you for watching the presentation.
You can visit for more tutorials.

Danny Hutson

Leave a Reply

Your email address will not be published. Required fields are marked *