top of page

Connect Java Application with MySQL database




Let's see how we can connect to MySQL database from Java application.


Firstly, we need to download the necessary jar file named "mysql-connector-java" from the following link:


Then we will add "mysql-connector-java" jar file into project folder and make build path.


After that we can use the following config to connect to MySQL database.

Please don't forget to create database which you want to use in config if it is not exist yet.


Driver class : "com.mysql.cj.jdbc.Driver".

Connection URL : "jdbc:mysql://localhost:3306/temp?serverTimezone=UTC", root", "root".


In connection URL,

jdbc is API,

mysql is database,

localhost is address of server,

3306 is port number of server,

temp is database name,

serverTimezone is to set time zone (optional).


In the following sample coding,

<databaseName> : Name of the Database.

<username> : Username of the Database (Default : root).

<password> : Password of the Database.

This sample code will open database connection only. You may call from main method or any other methods to use database related operations.


After finished using, you have to close the connection to avoid connection memory leakage.


Enjoy.

Single post: Blog_Single_Post_Widget
bottom of page