分享方式:


讀取大型資料範例

下載 JDBC 驅動程式

此 Microsoft JDBC Driver for SQL Server 範例應用程式會示範如何使用 getCharacterStream 方法,從 SQL Server 資料庫中擷取大型單一資料行值。

此範例的程式碼檔案名稱為 ReadLargeData.java,可在下列位置找到:

\<installation directory>\sqljdbc_<version>\<language>\samples\adaptive

需求

若要執行此應用程式範例,您必須存取 AdventureWorks2022 範例資料庫。 您也必須將 Classpath 設定為包含 mssql-jdbc jar 檔案。 如需如何設定 classpath 的詳細資訊,請參閱使用 JDBC 驅動程式

注意

適用於 SQL Server 的 Microsoft JDBC 驅動程式提供 mssql-jdbc 類別庫檔案,可根據您慣用的 Java Runtime Environment (JRE) 設定使用。 如需選擇哪個 JAR 檔案的詳細資訊,請參閱 JDBC Driver 的系統需求

範例

在下列範例中,範例程式碼會建立與 AdventureWorks2022 範例資料庫的連線。 接著,範例程式碼會建立範例資料,並使用參數化查詢更新 Production.Document 資料表。

此外,範例程式碼示範如何使用 SQLServerStatement 類別的 getResponseBuffering 方法來取得自適性緩衝模式。 請注意,從 JDBC Driver 2.0 版開始,responseBuffering 連接屬性預設設定為 "adaptive"。

然後,搭配 SQLServerStatement 物件使用 SQL 陳述式時,範例程式碼會執行 SQL 陳述式,並且將所傳回的資料放入 SQLServerResultSet 物件中。

最後,範例程式碼會逐一查看結果集中的資料列,然後使用 getCharacterStream 方法來存取部分資料。

import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.microsoft.sqlserver.jdbc.SQLServerStatement;

public class ReadLargeData {

    public static void main(String[] args) {
        // Create a variable for the connection string.
        String connectionUrl = "jdbc:sqlserver://<server>:<port>;databaseName=AdventureWorks;user=<user>;password=<password>";
        
        // Create test data as an example.
        StringBuffer buffer = new StringBuffer(4000);
        for (int i = 0; i < 4000; i++)
            buffer.append((char) ('A'));
        
        try (Connection con = DriverManager.getConnection(connectionUrl);
                Statement stmt = con.createStatement();
                PreparedStatement pstmt = con.prepareStatement("UPDATE Production.Document SET DocumentSummary = ? WHERE (DocumentID = 1)");) {

            pstmt.setString(1, buffer.toString());
            pstmt.executeUpdate();

            // In adaptive mode, the application does not have to use a server cursor
            // to avoid OutOfMemoryError when the SELECT statement produces very large
            // results.

            // Create and execute an SQL statement that returns some data.
            String SQL = "SELECT Title, DocumentSummary FROM Production.Document";

            // Display the response buffering mode.
            SQLServerStatement SQLstmt = (SQLServerStatement) stmt;
            System.out.println("Response buffering mode is: " + SQLstmt.getResponseBuffering());
            SQLstmt.close();

            // Get the updated data from the database and display it.
            ResultSet rs = stmt.executeQuery(SQL);

            while (rs.next()) {
                Reader reader = rs.getCharacterStream(2);
                if (reader != null) {
                    char output[] = new char[40];
                    while (reader.read(output) != -1) {
                        // Do something with the chunk of the data that was
                        // read.
                    }

                    System.out.println(rs.getString(1) + " has been accessed for the summary column.");
                    // Close the stream.
                    reader.close();
                }
            }
        }
        // Handle any errors that may have occurred.
        catch (SQLException | IOException e) {
            e.printStackTrace();
        }
    }
}

另請參閱

使用大型資料