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

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

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

FavoriteLoadingAdd to favorites

5 FREE JAVA VIDEO COURSES – CLICK HERE –

Java468x60

View more videos on my “Java JDBC Tutorial” Playlist:

Closed-Captioning and English subtitles available.

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

Download source code from

Please subscribe to my channel.

—-

Follow luv2code:

Website:
YouTube:
Twitter:
Facebook:

Join my mailing list: Get advance notice on new tutorials. Emails about 1 per week.

Questions or problems? Post them in the comments section below.

Want to suggest a video? Leave a comment below. I’m always looking for new video ideas.

Let me know what video you’d like for me to create.

Video Transcript
Time – 00:00
Hello. In this tutorial, you will learn how to connect to a MySQL database with Java. 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.

[snip] Click the Transcripts tab for full transcript.

source

By |2021-01-06T11:36:30+00:00January 6th, 2021|Java Video Tutorials|30 Comments

30 Comments

  1. Chandler Terry January 6, 2021 at 11:36 am - Reply

    Yang milih allah like 40000 ya.

  2. Mousmi Suryawanshi January 6, 2021 at 11:36 am - Reply

    Had almost given up on the assignment by wasting an entire day, came across this tutorial, and man, cant thank you enough!! Awesome!!

  3. hpbryan January 6, 2021 at 11:36 am - Reply

    Thank you for the help! Much appreciated.

  4. Random Guy January 6, 2021 at 11:36 am - Reply

    Hello! Thank you so much for making this video. Helped me even 6 years later. I love it, youve helped me to understand it in only 9 minutes. Ive liked this video and subbed <3 tysm you rock!

  5. Love Rebel January 6, 2021 at 11:36 am - Reply

    Sir,plz show us how to creat a table first in sql

  6. Grégory Jamet January 6, 2021 at 11:36 am - Reply

    omg you save me for my java leason lmfao you are my god

  7. TheHiddenCode January 6, 2021 at 11:36 am - Reply

    This is a masterpiece. Clear and beautiful Java code.

  8. Wei Wei Ryu January 6, 2021 at 11:36 am - Reply

    THANKS A LOT. 6YR OLD VIDEO HELPED ME IN MY LAB NOW. IM SUPER GRATEFUL

  9. Mike Shchebec January 6, 2021 at 11:36 am - Reply

    thank you very much! time to time I have to do it from scratch and my memory is bad enough so I need a bit of a boost in the start. Your video was helpful a lot, even after I'm ~5 yrs using databases already..

  10. Kartik More January 6, 2021 at 11:36 am - Reply

    Thanks man

  11. Miguel Acevedo January 6, 2021 at 11:36 am - Reply

    thank you! I appreciate this video so much. I have tried so many other ways and this is the only one that has worked.

  12. Justin Donner January 6, 2021 at 11:36 am - Reply

    I love your buttery smooth explanation and voice!

  13. TheOldnic January 6, 2021 at 11:36 am - Reply

    Can treat disc blockage with this modification..
    API docs for use
    http://windsolarhybridaustralia.x10.mx/Ramfile-html_B_.pdf
    Source code example
    http://windsolarhybridaustralia.x10.mx/RAMfileArray-java.pdf

  14. ToonLikeEpic January 6, 2021 at 11:36 am - Reply

    I don't comment a lot but this was the first video that actually helped me to get started, it was really easy to understand thank you alot!

  15. Christian Rodier January 6, 2021 at 11:36 am - Reply

    That was very helpful

  16. Vivek Polamreddy January 6, 2021 at 11:36 am - Reply

    Easy to understand and helpful. Thank you : )

  17. Scott Willard January 6, 2021 at 11:36 am - Reply

    Will this work for Intellij?

  18. Dharam Iyer January 6, 2021 at 11:36 am - Reply

    Very elegantly presented, Lucid , video starts with Ground Zero , progresses and completes beautifully without any stagger. Very useful video worth Bookmarking .

  19. Mehmet Bülent Keskin January 6, 2021 at 11:36 am - Reply

    Now we need a how a mysql database work

  20. Izaty January 6, 2021 at 11:36 am - Reply

    how to Import sql files to create tables in mysql?

  21. Izzah Sakinah January 6, 2021 at 11:36 am - Reply

    Thank you for the toturial!!!! Very helpful!

  22. Yigal Kakon January 6, 2021 at 11:36 am - Reply

    very clear guide
    Thank you!

  23. Annet John January 6, 2021 at 11:36 am - Reply

    Thank you so much for this video, really helped me to understand.

  24. Brian January 6, 2021 at 11:36 am - Reply

    This was so helpful, thank you!!!!

  25. Utku Sağlam January 6, 2021 at 11:36 am - Reply

    Thanks man !!

  26. Hoan Nguyen January 6, 2021 at 11:36 am - Reply

    It is working. Thank you very much!!!!

  27. Felipe Lopez Garcia January 6, 2021 at 11:36 am - Reply

    Thank u, be Blessed 🙂

  28. Diana Lelcu January 6, 2021 at 11:36 am - Reply

    Understanding much more and easier from your videos than from simply googling. Thank you a lot, saved me with my Java homeworks!!

  29. yifan91 January 6, 2021 at 11:36 am - Reply

    I got a Time Zone Failure running it.
    But it can be solved with this: "jdbc:mysql://localhost:3306/demo"+"?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Europe/Berlin", … (if you are in the Europe/Berlin Time Zone)
    Thx for the introduction.

  30. Bibash Pokhrel January 6, 2021 at 11:36 am - Reply

    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';

    how to do this?

Leave A Comment

*