MySQL can be connected with the help of JAVA programming language. For this we require a driver which will enable us to create a connection to connect to MySQL database. A connection object is created with the help of getConnection() method of DriverManager class.

The standard syntax of this getConncetion() method contains one, two and three parameters. The syntax with the three parameters is important to us and we are going to use that syntax only.

Syntax for getConnection()


getConnection(String DataSourceName, String MySQLDatabaseUsername, String MySQLDatabasePassword);


The DataSourceName is created with the help of one exe/msi file with the name  mysql-connector-odbc-5.3.2-win32. You can download this connector from the following link

The screenshot shared below will be helpful.



Download page of JDBCODBCDRIVER


After installing this connector, you need to create a system DSN (Data Source Name) which will help you to connect to the MySQL Database.

The process of creating a system DSN is as follows.

1)      Open Data Sources (ODBC)

There are two ways to do the same.

a)      You can write Data Sources in the start menu and you will get the desired component popping in the Programs category. Following screenshot will lighten the burden. Please have a look


Data Source Locator


a)      The other way is to go to the same file location using My Computer.

The system path for Data Sources is as follows:

C:\Windows\system32\ odbcad32.exe


2)      If you use either option a or option b, you will see the following screen which is nothing but the ODBC Data Source Administrator window.


ODBC Data Source Administrator


Click on System DSN shown by red marker in the above diagram.

3)      After this, you need to click Add button on the right hand side. That will open a new window as shown below. Select MySQL ODBC 5.3 Unicode Driver and click on Finish.


MySQL DSN Name UI Window


4) Once you click Finish, you will see a new screen on you monitor. Enter the details as shown in the following diagram and click on Test button. If everything looks fine, it will pop up the new window named Test Result. It will show the CONNECTION SUCCESSFUL message. Click Ok.


successful connection of DSN with Database


5)      Then you will see that the mysqldsn added to the System DSN category. Once you click OK, you are done with the process of adding DSN to the system for MySQL JAVA connection purpose.

Now we need to proceed to the next stage and that is the coding stage

package mysqlandjava;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;


publicclass JdbcOdbcConnectionWithMysql {


publicstaticvoid main(String[] args) throws ClassNotFoundException,

SQLException {

// TODO Auto-generated method stub


try {

try {

  1. forName(“sun.jdbc.odbc.JdbcOdbcDriver”).newInstance();

} catch (InstantiationException e) {

// TODO Auto-generated catch block


} catch (IllegalAccessException e) {

// TODO Auto-generated catch block



  1. out.println(“Driver Name accepted…”);

} catch (ClassNotFoundException e) {




Connection connection = null;


try {

connection = DriverManager.getConnection(“jdbc:odbc:mysqldsn”,

“root”, “root”);

  1. out.println(“connection established…”);

} catch (SQLException e) {





  1. out

.println(“connection closed…\nCongrats you can access your database now”);






Once you run this program using Eclipse IDE, you will see the following output if everything is fine, otherwise some exception will be thrown.

successful output of the java program


I hope you had a great read. Suggestions and Improvements are always welcome. Thank you.

Published by milindjagre

I founded my blog four years ago and am currently working as a Data Scientist Analyst at the Ford Motor Company. I graduated from the University of Connecticut pursuing Master of Science in Business Analytics and Project Management. I am working hard and learning a lot of new things in the field of Data Science. I am a strong believer of constant and directional efforts keeping the teamwork at the highest priority. Please reach out to me at for further information. Cheers!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: