A ResultSet stores the data returned by a SQL query and provides methods to retrieve values from each row. JDBC allows data to be accessed using either the column name or the column index. Column names make the program more readable, while column indices can be useful when processing columns dynamically. The column index starts from 1, not 0.
Program: Accessing ResultSet Data Using Column Names and Indices
This program retrieves employee records from the employee table. It demonstrates how to access the same data using both column names and column indices.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ResultSetColumnAccessExample {
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, " +
"department, salary FROM employee";
try {
// Establish connection
Connection con =
DriverManager.getConnection(
url, username, password);
// Create Statement
Statement stmt =
con.createStatement();
// Execute query
ResultSet rs =
stmt.executeQuery(sql);
System.out.println(
"Employee Details");
System.out.println(
"==============================");
while (rs.next()) {
// Accessing data using column names
int id =
rs.getInt("employee_id");
String name =
rs.getString("employee_name");
// Accessing data using column indices
String department =
rs.getString(3);
double salary =
rs.getDouble(4);
System.out.println(
"Employee ID: " + id);
System.out.println(
"Employee Name: " + name);
System.out.println(
"Department: " + department);
System.out.println(
"Salary: " + salary);
System.out.println(
"------------------------------");
}
// Close resources
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
System.out.println(
"Database Error: " +
e.getMessage());
}
}
}Execution and Output:
PS D:\test> javac -classpath ".;D:\test\mysql-connector.jar" ResultSetColumnAccessExample.java
PS D:\test> java -classpath ".;D:\test\mysql-connector.jar" ResultSetColumnAccessExample
Employee Details
==============================
Employee ID: 101
Employee Name: Ravi
Department: Development
Salary: 88500.0
------------------------------
Employee ID: 102
Employee Name: Priya
Department: Operations
Salary: 71500.0
------------------------------
Employee ID: 106
Employee Name: Chakrapani
Department: Finance
Salary: 204000.0
------------------------------
Employee ID: 107
Employee Name: Suresh
Department: IT
Salary: 46500.0
------------------------------
Employee ID: 108
Employee Name: Anita
Department: HR
Salary: 48500.0
------------------------------
Employee ID: 109
Employee Name: Kiran
Department: Finance
Salary: 52000.0
------------------------------
Employee ID: 110
Employee Name: Sai
Department: IT
Salary: 45000.0
------------------------------
Employee ID: 111
Employee Name: Akhila
Department: HR
Salary: 47000.0
------------------------------
Employee ID: 112
Employee Name: Raghu
Department: Finance
Salary: 52000.0
------------------------------
Employee ID: 119
Employee Name: Deepak
Department: Support
Salary: 36000.0
------------------------------Code language: JavaScript (javascript)
