ResultSet rs = statement.executeQuery("SELECT name FROM employees WHERE id = 1"); if (rs.next()) { String name = (String) rs.getObject("name"); System.out.println("Name: " + name); }
ResultSet rs = statement.executeQuery("SELECT salary FROM employees WHERE id = 2"); if (rs.next()) { double salary = (Double) rs.getObject("salary"); System.out.println("Salary: " + salary); }In both examples, the method rs.getObject() is used to retrieve the value of the specified column in the current row of the ResultSet object. The returned Object is casted to the appropriate data type to be used in the application code. The java.util package library is used in both examples to access the ResultSet and Statement classes.