Java and MySQL Connectivity Using JDBC

This tutorial is about Connecting Java to MySQL database using JDBC driver. You may find many tutorial on this topic but most of them show you the code and links to download driver and install it which is what i am also gonna do but in easy, straight forward way.

You need MySQL database before proceeding. Installing XAMPP in your system helps a lot because apart of MySQL you will get many other handy tools coupled as phpmyadmin package. So if you do not have it, please install it and run it. You should have this screen in XAMPP control panel.

xampp control panel

Since this tutorial is about connecting to MySQL database, i am not going to create new database or that stuff, instead i am going to use databases which come’s as example in MySQL database. To know it go to http://localhost:1000/phpmyadmin/. I am going to use “cdcol” database (it is example database comes with installation) to demonstrate my code.

NOTE: For this tutorial and running code successfully i am going to ask you to download latest Java from here and Eclipse editor. If you running Windows 64 bit like me then choose 64 bit Java and 64 bit Eclipse else you may face any issue (issue is debuged already. See here).

Open Eclipse, Create new Java project from File Menu. Inside “src” folder in Project on “Package Explorer” create new Java file and name it properly.

build path

Now it’s time to add JDBC library in our project. Right click on Project in “Package Explorer” and Build path -> Configure Build Path. Window Pops up. Click on “Add External Jar” and choose the Jar file from Computer.

Link: Download JDBC Jar file.

add external jar

Ok now you have done with the configuration of JDBC driver. It’s time to code our project and check whether it’s working or not.

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

Import these classes which is Java built in class for SQL operation. To load the JDBC driver we are going to use “Class.forName“. This line will load it.

Class.forName(“com.mysql.jdbc.Driver”);

This line will load the JDBC. Now its time to create Connection variable in order to connect to our database. So let’s do it. We are going to use “getConnection()” method. Syntax for it as shown below.

DriverManager.getConnection(“jdbc:mysql://<Host>/Database”,”<MySQL user name>”, “MySQL password”);

In my case it is.

DriverManager.getConnection(“jdbc:mysql://localhost/cdcol”,”root”, “”);

Where “cdcol” is database name, “root” is default MySQL username and MySQL password is blank. Here is complete code.

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
class main
{
public static void main(String args[])
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://localhost/test","root", "");
System.out.print("Database is connected !");
conn.close();
}
catch(Exception e)
{
System.out.print("Do not connect to DB - Error:"+e);
}
}
}

Copy the code and paste it in your file. Let’s run it. In eclipse just hit the play button and it will compile and run it. Here is output of this code.

output

Well it runs, let’s see what happens when we stop our MySQL server and run the code. Stop MySQL database from XAMPP control panel and then run the code. I got this.

debug output
I hope you find this tutorial useful and in next upcoming tutorial we will deal with Java Servlet and many other cool stuff and again in easy way.

Shahid (UnixRoot) Shaikh

Hey there, This is Shahid, an Engineer and Blogger from Bombay. I am also an Author and i wrote a programming book on Sails.js, MVC framework for Node.js.

Related Posts

6 Comments

  1. Do not connect to DB – Error:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

    I got this error what should i do

Leave a Reply

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