HOWTO · Java
MySQL Connector Java Maven Dependency
Add MySQL Connector/J to a Maven project and use JDBC to connect to a MySQL database.
On this page
To use MySQL Connector/J from a Java application, add its Maven dependency to pom.xml, resolve the project, and open a JDBC connection with DriverManager. You also need a reachable MySQL server and valid database credentials.
Select a supported Connector/J version and Maven coordinates
The Maven coordinates are group ID com.mysql and artifact ID mysql-connector-j. Select a Connector/J version from the official MySQL Connector/J documentation. Version availability and support can change, so use the version appropriate for your project rather than relying on a version copied from an old example.
Add Connector/J to pom.xml
Add this dependency inside the <dependencies> element in your project’s pom.xml. Keep the group ID, artifact ID, and version as separate values, and replace ${connector-j.version} with the version you selected:
<dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><version>${connector-j.version}</version></dependency>
If your project uses a Maven property for dependency versions, define connector-j.version in the <properties> element and keep the placeholder in the dependency. This makes future version updates easier to manage.
Resolve the dependency and verify the Maven project
Run a Maven build from the directory containing pom.xml, for example mvn clean verify. Maven resolves the declared dependency as part of the build and places it on the project classpath used by Maven. The command does not permanently add a library to the classpath of every environment; configure your IDE, packaging, or runtime through the project’s Maven setup as appropriate. A successful build checks the project according to its configured lifecycle, including any tests bound to it.
Connect to MySQL with JDBC
JDBC is included with Java. Connector/J supplies the MySQL JDBC driver, which DriverManager uses for a jdbc:mysql: URL. Set the URL to your server and database, then provide a database username and password. For example, the URL shape is jdbc:mysql://HOST:3306/DATABASE; replace each uppercase value with your server host and database name. Keep credentials out of source control; environment variables or a secrets manager are better choices for deployed applications.
A JDBC connection needs Connector/J on the runtime classpath, a reachable MySQL server, and valid credentials. This complete class reads its credentials from DB_USER and DB_PASSWORD, uses the example JDBC URL, and calls DriverManager.getConnection(url, username, password). It is ready to adapt in a Maven project; the supplied runtime evidence below demonstrates URL construction only, not a connection to MySQL.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MysqlConnectionExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/inventory";
String username = System.getenv("DB_USER");
String password = System.getenv("DB_PASSWORD");
if (username == null || password == null) {
System.out.println("Set DB_USER and DB_PASSWORD before running this example.");
return;
}
try (Connection connection = DriverManager.getConnection(url, username, password)) {
System.out.println("Connected to MySQL.");
} catch (SQLException e) {
System.out.println("Connection failed: " + e.getMessage());
}
}
}
From the Maven project root, set DB_USER and DB_PASSWORD in the shell, then use the exec plugin to launch MysqlConnectionExample. Replace its URL with your server and database. If you add a package declaration, include the fully qualified class name in -Dexec.mainClass:
mvn -q -DskipTests compile org.codehaus.mojo:exec-maven-plugin:3.5.0:java -Dexec.mainClass=MysqlConnectionExample
If the driver is missing, confirm that the mysql-connector-j dependency is inside the project’s <dependencies> element and that Maven can resolve it. The Maven exec goal uses that dependency classpath; running java MysqlConnectionExample directly does not automatically include Maven dependencies.
Run and adapt the example
After adding the dependency, place MysqlConnectionExample in the Maven project’s Java source tree. Set DB_USER and DB_PASSWORD in the shell before running the Maven exec command above. A successful connection requires a reachable MySQL server, the selected database, and credentials that permit access. The following separate example assembles the JDBC URL from editable settings; it does not contact a server:
Run this example:
public class JdbcUrlExample {
public static void main(String[] args) {
String host = "localhost";
int port = 3306;
String database = "inventory";
String url = "jdbc:mysql://" + host + ":" + port + "/" + database;
System.out.println(url);
}
}
jdbc:mysql://localhost:3306/inventory
Adapt the host, port, and database in MysqlConnectionExample to your environment. Its success message is printed only after DriverManager.getConnection returns; a failure message reports the JDBC error, while missing credentials produce the setup prompt. The supplied runtime result above is only the URL printed by JdbcUrlExample; it does not show that the JDBC connection succeeded or failed. If your connection fails, check that Maven resolved Connector/J, the URL names the correct host, port, and database, the server accepts connections from your application, and the credentials are valid. A Java compiler alone cannot provide a database service.