Home Tutorial Java Jdbc Jdbcresultset JDBC ResultSet beforeFirst() Example

 
 

Share on Google+Share on Google+
JDBC ResultSet beforeFirst() Example
Posted on: October 20, 2010 at 12:00 AM
Advertisement
This ResultSet method set cursor to before first record. It sets the cursor position to 0.

JDBC ResultSet beforeFirst() Example:

This ResultSet method set cursor to before first record. It sets the cursor position to 0. It indicates whether the cursor is before the first row in this ResultSet object.

Syntax:

ResultSet rs;

void rs.beforeFirst();

Now we will create example using this method. In this example, the beforeFirst() point the 0 position in the resultset object.

Example:

package ResultSet;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class BeforeFirstMethodExample {

  public static void main(String[] args) {
    
    Connection connection = null;
      String url = "jdbc:mysql://localhost:3306/";
      String dbName = "roseindia_jdbc_tutorials";
      String driverName = "com.mysql.jdbc.Driver";
      String userName = "root";
      String password = "root";
      try{
        Class.forName(driverName).newInstance();
        connection = DriverManager.getConnection(url+dbName, userName, password);
        try{
          Statement stmt = connection.createStatement();
          String selectquery = "select user_id, user_name from user";
          ResultSet rs = stmt.executeQuery(selectquery);
          
          rs.beforeFirst();
          
          while (rs.next()) {
              System.out.print("User id :" + rs.getInt("user_id"" ");
              System.out.println("User Name :" + rs.getString("user_name"));
          }          
        }
        catch(SQLException s){
          System.out.println(s);
        }
        connection.close();
      }
      catch (Exception e){
        e.printStackTrace();
      }  
  }
}

Now run this example using eclipse IDE and see the output.

Program output:

Database table is:

The eclipse console output is:

Download source code

Advertisement

Related Tags for JDBC ResultSet beforeFirst() Example:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 20, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com