CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > JDBC > 6 Steps To Execute JDBC Query

JDBC Tutorial

  • Introduction
  • Setting Environment
  • Connecting to a Database
  • Executing Statement
  • Processing Results with ResultSet
  • CRUD Operations
  • Transactions
  • JDBC Best Practices
  • Simple JDBC Project
  • Advanced JDBC Topics
  • Common JDBC Issues
  • Interview Questions

6 Steps To Execute JDBC Query

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 ยท 2 min read ยท 0 Comments
Share: in X

JDBC (Java Database Connectivity) is a Java API that allows Java applications to interact with databases.

Following are the 6 steps to execute JDBC query:

  1. Register the JDBC driver
  2. Open a database connection
  3. Create a statement
  4. Execute the query
  5. Process the result
  6. Close the resources

Table of Contents

  • Register the JDBC driver
  • Open a database connection
  • Create a statement
  • Execute the query
  • Process the result
  • Close the resources
  • Example

Register the JDBC driver

Before we can interact with a database using JDBC, we need to register the JDBC driver. This is typically done using the Class.forName() method, which loads the driver class into memory.

Open a database connection

After the driver is registered, we can use the DriverManager.getConnection() method to establish a connection to the database. This method returns a Connection object that represents the connection to the database.

Create a statement

Once we have a connection to the database, we can create a Statement object using the Connection.createStatement() method. This object represents a SQL statement that we want to execute.

Execute the query

To execute the query, we call one of the Statement.execute*() methods (e.g. executeQuery(), executeUpdate(), etc.) with the SQL statement we want to execute. The method returns a ResultSet object if the query returns data, or an integer representing the number of rows affected if the query is an update, insert or delete.

Process the result

If the query returned data, we can use the ResultSet object to iterate over the rows of data. The ResultSet provides methods to get the values of the columns for each row.

Close the resources

After we finished using the ResultSet, we must close it using the ResultSet.close() method. We can also close the Statement and Connection objects using their close() methods.

Example

The complete example code is given below:

import java.sql.*;

public class JdbcExample {
  public static void main(String[] args) throws SQLException, ClassNotFoundException {
    // Register the JDBC driver
    Class.forName("com.mysql.jdbc.Driver");

    // Open a database connection
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");

    // Create a statement
    Statement stmt = conn.createStatement();

    // Execute the query
    ResultSet rs = stmt.executeQuery("SELECT * FROM mytable");

    // Process the result
    while (rs.next()) {
      int id = rs.getInt("id");
      String name = rs.getString("name");
      System.out.println(id + "\t" + name);
    }

    // Close the resources
    rs.close();
    stmt.close();
    conn.close();
  }
}

Related Posts:

  • Control Statements in Java
  • MySQL Commands for Developers
  • What is Try-With-Resources in Java? A Complete Guide
  • Top 10 Common Java Errors
  • Install and Set Up Java Development Environment
  • JDBC Configuration
Tags:javajdbc
Was this article helpful?
โ† Previous ArticleExecuting Statements with JDBC
Next Article โ†’Thread Priority in Java

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap