Connect to a MySQL database in Eclipse

Home > Search

First, you will download the MySQL or MariaDB connector, and then extract the JAR file from the zipped archive..


Create a new project

  1. In Eclipse, select File New > Java Project.
  2. Give the project a new, such as SQLProject, and select Finish.

Add mysql-connector-java-<version>-bin.jar to the new project

  1. Right-click on SQLProject and select New > Folder. Name the folder lib (short for Library) and select Finish.
  2. Drag and drop the mysql-connector-java-<version>-bin.jar file into the lib folder.
  3. Right-click on the SQLProject folder and select Properties.
  4. Select Java Build Paths.
  5. Select the Libraries folder.
  6. Select Add JARs.
  7. Select the the mysql-connector-java-<version>-bin.jar file and select Apply and Close.

Create the class

  1. In the left panel of Eclipse, expand SQLProject.
  2. Right-click on the src folder and select New > Class.
  3. The class can be named anything you want, select as Testing. Check public static void main(String[] args) and select Finish.
  4. Update the markup to have the following.


package mySQL;

import java.sql.*;
public class Testing {

	public static void main(String[] args) {

		try {
		Connection myConnection = DriverManager.getConnection("jdbc:mysql://","your_username","your_password");
		Statement myStatement = myConnection.createStatement();
		ResultSet myResultSet = myStatement.executeQuery("select * from table_name");
		while ( {
		} catch (Exception e) {





Run the project

  1. Right-click on SQLProject and select Run as > Java Application.


In this example, after running the application, data from MySQL is displayed in the console.

Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter in the box below so that we can be sure you are a human.