Home Tutorial Java Jdbc Jdbcresultset JDBC ResultSet Example

 
 

Share on Google+Share on Google+
JDBC ResultSet Example
Posted on: October 20, 2010 at 12:00 AM
Advertisement
In this example, we are discuss about ResultSet class that provides methods for access (retrieving, navigating, and manipulating) database query results

JDBC ResultSet Example:

In this example, we are discuss about ResultSet class that provides methods for access (retrieving, navigating, and manipulating) database query results. Through this example you can see how to use ResultSet in the application for accessing data.

First we will create a java class that will have database connectivity after that we will create a Statement object and SQL select statement for retrieving data form the database as:

Statement stmt = connection.createStatement();

String selectquery = "select * from user";

After that we will execute this statement on the created statement object and store the result in the resultset object  as:

ResultSet rs = stmt.executeQuery(selectquery); 

Now we will access data using resultset methods.

The full code of the example is:

package ResultSet;

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

public class ResultSetExample {

  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 * from user";
          ResultSet rs = stmt.executeQuery(selectquery);
          while(rs.next()){
            System.out.print("User ID :" + rs.getInt(1)" ");
              System.out.println("User Name :" + rs.getString(2));
          }
        }
        catch(SQLException s){
          System.out.println(s);
        }
        connection.close();
      }
      catch (Exception e){
        e.printStackTrace();
      }
  }
}

In this example we have used resultset next() method to retrieve data. Now we will run this example using eclipse IDE and see the result.

Program output:

The database table output is:

And the eclipse console output is:

Download source code

JDBC ResultSet first() Example
The ResultSet first() are use to moves the cursor to the first row in the ResultSet object.
 
JDBC ResultSet next() Example
In this example, we are discuss about resultset next() method that moves the cursor forward one row.
 
JDBC ResultSet last() Example
The ResultSet last() are use to moves the cursor to the last row in the ResultSet object.
 
JDBC ResultSet afterLast() Example
This ResultSet method set cursor to after last record. It sets the cursor position to last + 1.
 
JDBC ResultSet beforeFirst() Example
This ResultSet method set cursor to before first record. It sets the cursor position to 0.
 
Rows Count Example Using JDBC ResultSet
In this tutorial you can count the number of rows in the database table using ResultSet. Through this example, you can do it.
 
JDBC ResultSet Forward Type Example
Through this ResultSet type the cursor only move forward in the result set and are non-scrollable.
 
JDBC ResultSet Scroll Insensitive Type Example
Through this ResultSet type the cursor can move in any direction.
 
JDBC ResultSet Scroll Sensitive Type Example
Through this ResultSet type the cursor can move in any direction.
 
Find Default ResultSet Type Example
We are discuss about to find out Default ResultSet type.
 
JDBC ResultSet Delete Row Example
Learn how to delete row using ResultSet. We are also used ResultSet object with update capability for delete rows from database tables.
 
Advertisement

Related Tags for JDBC ResultSet 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