Friday, 20 January 2017

RELATIVE IN RESULTSET

boolean relative(int row)
Moves the cursor a relative number of rows, either positive or negative.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class Test14 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "rakesh");
PreparedStatement ps = con.prepareStatement("select * from student_register",ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = ps.executeQuery();
boolean b=rs.first();
Scanner scan = new Scanner(System.in);
while(b)
{
System.out.println("Input row number");
int row = scan.nextInt();
b = rs.relative(row);
if(b==false)
{
System.out.println("Invalid row number");
}
else
{
int id=rs.getInt(1);
String name=rs.getString(2);
String course=rs.getString(3);
System.out.print(id+" "+name+" "+course);
}
}
}
}

Output -
Input row number
0
3 runjun mca
Input row number
1
1 rakesh mca
Input row number
1
2 barsha mca
Input row number
-2
3 runjun mca
Input row number
2
2 barsha mca
Input row number
1

Invalid row number

No comments:

Post a Comment