Home Tutorial Java Jdbc Jdbcresultset JDBC ResultSet first() Example

 
 

JDBC ResultSet first() Example
Posted on: October 20, 2010 at 12:00 AM
The ResultSet first() are use to moves the cursor to the first row in the ResultSet object.

JDBC ResultSet first() Example:

The ResultSet first() are use to moves the cursor to the first row in the ResultSet object. It return true if the cursor pointed first row in the ResultSet and return false if the ResultSet object does not contain any row.

Syntax:

ResultSet rs;

Boolean rs.first();

Now we are create a example of ResultSet first().

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 FirstMethodExample {

  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.first();
          
          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

Related Tags for JDBC ResultSet first() Example :


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.