eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

1. Overview

Connecting to a MySQL database from Eclipse can offer several benefits, especially if we’re working on database-driven applications. For example, we can use a local MySQL database to test our application’s database interactions and configuration settings (like JDBC URLs, connection pools, and other parameters) to ensure database operations work correctly before deployment. This helps us identify and fix issues early.

In this tutorial, we’ll go through the process of setting up a MySQL database in Eclipse, from downloading the necessary connectors to executing sample code.

2. Prerequisites

We need Eclipse IDE installed on our computer. We can download it from the Eclipse official website.

Next, we also need MySQL server installed to interact with the database. We can download it from the MySQL official website. If we’re using Docker, we can run a MySQL container with it. This allows us to quickly set up and manage a MySQL database in an isolated, consistent environment.

Finally, we’ll need MySQL Workbench or any other MySQL client. We can download it from the MySQL official website.

3. Steps to Setup MySQL DB in Eclipse

We’ll now take a look at setting up a MySQL database in Eclipse, from downloading and adding the MySQL connector to Eclipse IDE, to running a sample code.

3.1. Download MySQL Connector

The MySQL Connector is a driver that allows Java applications to communicate with MySQL databases. Let’s follow these steps to download it:

  1. Visit the MySQL Connector/J download page.
  2. Under Select Operating System drop down, choose Platform Independent.
  3. Download and extract the zip file to a location on our computer.

3.2. Add MySQL Connector/J to Our Eclipse Project

To interact with a database from Eclipse IDE, we need to add the MySQL Connector/J JAR to our Eclipse project’s build path. Below are the steps we need to follow:

  1. Open Eclipse and navigate to our project.
  2. Right-click on our project and select Build Path -> Configure Build Path.
  3. Go to the Libraries tab and click Add External JARs.
  4. Browse to the location where we extracted the MySQL Connector/J zip file and select the mysql-connector-j-x.x.x.jar file.
  5. Finally, click Open and then Apply and Close.

3.3. Create a MySQL Database and Table

Let’s create a sample database and table to work with. First, we open MySQL Workbench or any other MySQL client and connect to our MySQL Server. Then, we execute the following SQL commands to create a database and a table:

CREATE DATABASE sampledb;

USE sampledb;

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    email VARCHAR(50) NOT NULL
);

In the above script, we create a database named sampledb, switch to it, and then create a users table with columns for id, username, and email.

3.4. Sample Code

Now, let’s write some Java code to connect to our MySQL database and perform basic operations:

@BeforeAll
public void setUp() throws Exception {
    String url = "jdbc:mysql://localhost:3306/sampledb";
    String user = "root";
    String password = "<YOUR-DB-PASSWORD>";

    Class.forName("com.mysql.cj.jdbc.Driver");

    conn = DriverManager.getConnection(url, user, password);
    stmt = conn.createStatement();
}

Methods annotated with @BeforeAll are executed once before all test methods in the test class. Here, we’re trying to initialize a connection to a MySQL database before any tests are run. It ensures that the database connection is established and a Statement object is created for executing SQL queries during the tests.

It’s worth noting that conn and stmt here are class variables, and YOUR-DB-PASSWORD is the database password we created.

Let’s now take a look at the JUnit test method that inserts a user record into the users table, retrieves and verifies the inserted user’s details, and then deletes the user to clean up the test data:

@Test
public void givenNewUser_whenInsertingUser_thenUserIsInsertedAndRetrieved() throws Exception {
    String insertSQL = "INSERT INTO users (username, email) VALUES ('john_doe', '[email protected]')";
    stmt.executeUpdate(insertSQL);

    ResultSet rs = stmt.executeQuery("SELECT * FROM users WHERE username = 'john_doe'");

    assertTrue(rs.next());
    assertEquals("john_doe", rs.getString("username"));
    assertEquals("[email protected]", rs.getString("email"));

    String deleteSQL = "DELETE FROM users WHERE username = 'john_doe'";
    stmt.executeUpdate(deleteSQL);
}

4. Connecting to MySQL DB From Eclipse IDE

Having database access within our development environment means we don’t have to switch between different tools. This makes it easy to manage database schemas, tables, and other objects directly from Eclipse.

Let’s see the steps to connect to MySQL DB from Eclipse IDE:

  1. Open Data Source Explorer by navigating to Window > Show View > Other > Data Management > Data Source Explorer.
  2. Inside Data Source Explorer, right-click Database Connections > New.
  3. Select MySQL (or any other database that we want to connect to). Give it a meaningful name and click Next.
  4. On the next popup, click on the + icon to create a new driver definition.
  5. On the next popup window, under Name/Type Tab, select the Database version.
  6. Click on Jar List tab and click on Clear All to clear any existing jar (we’ll add one, on our own).
  7. Click Add Jar/Zip… and choose the connector Jar that we downloaded.
  8. Click on Properties tab to give database connection properties, click OK

Finally, we can click on Test Connection to verify we’re able to connect to the database:

Database Connection Parameters Eclipse

5. Conclusion

Setting up a MySQL database in Eclipse is a straightforward process that involves downloading the MySQL Connector, configuring Eclipse, creating a database, and writing Java code to interact with the database.

By following the steps outlined in this article, we can efficiently manage and utilize MySQL databases within our Eclipse environment, enhancing our development workflow.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)