import java.sql.*; public class Example { public static void main(String[] args) { try(Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password")) { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM mytable"); while(rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); int age = rs.getInt("age"); System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age); } } catch(SQLException e) { e.printStackTrace(); } } }
import java.sql.*; public class Example { public static void main(String[] args) { try(Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password")) { Statement stmt = conn.createStatement(); int rows = stmt.executeUpdate("UPDATE mytable SET age = 25 WHERE name = 'John'"); System.out.println("Rows updated: " + rows); } catch(SQLException e) { e.printStackTrace(); } } }This example shows how to update data in a database using the ResultSet. The program connects to a MySQL database, creates a statement, and executes a SQL query to update the 'age' column to 25 for all rows in the table 'mytable' where the 'name' column has the value 'John'. The ResultSet is not used in this example as it is not needed for an update operation. The number of rows updated is displayed on the console. Overall, the java.sql.ResultSet interface provides a convenient way to retrieve and process data from a database in a Java program. It is part of the standard library and does not require any additional package or library.