Sobes.tech
Junior — Middle

What methods or modes of data retrieval are you familiar with in the ResultSet object?

sobes.tech AI

Answer from AI

In the ResultSet object in Java, methods are used to retrieve data from the SQL query result, allowing values to be extracted by column name or column index. The main data retrieval methods are:

  • getString(int columnIndex) or getString(String columnLabel) — to get the value as a string.
  • getInt(int columnIndex) or getInt(String columnLabel) — to get an integer value.
  • getLong(), getDouble(), getBoolean(), and other methods for retrieving data of corresponding types.

Data retrieval modes:

  • By column index (starting from 1).
  • By column name (from the SQL query).

Example:

ResultSet rs = statement.executeQuery("SELECT id, name FROM users");
while (rs.next()) {
    int id = rs.getInt("id");
    String name = rs.getString(2); // second column
    System.out.println(id + ": " + name);
}