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
http://dev.mysql.com/downloads/connector/odbc/
The screenshot shared below will be helpful.

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

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.

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.

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.

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 {
} catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); }
} catch (ClassNotFoundException e) { e.printStackTrace(); return; } Connection connection = null;
try { connection = DriverManager.getConnection(“jdbc:odbc:mysqldsn”, “root”, “root”);
} catch (SQLException e) { e.printStackTrace(); return; } connection.close();
.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.

I hope you had a great read. Suggestions and Improvements are always welcome. Thank you.
One thought on “MYSQL DATABASE CONNECTION THROUGH JAVA”