Java Video Tutorial 34

Java JDBC EclipseIn this part of the Java Video Tutorial I show you how to setup Java JDBC in Eclipse.

I then cover how to connect to a MySQL database, query it and get the results of a query. We also look at the Exceptions you’ll need and much more.

I will cover SQL in this tutorial, but not to the extent that I did in the past. My SQL tutorials can be found after the video.

If you like tutorials like this, share it

SQL Video Tutorials

Setup JDBC in Eclipse

There are a couple of ways to setup JDBC in Eclipse. First You need to get JDBC Connector.

The easiest way on Macs and PCs is to configure the build path in Eclipse:

  • In Eclipse Right click on your Project folder
  • Click Build Path and Configure Build Path
  • Click the Library tab
  • Click Add External jars and locate the file named mysql-connector-java-5.1.19-bin.jar

Setup JDBC the Easy Way on Macs

Just copy the jar file above to the directory /System/Library/Java/Extensions. That is it!

Setup JDBC on PCs by Adding to the ClassPath

  • Open Control Panel and double click on System
  • Click the Advanced Tab
  • Click Environment Variables
  • At the end of the classpath add ;c:\the location of mysql-connector-java-5.1.19-bin.jar

Code from the Video

// The API for accessing and processing data stored in a database

import java.sql.*;

public class Lesson34 {
    public static void main(String[] args) {
    	// A connection object is used to provide access to a database
    	Connection conn = null;
        try {
            // The driver allows you to query the database with Java
        	// forName dynamically loads the class for you
            // DriverManager is used to handle a set of JDBC drivers
            // getConnection establishes a connection to the database
            // You must also pass the userid and password for the database
            conn = DriverManager.getConnection("jdbc:mysql://localhost/customer","mysqladm","turtledove");
            // Statement objects executes a SQL query
            // createStatement returns a Statement object
            Statement sqlState = conn.createStatement();
            // This is the query I'm sending to the database
            String selectStuff = "Select first_name from customer";
            // A ResultSet contains a table of data representing the
            // results of the query. It can not be changed and can 
            // only be read in one direction
            ResultSet rows = sqlState.executeQuery(selectStuff);
            // next is used to iterate through the results of a query
        catch (SQLException ex) {
        	// String describing the error
            System.out.println("SQLException: " + ex.getMessage());
            // Vendor specific error code
            System.out.println("VendorError: " + ex.getErrorCode());
        catch (ClassNotFoundException e) {
			// Executes if the driver can't be found

35 Responses to “Java Video Tutorial 34”

  1. mr.roshik says:

    thanks for informative tutorial again.

  2. Paul says:

    Excellent video tutorial series on Java. Thank you.

  3. Punit says:

    hey nice, you started with jdbc, great..
    please cover as much as possible, as usual..
    also callable statement, how to display data from multiple tables(joins), and all..
    if possible create one standalone application in swing with database, that covers all.
    Thank you..

    • admin says:

      I’ll cover as much as possible. The SQL tutorial I mentioned will go into more detail on SQL than I plan to cover here. I don’t want to redo past work. Don’t worry, I’ll explain more than enough for you to use databases with Java

  4. TheCoder says:

    Hey, thanks alot, great information.

    I’m trying write a short java code that connects to a MySQL database, but I don’t want to hardcode the password at all into my code (Security reasons, obviously).

    Can you please show how to do such a thing?

    Thanks again,
    keep up those great vids!

  5. Zubaidi says:

    Nice tutorial… 😀

  6. Nicky says:

    Hi, I have a question as I am very new with Sql. do I have to install mysql? and how do you name a sql to be connected to your java code? thanks

  7. Altin says:

    I have just discovery this website and i have watch about 5 videos. Your work is wonderful and it is very helpful i think for all.
    I was seraching something about java Generics. maybe you have any video around but i will take the time watching something before.
    Thnks for your work man from Italy!!!

    • Derek Banas says:

      Thank you for the kind words. By the end I will have covered everything there is on Java, but it is taking more time than I could have imagined in the beginning. I’m very happy that you like them 🙂

  8. fred says:

    I download as per lesson java video 34 mysql
    when I run your code I got:


    at Method)
    at Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at march202013.Lesson34.main(


  9. Abhi says:

    Hi Derek,

    I attempted to add the JDBC connector through eclipse but it wouldn’t let me add it to the JRE System Library and it was added to a separate Reference Library instead. I attempted to follow you on this tutorial and I kept getting this error:
    SQLException: No suitable driver found for jdbc:mysql:localhost/customer
    VendorError: 0

    I also tried to add it through the class path but it lead to the same result. Please let me know how I can fix this. Also, thank you for the awesome tutorials.

    • Derek Banas says:


      Make sure that your JDBC driver is loaded and / or that you are using the correct URL. The URL should have this format jdbc:mysql://localhost:3306/databasename

      I hope that helps

  10. NFiorentini says:

    Hi again Derek! I am completely new to SQL, thus my description of the problem(s) is going to look a bit like a Neanderthal trying to describe the latest iPhone (I’ll TRY to limit the grunting and monitor-beating-with-a-stick).

    Windows machine here. If I go here … … and select Microsoft Windows, I have just the option of downloading mysql-connector-java-gpl-5.1.26.msi. When I click it after downloading, a window BRIEFLY pops up(“Please wait while Windows configures …”), and then … nothing. Doing that multiple times (and going into the Task Manager seems to confirm that nothing happens).

    On that same page, if I select Platform Independent, I’m then given the option of downloading the .zip. Downloading, extracting, and using the Eclipse option that you describe at about the 1:20 mark in the video … everything seems to be fine.

    Then at the 1:58 mark of the video, you bring up a Terminal. So first question … Is this window the Apple equivalent of the Windows CMD Prompt window? And, if so, do you have any recommendations on Command Line tutorials (I’ve avoided Command Line like Ebola for all of my 18 years of computer semi-literacy).

    From there, copying the Java code and pasting it into a .java via Eclipse … no errors! However, I don’t have my own UserID and Password. So second question … what am I supposed to register/sign up for here in order to learn SQL? I can’t seem to find an explanation anywhere of how all of this fits together. Every explanation that I can find seems to begin with actually using SQL commands and not setup.

    I’ve also installed WAMPSERVER because it seemed like the thing to do at the time (Beer number three, I think).

    Thank you for all of these videos and any help that you might have in this situation!

    • Derek Banas says:

      Funny post 🙂 I’ll do my best to help you with this.

      Q. Is this window the Apple equivalent of the Windows CMD Prompt window?

      A. Yes they are pretty much the same thing. The difference is that Macs are pretty much Linux operating systems while Windows uses different commands.

      The easiest solution is to install something like WUBI. It is just like installing Linux on your Windows machine as an application. Then you’ll be able to easily install all of this stuff. If you decide you don’t like WUBI you can just uninstall it like any other application.

      Tell me if you have any other questions, but I think that is the easiest solution.

      • NFiorentini says:

        Sorry about the late reply. I was out of town.

        At the top of my computer science education goals is to master Java completely, then Android, and then to set up an older computer with a flavor of Linux (Ubuntu, Mint, etc.). I’m going to hold off on the SQL tutorials until that time and focus on the Java and Android tutorials for now.

        Thank you again!

        • Derek Banas says:

          You’re very welcome 🙂 I have pretty much everything you need to learn java. I’ll keep making Android tutorials for about another 6 months or more. I hope they help.

  11. David McMonigle says:


    I was a bit stumped! I saw you were writing in a terminal on a Mac, and maybe I was just being silly, but I was in MySQL using the MySQL command line. The Command Prompt for windows was not working, how can I troubleshoot this?

  12. Sham says:

    Hello Derek,
    I am working on XP, so in addition to mysql connector, i also need Mysql DB server itself, to connect to it.

    So am picking
    mysql-5.5.34-win32.msi (DB server)

    Is this fine?

  13. j says:

    Your tutorials are a big help thanks a lot.
    I get the following error when i run the code and none of the data from the db shows:

    A fatal error has been detected by the Java Runtime Environment:
    # Internal Error (classFileParser.cpp:3470), pid=2660, tid=7164
    # Error: ShouldNotReachHere()
    # JRE version: 7.0-b147
    # Java VM: Java HotSpot(TM) 64-Bit Server VM (21.0-b17 mixed mode windows-amd64 compressed oops)
    # Failed to write core dump. Minidumps are not enabled by default on client versions of Windows


  14. j says:

    Your tutorials are a big help thanks a looottttt
    None of the data from the db shows and the following error occurs

    # A fatal error has been detected by the Java Runtime Environment:
    # Internal Error (classFileParser.cpp:3470), pid=6964, tid=3228
    # Error: ShouldNotReachHere()
    # JRE version: 7.0-b147
    # Java VM: Java HotSpot(TM) 64-Bit Server VM (21.0-b17 mixed mode windows-amd64 compressed oops)
    # Failed to write core dump. Minidumps are not enabled by default on client versions of Windows

  15. j says:

    Hi thanks a lot but I tried but without luck I just made the customer database you made by doing your tutorials SQL Video Tutorial Pt 1-2-3-4 and 5 and it worked out perfectly they are really good.
    When i try run configurations i have to pick a device how can I just see the results on console in eclipse like yours ? Without a xml file for this tutorial I cant launch it on a device without getting an erro you see? Id like to know how to output the data on a xml layout this i stiil have to learn!. Please Please help! 

  16. abe says:

    i am trying to get the tutorial 34 up and running and i am getting this error message:

    Exception in thread “main” java.lang.Error: Unresolved compilation problem:
    Syntax error, insert “Finally” to complete TryStatement

    at Datab.main(

    and in line 18 there is only a comment
    i have MYSQL installed and running on my PC
    i have worked with it before and have some database in it

  17. abe says:

    its ok i solved the problem thank you
    now its running normal

  18. Aruni says:

    Nice tutorials! Keep the good work up..

Leave a Reply

Your email address will not be published.