Share on Google+Share on Google+

JDBC ResultSet first() Example

Advertisement
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

Advertisements

Advertisement

Posted on: October 20, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial 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.