Share on Google+Share on Google+

JDBC ResultSet next() Example

Advertisement
In this example, we are discuss about resultset next() method that moves the cursor forward one row.

JDBC ResultSet next() Example:

In this example, we are discuss about resultset next() method that moves the cursor forward one row. It returns true if the move succeeds and the now-current row of data exists and false if the cursor is positioned after the last row.

Syntax:

ResultSet rs;

Boolean rs.next();

Now the following code snippet show the use of next().

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

  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_name from user";
          ResultSet rs = stmt.executeQuery(selectquery);
          while(rs.next()){
            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:

Program 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.