How to Connect to a MySQL Database Using JDBC

Migel Hewage Nimesha Feb 02, 2024
  1. Basic Requirements to Connect a Database Using JDBC
  2. Connect to a MySQL Database Using JDBC
  3. Conclusion
How to Connect to a MySQL Database Using JDBC

JDBC stands for Java Database Connectivity. This Java API connects and executes the query with the database.

The API uses JDBC drivers to connect with the database. The drivers comprise four types: JDBC-ODBC Bridge Driver, Native Driver, Network Protocol Driver, and Thin Driver.

Basic Requirements to Connect a Database Using JDBC

MySQL provides connectivity for applications developed using the Java programming language with MySQL Connector/J. Connector/J implements the JDBC API and several value-adding extensions of it.

To connect the MySQL database with Java, we should have two main components installed in our system.

  • Java Development Kit (JDK)
  • MySQL JDBC Driver

Besides the above requirements, we need an IDE to implement the Java code.

If you don’t have the JDBC driver, search and download the relevant driver to your system from the internet. Also, you can use the below link to download the MySQL Installer, and through that, you can install the Connector/J connector.

https://dev.mysql.com/downloads/windows/installer/8.0.html

Or you can install the connector when installing MySQL to your system. Then we need to load the mysqlconnector.jar file.

We can copy the jar file and paste it into the JRE/lib/ext folder, and we are good to go. After having both requirements ready, we can implement Java code to connect the MySQL database.

Connect to a MySQL Database Using JDBC

Before setting up the connection, we should import the SQL libraries for the Java code as below.

import java.sql.Connection; // To create a connection
import java.sql.DriverManager; // To access the JDBC ddriver
import java.sql.SQLException; // provides info on database access errors or other errors

Then we can define the driver class as below.

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

The driver class com.mysql.jdbc.Driver was the driver class for the MySQL database, but now it’s deprecated. The new driver class is com.mysql.cj.jdbc.Driver, as shown above.

Now we can create a connection to the MySQL database. The syntax to make a connection is as below.

Connection con = DriverManager.getConnection(ConnectionString);

Here, the ConnectionString is a crucial phase, and the syntax to write the ConnectionString is below.

"jdbc:mysql://host:port/database","username","password"

If we explain the above connection string, jdbc is the API, and mysql is the database. host is the server name on which MySQL is running.

port is the port number that the database uses, and database is the database name we intend to connect. username is the database user’s username, and the password is the user’s password for the database.

As in below, we can modify the syntax used to create the connection with the string.

Connection con =
    DriverManager.getConnection("jdbc:mysql://host:port/database", "username", "password");

Full Code:

package mysqldemos;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MySQLJDBC {
  public static void main(String[] args) throws SQLException, ClassNotFoundException {
    Class.forName("com.mysql.cj.jdbc.Driver");
    Connection con =
        DriverManager.getConnection("jdbc:mysql://host:port/database", "username", "password");
  }
}

Let’s see how we can connect a MySQL database using the above code. Note that the components of the connection string have to be changed according to our database information.

package mysqldemos;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MySQLJDBC {
  public static void main(String[] args) throws SQLException, ClassNotFoundException {
    Class.forName("com.mysql.cj.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/new", "root", "root");
  }
}

Running the code doesn’t provide any output as it is connected to the MySQL database. This is how to tie a MySQL database using the JDBC MySQL connection string and relevant resources.

Conclusion

This write-up explains the JDBC MySQL connection string along with an introduction and some essential components that relate to the connection string. We looked at the requirements to set a connection to the MySQL database and how we can set a connection through the Java code by fulfilling those features.

Migel Hewage Nimesha avatar Migel Hewage Nimesha avatar

Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.