0% found this document useful (0 votes)
7 views2 pages

JPR Manual Answers Experiment 30

The document provides a Java program that retrieves data from a 'Student' table using the ResultSet interface. It demonstrates various navigation methods such as next(), absolute(), relative(), first(), last(), and previous() to access and display student records. The program connects to a MySQL database and handles SQL exceptions appropriately.

Uploaded by

Riya Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

JPR Manual Answers Experiment 30

The document provides a Java program that retrieves data from a 'Student' table using the ResultSet interface. It demonstrates various navigation methods such as next(), absolute(), relative(), first(), last(), and previous() to access and display student records. The program connects to a MySQL database and handles SQL exceptions appropriately.

Uploaded by

Riya Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Practical No.

: 30

Program Statement: Write program to retrieve data from table using ResultSet interface.
(Use various methods of navigation methods)

Source Code:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class RetrieveStudentData {


public static void main(String[] args) {
String jdbcURL = "jdbc:mysql://localhost:3306/newdb";
String dbUser = "root";
String dbPassword = "riya@3227#vp;";
try (Connection connection = [Link](jdbcURL, dbUser,
dbPassword);
Statement statement =
[Link](ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet resultSet = [Link]("SELECT * FROM Student"))
{
[Link]("Data from Student Table:");
[Link]("\nUsing next():");
while ([Link]()) {
int id = [Link]("id");
String name = [Link]("name");
int age = [Link]("age");
String email = [Link]("email");
[Link]("ID: " + id + ", Name: " + name + ", Age: " + age + ",
Email: " + email);
}
[Link]("\nUsing absolute(2):");
if ([Link](2)) {
int id = [Link]("id");
String name = [Link]("name");
int age = [Link]("age");
String email = [Link]("email");
[Link]("ID: " + id + ", Name: " + name + ", Age: " + age + ",
Email: " + email);
} else {
[Link]("Row 2 not found.");
}
[Link]("\nUsing relative(-1):");
if ([Link](-1)) {
int id = [Link]("id");
String name = [Link]("name");
int age = [Link]("age");
String email = [Link]("email");
[Link]("ID: " + id + ", Name: " + name + ", Age: " + age + ",
Email: " + email);
} else {
[Link]("Relative position not found.");
}
[Link]("\nUsing first() and last():");
if ([Link]()) {
[Link]("First row: ID: " + [Link]("id") + ", Name: " +
[Link]("name"));
}
if ([Link]()) {
[Link]("Last row: ID: " + [Link]("id") + ", Name: " +
[Link]("name"));
}
[Link]("\nusing previous():");
if ([Link]()){
[Link]("Previous Row: ID: " + [Link]("id") + ", Name:
" + [Link]("name"));
} else {
[Link]("No previous row.");
}
} catch (SQLException e) {
[Link]();
}
}
}

Output:

You might also like