ResultSet beforeFirst Example

ResultSet class beforeFirst example. This example shows you how to use beforeFirst method.


ResultSet class beforeFirst example. void beforeFirst() throws SQLException Moves the cursor to the front of this ResultSet object, just before the first row. This method has no effect if the result set contains no rows.

Here is the code
/*
 * @ # BeforeFirst.java
 * A class repersenting use to BeforeFirst method 
 * of ResultSet Interface in java.sql package
 * version 23 June 2008
 * author Rose India 
 */

import java.sql.*;

public class BeforeFirst {

  public static void main(String[] argsthrows 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 cursorPosition = rs.getRow();
      rs.absolute(cursorPosition + 1);

      cursorPosition = rs.getRow();
      System.out.println("Moving to position: " + cursorPosition);
      System.out.println("name " + rs.getString(2));

      rs.beforeFirst();

      cursorPosition = rs.getRow();
      System.out.println("Moving to position: " + cursorPosition);
      System.out.println("name " + rs.getString(2));

    catch (Exception e) {
      System.out.println(e.toString());
    }
  }
}

Output
Moving to position: 1
name Mahendra
Moving to position: 0
java.sql.SQLException: Before start of result set

Post Comment
Name:
E-mail:
Contact no :
Comments:
  Refresh Image
Verify Image:
 
 
Your Comment's
 
 
 

HOME | COPYRIGHT | CONTACT US