Java Database Connectivity with MySQL
To connect Java application with the MySQL database, we need to follow 5 following steps.
In this example we are using MySql as the database. So we need to know following informations for the mysql database:
Let's first create a table in the mysql database, but before creating table, we need to create database first.
Example to Connect Java Application with mysql database
In this example, sonoo is the database name, root is the username and password both.
The above example will fetch all the records of emp table.
To connect java application with the mysql database, mysqlconnector.jar file is required to be loaded.
Two ways to load the jar file:
1) Paste the mysqlconnector.jar file in JRE/lib/ext folder:
2) Set classpath:
How to set the temporary classpath
How to set the permanent classpath
Go to environment variable then click on new tab. In variable name write classpath and in variable value paste the path to the mysqlconnector.jar file by appending mysqlconnector.jar;.; as C:\folder\mysql-connector-java-5.0.8-bin.jar;.;