Redigera

Dela via


Managing result sets with the JDBC driver

Download JDBC driver

The result set is an object that represents a set of data returned from a data source, usually as the result of a query. The result set contains rows and columns to hold the requested data elements, and it is navigated with a cursor. A result set can be updatable, meaning that it can be modified and have those modifications pushed to the original data source. A result set can also have various levels of sensitivity to changes in the underlying data source.

The type of result set is determined when a statement is created, which is when a call to the createStatement method of the SQLServerConnection class is made. The fundamental role of a result set is to provide Java applications with a usable representation of the database data. This task is typically done with the typed getter and setter methods on the result set data elements.

In the following example, which is based on the AdventureWorks2022 sample database, a result set is created by calling the executeQuery method of the SQLServerStatement class. Data from the result set is then displayed by using the getString method of the SQLServerResultSet class.

public static void executeStatement(Connection con){
    try(Statement stmt = con.createStatement();) {
        String SQL = "SELECT TOP 10 * FROM Person.Contact";
        ResultSet rs = stmt.executeQuery(SQL);

        while (rs.next()) {
            System.out.println(rs.getString("FirstName") + " " + rs.getString("LastName"));
        }
    }
    // Handle any errors that may have occurred.
    catch (SQLException e) {
        e.printStackTrace();
    }
}

The articles in this section describe various aspects of result set usage, including cursor types, concurrency, and row locking.

In this section

Article Description
Understanding cursor types Describes the different cursor types that the Microsoft JDBC Driver for SQL Server supports.
Understanding concurrency control Describes how the JDBC driver supports concurrency control.
Understanding row locking Describes how the JDBC driver supports row locking.

See also

Overview of the JDBC driver