import java.sql.*; public class ResultSetExample{ public static void main(String[] args) throws SQLException{ // Connect to the database Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "admin"); // Execute a SQL query Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM students"); // Loop through the results and retrieve the integer value from the 3rd column while(rs.next()){ int age = rs.getInt(3); System.out.println("Age: " + age); } // Close the connection con.close(); } }
import java.sql.*; public class ResultSetExample{ public static void main(String[] args) throws SQLException{ // Connect to the database Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "admin"); // Execute a SQL query Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM students WHERE id = 1"); // Retrieve the integer value from the 3rd column of the first row rs.first(); int age = rs.getInt(3); System.out.println("Age: " + age); // Close the connection con.close(); } }In this example, we connect to a MySQL database, execute a SELECT query on a table named "students" with a condition that the id column value is 1, and retrieve the integer value from the 3rd column of the first row in the table. We then print out the age value to the console. The java.util.ResultSet class is part of the Java Database Connectivity (JDBC) API, which is included in the Java SE development kit.