// Define a new SQL SELECT statement.
String sql_string = "SELECT column1, column2 FROM SampleTable";
// Create a new PreparedStatement from an existing connection.
PreparedStatement ps = conn.prepareStatement(sql_string);
// Create a new ResultSet to contain the query results of the SQL statement.
ResultSet rs = ps.executeQuery();
// Check if the PreparedStatement contains a ResultSet.
if (ps.hasResultSet()) {
// Retrieve the column1 value from the first row using getString.
String row1_col1 = rs.getString(1);
// Get the next row in the table.
if (rs.next()) {
// Retrieve the value of column1 from the second row.
String row2_col1 = rs.getString(1);
}
}
rs.close();
ps.close();