Program 1: Retrieve All Employee Records Using ResultSet
This program retrieves all employee records from the employee table. The next() method is used to navigate through each row of the ResultSet.
import java.sql.*;
public class RetrieveAllEmployees {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/jdbc_demo";
String username = "root";
String password = "password";
String sql = "SELECT * FROM employee";
try {
Connection con =
DriverManager.getConnection(url, username, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
System.out.println("Employee Records");
System.out.println("--------------------------------");
while (rs.next()) {
System.out.println(
rs.getInt("employee_id") + " | " +
rs.getString("employee_name") + " | " +
rs.getString("department") + " | " +
rs.getDouble("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" RetrieveAllEmployees.java
PS D:\test> java -classpath ".;D:\test\mysql-connector.jar" RetrieveAllEmployees
Employee Records
--------------------------------
101 | Ravi | IT | 55000.0
102 | Priya | HR | 48000.0
104 | Arun | null | 38000.0
105 | Mahesh | null | 200000.0
106 | Chakrapani | Finance | 200000.0Code language: JavaScript (javascript)
Program 2: Retrieve a Specific Employee Using ResultSet
This program retrieves a specific employee using the employee ID. The ResultSet is checked using the next() method before retrieving the employee details.
import java.sql.*;
public class RetrieveSpecificEmployee {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/jdbc_demo";
String username = "root";
String password = "password";
String sql =
"SELECT * FROM employee WHERE employee_id = 102";
try {
Connection con =
DriverManager.getConnection(url, username, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
System.out.println("Employee ID: " +
rs.getInt("employee_id"));
System.out.println("Employee Name: " +
rs.getString("employee_name"));
System.out.println("Department: " +
rs.getString("department"));
System.out.println("Salary: " +
rs.getDouble("salary"));
} else {
System.out.println("Employee not found.");
}
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}Execution and Output:
PS D:\test> javac -classpath ".;D:\test\mysql-connector.jar" RetrieveSpecificEmployee.java
PS D:\test> java -classpath ".;D:\test\mysql-connector.jar" RetrieveSpecificEmployee
Employee ID: 102
Employee Name: Priya
Department: HR
Salary: 48000.0Code language: CSS (css)
Program 3: Navigate to the First and Last Row Using ResultSet
This program demonstrates cursor navigation using first() and last(). A scrollable ResultSet is required to move directly to the first and last records.
import java.sql.*;
public class FirstLastRowExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/jdbc_demo";
String username = "root";
String password = "password";
String sql = "SELECT * FROM employee";
try {
Connection con =
DriverManager.getConnection(url, username, password);
Statement stmt = con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY
);
ResultSet rs = stmt.executeQuery(sql);
if (rs.first()) {
System.out.println("First Employee");
System.out.println("----------------");
System.out.println(
rs.getInt("employee_id") + " | " +
rs.getString("employee_name"));
}
if (rs.last()) {
System.out.println("\nLast Employee");
System.out.println("----------------");
System.out.println(
rs.getInt("employee_id") + " | " +
rs.getString("employee_name"));
}
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}Execution and Output:
PS D:\test> javac -classpath ".;D:\test\mysql-connector.jar" FirstLastRowExample.java
PS D:\test> java -classpath ".;D:\test\mysql-connector.jar" FirstLastRowExample
First Employee
----------------
101 | Ravi
Last Employee
----------------
106 | ChakrapaniCode language: JavaScript (javascript)
Program 4: Navigate to the Previous Row Using ResultSet
This program demonstrates backward navigation through a ResultSet. The last() method moves to the last record, and previous() moves the cursor to the previous record.
import java.sql.*;
public class PreviousRowExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/jdbc_demo";
String username = "root";
String password = "password";
String sql = "SELECT * FROM employee";
try {
Connection con =
DriverManager.getConnection(url, username, password);
Statement stmt = con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY
);
ResultSet rs = stmt.executeQuery(sql);
if (rs.last()) {
System.out.println("Last Employee");
System.out.println(
rs.getInt("employee_id") + " | " +
rs.getString("employee_name"));
}
if (rs.previous()) {
System.out.println("\nPrevious Employee");
System.out.println(
rs.getInt("employee_id") + " | " +
rs.getString("employee_name"));
}
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}Execution and Output:
PS D:\test> javac -classpath ".;D:\test\mysql-connector.jar" PreviousRowExample.java
PS D:\test> java -classpath ".;D:\test\mysql-connector.jar" PreviousRowExample
Last Employee
106 | Chakrapani
Previous Employee
105 | MaheshCode language: JavaScript (javascript)
Program 5: Navigate to a Specific Row Using absolute()
This program moves the ResultSet cursor directly to a specific row. The absolute() method is used to move the cursor to the required row number.
import java.sql.*;
public class AbsoluteRowExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/jdbc_demo";
String username = "root";
String password = "password";
String sql = "SELECT * FROM employee";
try {
Connection con =
DriverManager.getConnection(url, username, password);
Statement stmt = con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY
);
ResultSet rs = stmt.executeQuery(sql);
if (rs.absolute(3)) {
System.out.println("Third Employee");
System.out.println("----------------");
System.out.println(
"Employee ID: " +
rs.getInt("employee_id"));
System.out.println(
"Employee Name: " +
rs.getString("employee_name"));
System.out.println(
"Department: " +
rs.getString("department"));
System.out.println(
"Salary: " +
rs.getDouble("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" AbsoluteRowExample.java
PS D:\test> java -classpath ".;D:\test\mysql-connector.jar" AbsoluteRowExample
Third Employee
----------------
Employee ID: 104
Employee Name: Arun
Department: null
Salary: 38000.0Code language: CSS (css)
Program 6: Update Employee Salary Using ResultSet
This program updates an employee’s salary directly through an updatable ResultSet. The updateDouble() method changes the salary, and updateRow() saves the change to the database.
import java.sql.*;
public class ResultSetUpdateSalary {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/jdbc_demo";
String username = "root";
String password = "password";
String sql =
"SELECT * FROM employee";
try {
Connection con =
DriverManager.getConnection(url, username, password);
Statement stmt = con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE
);
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
if (rs.getInt("employee_id") == 101) {
System.out.println("Old Salary: " +
rs.getDouble("salary"));
rs.updateDouble("salary", 56000.00);
rs.updateRow();
System.out.println("New Salary: " +
rs.getDouble("salary"));
break;
}
}
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}Execution and Output:
PS D:\test> javac -classpath ".;D:\test\mysql-connector.jar" ResultSetUpdateSalary.java
PS D:\test> java -classpath ".;D:\test\mysql-connector.jar" ResultSetUpdateSalary
Old Salary: 55000.0
New Salary: 56000.0Code language: CSS (css)
Program 7: Update Employee Department Using ResultSet
This program updates the department of an employee using an updatable ResultSet. The updateString() method changes the department and updateRow() saves the change.
import java.sql.*;
public class ResultSetUpdateDepartment {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/jdbc_demo";
String username = "root";
String password = "password";
String sql =
"SELECT * FROM employee";
try {
Connection con =
DriverManager.getConnection(url, username, password);
Statement stmt = con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE
);
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
if (rs.getInt("employee_id") == 104) {
System.out.println("Old Department: " +
rs.getString("department"));
rs.updateString("department", "HR");
rs.updateRow();
System.out.println("New Department: " +
rs.getString("department"));
break;
}
}
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}Execution and Output:
PS D:\test> javac -classpath ".;D:\test\mysql-connector.jar" ResultSetUpdateDepartment.java
PS D:\test> java -classpath ".;D:\test\mysql-connector.jar" ResultSetUpdateDepartment
Old Department: null
New Department: HRCode language: PHP (php)
Program 8: Retrieve Employees Based on Salary
This program retrieves employees whose salary is greater than 40000. The ResultSet is processed using the next() method, and matching employee details are displayed.
import java.sql.*;
public class SalaryBasedResultSet {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/jdbc_demo";
String username = "root";
String password = "password";
String sql =
"SELECT * FROM employee " +
"WHERE salary > 40000";
try {
Connection con =
DriverManager.getConnection(url, username, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
System.out.println("Employees with salary greater than 40000");
System.out.println("----------------------------------------");
while (rs.next()) {
System.out.println(
rs.getInt("employee_id") + " | " +
rs.getString("employee_name") + " | " +
rs.getDouble("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" SalaryBasedResultSet.java
PS D:\test> java -classpath ".;D:\test\mysql-connector.jar" SalaryBasedResultSet
Employees with salary greater than 40000
----------------------------------------
101 | Ravi | 56000.0
102 | Priya | 48000.0
105 | Mahesh | 200000.0
106 | Chakrapani | 200000.0Code language: JavaScript (javascript)
Program 9: Count and Display the Number of Rows Using ResultSet
This program uses a scrollable ResultSet to determine the number of employee records. The last() method moves to the final row, and getRow() returns the current row number.
import java.sql.*;
public class ResultSetRowCount {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/jdbc_demo";
String username = "root";
String password = "password";
String sql = "SELECT * FROM employee";
try {
Connection con =
DriverManager.getConnection(url, username, password);
Statement stmt = con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY
);
ResultSet rs = stmt.executeQuery(sql);
if (rs.last()) {
int count = rs.getRow();
System.out.println(
"Total number of employees: " + count);
}
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}Execution and Output:
PS D:\test> javac -classpath ".;D:\test\mysql-connector.jar" ResultSetRowCount.java
PS D:\test> java -classpath ".;D:\test\mysql-connector.jar" ResultSetRowCount
Total number of employees: 5Code language: CSS (css)
