Sitemap
Imsannykumar

Imsannykumar

Java Programming Tutor

How to connect MySQL database with Java ?

3 min readDec 25, 2025

Connecting a database with Java is a very important concept for every Java developer. Java uses JDBC (Java Database Connectivity) API to communicate with databases like MySQL, Oracle, PostgreSQL, and others. JDBC provides a standard way to connect Java applications with databases, execute queries, and process results.

In this article, I will explain the six basic steps required to connect a database with Java in a simple and easy language. These steps are common for almost all databases when using JDBC.

Step 1: Load the Driver

The first step is to load the JDBC driver. A driver is a software component that allows Java to communicate with the database. Each database has its own JDBC driver.

When we load the driver, Java gets information about how to connect to the database. This is usually done using the Class.forName() method.

Example:
Class.forName(“com.mysql.cj.jdbc.Driver”);

In modern versions of JDBC, the driver may load automatically, but it is still good practice to load it explicitly to understand the process clearly.

Step 2: Establish the Connection

After loading the driver, the next step is to establish a connection with the database. A connection represents a session between the Java application and the database.

To create a connection, we use the DriverManager.getConnection() method. This method requires three main details:

  1. Database URL
  2. Username
  3. Password

Example:
Connection con = DriverManager.getConnection(
“jdbc:mysql://localhost:3306/mydatabase”,
“root”,
“password”
);

If the connection is successful, Java is now connected to the database and ready to perform operations.

Step 3: Create a Medium

Once the connection is established, we need a medium to send SQL queries to the database. This medium is created using objects like Statement, PreparedStatement, or CallableStatement.

Statement is used for simple queries.
PreparedStatement is used for dynamic queries and is more secure.
CallableStatement is used to call stored procedures.

Example:
Statement stmt = con.createStatement();

This medium acts as a bridge between Java and the database for executing SQL commands.

Step 4: Write and Execute the Query

Get Imsannykumar’s stories in your inbox

Join Medium for free to get updates from this writer.

In this step, we write the SQL query and execute it using the medium created in the previous step.

There are different methods to execute queries:
executeQuery() is used for SELECT queries.
executeUpdate() is used for INSERT, UPDATE, and DELETE queries.
execute() can be used for any type of query.

Example:
String query = “SELECT * FROM student”;
ResultSet rs = stmt.executeQuery(query);

The query is sent to the database, and the database returns the result.

Step 5: Display the Result

After executing a SELECT query, the result is stored in a ResultSet object. ResultSet holds the data returned by the database.

We use a loop to read the data row by row from the ResultSet and display it.

Example:
while (rs.next()) {
System.out.println(rs.getInt(“id”));
System.out.println(rs.getString(“name”));
}

Each call to rs.next() moves the cursor to the next row in the result.

Step 6: Close the Resources

The final step is to close all the resources used during the database operation. This is very important to avoid memory leaks and performance issues.

The resources that should be closed are:

  1. ResultSet
  2. Statement or PreparedStatement
  3. Connection

Example:
rs.close();
stmt.close();
con.close();

Closing resources ensures that the database connection is released properly and can be reused by other applications.

Conclusion

Connecting a database with Java using JDBC follows a simple and logical flow. First, we load the driver, then establish a connection, create a medium to communicate, execute SQL queries, process the results, and finally close all resources. Understanding these six steps clearly will help you build strong database-driven Java applications and perform database operations confidently.

Press enter or click to view image in full size

The code for JDBC program,using all six steps

package jdbcproject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;

public class jdbcprogram {

public static void main(String[] args) {
// TODO Auto-generated method stub
Connection con = null;
try {
// step1-load the driver
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Driver is loaded");
// Establish connection
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/decjdbc","root","Sunny6204@");
//(url,username,password)
System.out.println("connection established");
// statement created
Statement st =con.createStatement();
System.out.println("statement medium is created");
//step 4- write querying
String sql = "select * from student";
ResultSet rs = st.executeQuery(sql);
System.out.println("query excuted and result in java");
// displaying the result
System.out.println("s_id" + " " + "s_name"+ " " + "s_marks");
while(rs.next()) {

System.out.println(rs.getInt(1) + " " + rs.getString(2) + " " + rs.getInt(3));
}
// step6 - close the resources
con.close();
st.close();
rs.close();

}
catch(Exception e) {
e.printStackTrace();
}

}

}

--

--