Handling Query Results

Handling Query Results in JDBC means processing the data returned by a database after executing a SELECT query. The executeQuery() method returns a ResultSet object containing the retrieved records. The next() method is used to move through the records one row at a time, while getter methods retrieve column values. After processing the results, the ResultSet, Statement or PreparedStatement, and Connection should be closed.

Common ResultSet Methods

Method Description
next() Moves the cursor to the next row in the result set.
getInt() Retrieves an integer value from a column.
getString() Retrieves a string value from a column.
getDouble() Retrieves a double value from a column.
getBoolean() Retrieves a Boolean value from a column.
getObject() Retrieves a column value as an Object.
close() Closes the ResultSet.

Program 1: Handling a Single Query Result

This program retrieves a specific employee from the database using its ID. The ResultSet object is used to check whether a record exists and retrieve its column values.

import java.sql.*;
import java.util.Scanner;

public class SingleQueryResultExample {

    public static void main(String[] args) {

        String url = "jdbc:mysql://localhost:3306/jdbc_demo";
        String username = "root";
        String password = "password";

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter employee ID: ");
        int id = sc.nextInt();

        String sql =
            "SELECT employee_id, employee_name, salary FROM employee WHERE employee_id = ?";

        try {
            Connection con =
                DriverManager.getConnection(url, username, password);

            PreparedStatement pstmt = con.prepareStatement(sql);

            pstmt.setInt(1, id);

            ResultSet rs = pstmt.executeQuery();

            if (rs.next()) {
                System.out.println("Employee ID: " +
                        rs.getInt("employee_id"));

                System.out.println("Employee Name: " +
                        rs.getString("employee_name"));

                System.out.println("Employee Salary: " +
                        rs.getDouble("salary"));
            } else {
                System.out.println("Employee not found.");
            }

            rs.close();
            pstmt.close();
            con.close();

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

        sc.close();
    }
}
Execution and Output:

PS D:\test> javac -classpath ".;D:\test\mysql-connector.jar" SingleQueryResultExample.java
PS D:\test> java -classpath ".;D:\test\mysql-connector.jar" SingleQueryResultExample
Enter employee ID: 105
Employee ID: 105
Employee Name: Mahesh
Employee Salary: 200000.0Code language: CSS (css)

Program 2: Handling Multiple Query Results

This program retrieves all employees from the employee table. The while loop with rs.next() processes each row and displays the employee details.

import java.sql.*;

public class MultipleQueryResultsExample {

    public static void main(String[] args) {

        String url = "jdbc:mysql://localhost:3306/jdbc_demo";
        String username = "root";
        String password = "password";

        String sql =
            "SELECT employee_id, employee_name, salary FROM employee";

        try {
            Connection con =
                DriverManager.getConnection(url, username, password);

            Statement stmt = con.createStatement();

            ResultSet rs = stmt.executeQuery(sql);

            System.out.println("Employee Details");
            System.out.println("-------------------------");

            while (rs.next()) {

                int id = rs.getInt("employee_id");
                String name = rs.getString("employee_name");
                double salary = rs.getDouble("salary");

                System.out.println(
                    id + " | " + name + " | " + salary
                );
            }

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

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
Execution and Output:

PS D:\test> javac -classpath ".;D:\test\mysql-connector.jar" MultipleQueryResultsExample.java
PS D:\test> java -classpath ".;D:\test\mysql-connector.jar" MultipleQueryResultsExample
Employee Details
-------------------------
101 | Ravi | 50000.0
102 | Priya | 42000.0
103 | Arjun | 50000.0
104 | Arun | 38000.0
105 | Mahesh | 200000.0Code language: JavaScript (javascript)

Program 3: Handling Query Results with Column Names

This program retrieves employee details and accesses the result using column names. Using column names with ResultSet getter methods makes the code easier to understand and maintain.

import java.sql.*;

public class ResultSetColumnNameExample {

    public static void main(String[] args) {

        String url = "jdbc:mysql://localhost:3306/jdbc_demo";
        String username = "root";
        String password = "password";

        String sql =
            "SELECT employee_id, employee_name, salary FROM employee";

        try {
            Connection con =
                DriverManager.getConnection(url, username, password);

            Statement stmt = con.createStatement();

            ResultSet rs = stmt.executeQuery(sql);

            while (rs.next()) {

                System.out.println("ID     : " +
                        rs.getInt("employee_id"));

                System.out.println("Name   : " +
                        rs.getString("employee_name"));

                System.out.println("Salary : " +
                        rs.getDouble("salary"));

                System.out.println("-------------------");
            }

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

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
Execution and Output:

PS D:\test> javac -classpath ".;D:\test\mysql-connector.jar" ResultSetColumnNameExample.java
PS D:\test> java -classpath ".;D:\test\mysql-connector.jar" ResultSetColumnNameExample
ID     : 101
Name   : Ravi
Salary : 50000.0
-------------------
ID     : 102
Name   : Priya
Salary : 42000.0
-------------------
ID     : 103
Name   : Arjun
Salary : 50000.0
-------------------
ID     : 104
Name   : Arun
Salary : 38000.0
-------------------
ID     : 105
Name   : Mahesh
Salary : 200000.0
-------------------Code language: CSS (css)
Scroll to Top