Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java ResultSet getType Method with Example



In JDBC (Java Database Connectivity), the ResultSet interface represents the result set of a database query in Java. One of the important methods provided by ResultSet is getType(), which returns the type of the ResultSet object.

Result getType() method

The getType() method is used to determine the type of cursor and its behavior when scrolling through the result set.

Syntax of getType()
int resultSet_Type = rs.getType();

This method returns an integer value that corresponds to one of the ResultSet type constants. These constants indicate the type of cursor used by the ResultSet:

  • ResultSet.TYPE_FORWARD_ONLY (value: 1003): This is the default cursor type. The result set is only scrollable in a forward direction.
  • ResultSet.TYPE_SCROLL_INSENSITIVE (value: 1004): The result set can be scrolled in both directions, but it does not reflect changes made to the database while the result set is open.
  • ResultSet.TYPE_SCROLL_SENSITIVE (value: 1005): The result set can be scrolled in both directions and reflects changes made to the database during the result set's lifetime.

Let us create a table with the name MyPlayers in the MySQL database using the CREATE statement as shown below ?

CREATE TABLE MyPlayers(
   ID INT,
   First_Name VARCHAR(255),
   Last_Name VARCHAR(255),
   Date_Of_Birth date,
   Place_Of_Birth VARCHAR(255),
   Country VARCHAR(255),
   PRIMARY KEY (ID)
   );

Now, we will insert 7 records in the MyPlayers table using INSERT statements ?

insert into MyPlayers values(1, 'Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India');
insert into MyPlayers values(2, 'Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica');
insert into MyPlayers values(3, 'Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka');
insert into MyPlayers values(4, 'Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India');
insert into MyPlayers values(5, 'Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India');
insert into MyPlayers values(6, 'Ravindra', 'Jadeja', DATE('1988-12-06'), 'Nagpur', 'India');
insert into MyPlayers values(7, 'James', 'Anderson', DATE('1982-06-30'), 'Burnley', 'England');

Following are the steps toretrieve the MyPlayers table's contents as a ResultSet object and display the type of ResultSet ?

  • Register JDBC Driver: Load and register the MySQL JDBC driver using DriverManager.registerDriver().
  • Establish Database Connection: Use DriverManager.getConnection() to connect to the database.
  • Create Statement: Create a Statement object with ResultSet.TYPE_SCROLL_SENSITIVE and ResultSet.CONCUR_UPDATABLE settings.
  • Execute Query: Execute a SELECT query using stmt.executeQuery() to retrieve data into a ResultSet object.
  • Get ResultSet Type: Call rs.getType() on the ResultSet object to retrieve the cursor type.
  • Display Result: Print the result of getType() to show the type of the ResultSet.
  • Close Connection: Close the database connection using con.close().

Example

Below is an example to retrieve the MyPlayers table's contents as a ResultSet object and display the type of ResultSet ?

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ResultSet_getType {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/mydatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating the Statement
      Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
      //Query to retrieve records
      String query = "Select * from MyPlayers";
      //Executing the query
      ResultSet rs = stmt.executeQuery(query);
      //Retrieving the datatype of the current ResultSet object
      int resultSet_Type = rs.getType();
      System.out.println("Type of the ResultSet object: "+resultSet_Type);
   }
}

Output

Connection established......
Type of the result set: 1005

Conclusion

The getType() method in JDBC helps you identify the cursor type used by the ResultSet. It plays an essential role in understanding how the result set behaves, especially in terms of scrolling and reflecting database changes. 

Updated on: 2025-01-28T14:57:12+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements