Steps for Executing SQL Queries in Java: A Complete Guide

8/16/2025

#Steps for Executing SQL Queries in Java: A Complete Guide

Go Back

Steps for Executing SQL Queries in Java: A Complete Guide


Introduction

When working with databases in Java, executing SQL queries is one of the most fundamental operations. Java provides JDBC (Java Database Connectivity), which allows developers to run SQL commands such as SELECT, INSERT, UPDATE, and DELETE directly from Java programs.

In this article, we’ll go through the step-by-step process of executing SQL queries in Java with examples.


#Steps for Executing SQL Queries in Java: A Complete Guide

Steps for Executing SQL Queries in Java

Step 1: Import JDBC Packages

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

Step 2: Load and Register JDBC Driver

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

Step 3: Establish Database Connection

Connection con = DriverManager.getConnection(
    "jdbc:mysql://localhost:3306/testdb", "root", "password");

Step 4: Create Statement

Statement stmt = con.createStatement();

Step 5: Execute SQL Queries

  • SELECT Query Example:

ResultSet rs = stmt.executeQuery("SELECT * FROM users");
while (rs.next()) {
    System.out.println(rs.getInt(1) + " " + rs.getString(2));
}
  • INSERT Query Example:

int rowsInserted = stmt.executeUpdate("INSERT INTO users (id, name) VALUES (1, 'John')");
System.out.println(rowsInserted + " row(s) inserted.");
  • UPDATE Query Example:

int rowsUpdated = stmt.executeUpdate("UPDATE users SET name='Mike' WHERE id=1");
System.out.println(rowsUpdated + " row(s) updated.");
  • DELETE Query Example:

int rowsDeleted = stmt.executeUpdate("DELETE FROM users WHERE id=1");
System.out.println(rowsDeleted + " row(s) deleted.");

Step 6: Close the Connection

con.close();

Complete Example: SQL Execution in Java

import java.sql.*;

public class SQLExecutionDemo {
    public static void main(String[] args) {
        try {
            // Step 1: Load Driver
            Class.forName("com.mysql.cj.jdbc.Driver");

            // Step 2: Establish Connection
            Connection con = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/testdb", "root", "password");

            // Step 3: Create Statement
            Statement stmt = con.createStatement();

            // Step 4: Execute SELECT Query
            ResultSet rs = stmt.executeQuery("SELECT * FROM users");
            while (rs.next()) {
                System.out.println(rs.getInt(1) + " " + rs.getString(2));
            }

            // Step 5: Execute INSERT Query
            stmt.executeUpdate("INSERT INTO users (id, name) VALUES (2, 'Alice')");

            // Step 6: Close Connection
            con.close();

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

Best Practices

  • Use PreparedStatement instead of Statement for better performance and SQL injection prevention.

  • Always close ResultSet, Statement, and Connection.

  • Handle exceptions using try-catch-finally.

  • Use connection pooling for large-scale applications.


Conclusion

Executing SQL queries in Java is straightforward with JDBC. Whether you need to fetch records, insert data, update tables, or delete records, JDBC provides a standard way to interact with databases. By following best practices, you can build secure and scalable database-driven Java applications.


🔑 Target SEO Keywords:
Execute SQL in Java, JDBC SQL query, SQL SELECT Java, SQL INSERT Java, SQL UPDATE Java, SQL DELETE Java, Java JDBC tutorial, SQL query execution in Java.

Table of content