Practical No.
30
3. Write program to retrieve data from table using ResultSet interface. (Use various methods
of navigation methods)
Code
package resultsetnavigationexample;
import [Link];
import [Link];
import [Link];
import [Link];
public class ResultSetNavigationExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/Students";
String username = "root";
String password = "3367";
String selectQuery = "SELECT id, name, age, grade FROM students";
try {
Connection connection =
[Link]("jdbc:mysql://localhost:3306/Students", "root", "3367");
Statement statement = [Link](ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet resultSet = [Link](selectQuery);
[Link]("Navigating the ResultSet...");
if ([Link]()) {
[Link]("First row: " + [Link]("id") + ", " + [Link]("name"));
if ([Link]()) {
[Link]("Last row: " + [Link]("id") + ", " + [Link]("name"));
}
if ([Link](2)) {
[Link]("Second row: " + [Link]("id") + ", " +
[Link]("name"));
if ([Link](-1)) {
[Link]("Previous row relative to current: " + [Link]("id") + ", " +
[Link]("name"));
[Link]("\nIterating through all rows:");
[Link]();
while ([Link]()) {
[Link]([Link]("id") + ", " + [Link]("name") + ", " +
[Link]("age") + ", " + [Link]("grade"));
[Link]();
[Link]();
[Link]();
} catch (Exception e) {
[Link]();
}
Output:-