sql absolute Example
sql class absolute example. This example shows you how to use absolute method.
boolean absolute(int row) throws SQLException Moves the cursor to the given row number in this ResultSet object. If the row number is positive, the cursor moves to the given row number with respect to the beginning of the result set. The first row is row 1, the second is row 2, and so on.
/*
* @ # Absolute.java
* A class repersenting use to Absolute method
* of ResultSet class in java.sql package
* version 23 June 2008
* author Rose India
*/
import java.sql.*;
public class Absolute {
public static void main(String[] args) throws Exception {
Connection con = null;
Statement st = null;
ResultSet rs = null;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://192.168.10.59:3306/";
String db = "Student";
Class.forName(driver);
con = DriverManager.getConnection(url + db, "root", "root");
try {
st = con.createStatement();
rs = st.executeQuery("select* from stu_info");
int rowSize = 0;
while (rs.next()) {
rowSize++;
}
System.out.println("Number of Rows in ResultSet is: " + rowSize);
if (rowSize == 0) {
System.out.println("Since there are no rows, exiting...");
System.exit(0);
}
int cursorPosition = Math.round(rowSize / 2);
System.out.println("Moving to position: " + cursorPosition);
rs.absolute(cursorPosition);
System.out.println("Name: " + rs.getString(2));
rs.relative(1);
cursorPosition = rs.getRow();
System.out.println("Moving to position: " + cursorPosition);
System.out.println("Name: " + rs.getString(2));
} catch (SQLException e) {
System.out.println("SQL statement is not executed!");
System.out.println(e.toString());
}
}
} |
Output
Number of Rows in ResultSet is: 5
Moving to position: 2
Name: Ravi
Moving to position: 3
Name: komal singh |
|