Configure the Project

Add the MySQL driver, commons-dbcp2 and spring-jdbc dependencies to a Maven project's pom.xml so your Java code can work with a MySQL database.

Journeyman · Java · SQL · Updated

By

To work with a database, the Java Maven project needs a few dependencies, which are added in the pom.xml file.

The pom.xml file configures your Maven projects. One section defines external dependencies.

<project ...>

    <!-- project name and version -->

    <dependencies>
        <!-- dependencies added here -->
    </dependencies>

</project>

We will use the pom.xml file to import the following dependencies.

MySQL Driver

The MySQL driver is a Java library that is required to connect to a MySQL database. NOTE: Each database engine provides a driver that is used to connect to that database.

<dependencies>
  <dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <version>8.3.0</version>
  </dependency>
</dependencies>

BasicDataSource

The BasicDataSource object is an object used to open and close a connection to the database. This object exists in the commons-dbcp2 package.

<dependencies>
  <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-dbcp2</artifactId>
    <version>2.9.0</version>
  </dependency>
</dependencies>

JdbcTemplate

The JdbcTemplate is an object used to manage the execution of SQL queries. This object exists in the spring-jdbc package.

<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.6.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.2.6.RELEASE</version>
  </dependency>
</dependencies>

Cheat sheets