Share on Google+Share on Google+

Connecting to a MySQL Database in Java

In java we have been provided with some classes and APIs with which we can make use of the database as we like. Database plays as very important role in the programming because we have to store the values somewhere in the back- end.

Connecting to a MySQL Database in Java


In java we have been provided with some classes and APIs with which we can make use of the database as we like. Database plays as very important role in the programming because we have to store the values somewhere in the back- end. So, we should know how we can manipulate the data in the database with the help of java,  instead of going to database for a manipulation. We have many database provided like Oracle, MySQL etc. We are using MySQL for developing this application. 

In this section, you will learn how to connect the MySQL database with the Java file. Firstly, we need to establish a connection between MySQL and Java files with the help of MySQL driver .  Now we will make our  account in MySQL database so that we can get connected to the database. After establishing a connection  we can access or retrieve data form MySQL database. We are going to make a program on connecting to a MySQL database, after going through this program you will be able to establish a connection on your own PC.

Description of program:

This program establishes the connection between MySQL database and java files with the help of various types of APIs interfaces and methods. If connection is established then it shows "Connected to the database" otherwise it will displays a message "Disconnected from database".


Description of code:

This is an interface in  java.sql package that specifies connection with specific database like: MySQL, Ms-Access, Oracle etc and java files. The SQL statements are executed within the context of the Connection interface.

Class.forName(String driver):
This method is static. It attempts to load the class and returns class instance and takes string type value (driver) after that matches class with given string.

It is a class of java.sql package that controls a set of JDBC drivers. Each driver has to be register with this class.

getConnection(String url, String userName, String password):
This method establishes a connection to specified database url. It takes three string types of arguments like: 

    url: - Database url where stored or created your database
  userName: - User name of MySQL
  password: -Password of MySQL 

This method is used for disconnecting the connection. It frees all the resources occupied by the database.

The method is used to show error messages. If the connection is not established then exception is thrown and print the message.

Here is the code of program:

import java.sql.*;

public class MysqlConnect{
  public static void main(String[] args) {
  System.out.println("MySQL Connect Example.");
  Connection conn = null;
  String url = "jdbc:mysql://localhost:3306/";
  String dbName = "jdbctutorial";
  String driver = "com.mysql.jdbc.Driver";
  String userName = "root"
  String password = "root";
  try {
  conn = DriverManager.getConnection(url+dbName,userName,password);
  System.out.println("Connected to the database");
  System.out.println("Disconnected from database");
  catch (Exception e) {

Download this example.



Posted on: April 13, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Connecting to a MySQL Database in Java   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
August 18, 2012
i want to jdbc connection program in java

pls reply my email
September 7, 2012

which is more easy to use java or php
September 26, 2012

thank you it is really helpful content
September 30, 2012

kya hum without sql use kiye bina database use nhi kar sakte
October 25, 2012
MySQL Connection

I downloaded and installed ODBC driver for MySQL in my Windows7 OS.But while adding DSN, the ODBC driver for mySQL is not displaying....Please help....
November 27, 2012
database connectin

it s very useful.
November 30, 2012
good job..

this site more usefull for beginners..
April 1, 2013
Connecting with MySQL

While executing the above program am getting the following error. In my program i removed the UserName and Password and made it as blank. Anybody can suggest Exception in thread "main" java.lang.NoClassDefFoundError: MysqlConnect Caused by: java.lang.ClassNotFoundException: MysqlConnect at$ at Method) at at java.lang.ClassLoader.loadClass( at sun.misc.Launcher$AppClassLoader.loadClass( at java.lang.ClassLoader.loadClass( at java.lang.ClassLoader.loadClassInternal(
Er.Raj Roushan
June 12, 2013

What are the procedure to make a connection between java and mysql in control panel.