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

In this video tutorial, you will learn how to connect to a MySQL database with Java JDBC.

Please subscribe 🙂

 

Download Java Source Code


SQL Script

The code examples in the tutorials make use of the following user:

  • user id: student
  • password: student

You will need to create this user in your MySQL Admin tool. Execute the following SQL in the MySQL Admin tool:


You will need to create the database table. Run the following SQL script in your MySQL admin tool.

 


Video Transcript

Time – 00:00
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.

Time – 00:20
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.

Time – 01:04
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.

Time – 01:34
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.

Time – 02:28
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.

Time – 02:48
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.

Time – 03:25
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.

Time – 03:51
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.

Time –  04:17
I’ll also just setup a try catch block just to handle for any exception that may occur. Small typo there.

Time – 04:40
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.
Time – 05:16
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://localhost:3306/demo student, student. Student is the user ID and student is also the password.

Time – 06:00
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.

Time – 06:42
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.

Time – 07:00
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.

Time – 07:41
Next I’ll process the result set, myRs.next 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 append this with myRs.getString first name and I’ll continue the loop.

Time – 08:20
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.

Time – 08:58
That’s it, thank you for watching the presentation. You can visit luv2code.com for more tutorials.

 

Source Code: Driver.java

Download Source Code

Did you like my blog post?

Signup now and receive an email when I publish new luv2code content.

I agree to have my personal information transfered to AWeber ( more information )

I will never give away, trade or sell your email address. You can unsubscribe at any time.

3 Comments

  1. Comments  Mohamed Said   |  Tuesday, 24 November 2015 at 11:44 pm

    how i can configure this specially in what part in admin tools i can write this

    user id: student
    password: student
    You will need to create this user in your MySQL Admin tool. Execute the following SQL in the MySQL Admin tool:

    CREATE USER ’student’@’localhost’ IDENTIFIED BY ’student’;
    GRANT ALL PRIVILEGES ON * . * TO ’student’@’localhost’;

  2. Comments  Abbie   |  Wednesday, 14 February 2018 at 12:02 pm

    is this just a mere java api mysql connection or does this includes jdbc: odbc connectivity?

  3. Comments  Chad Darby   |  Wednesday, 14 February 2018 at 1:18 pm

    This is pure java api. No need for odbc.

Leave a Reply