此 Microsoft SQL Server 2005 JDBC Driver 示例应用程序演示如何在连接 URL 中和在语句级别设置自适应缓冲模式。它还说明如何使用 getCharacterStream 方法从 SQL Server 数据库中检索大的单列值。
此示例的代码文件命名为 useAdaptiveBuffering.java,可以在以下位置找到:
<安装目录>\sqljdbc_<版本>\<语言>\help\samples\adaptive
要求
若要运行此示例应用程序,必须将 classpath 设置为包含 sqljdbc.jar 文件。如果 classpath 缺少 sqljdbc.jar 项,示例应用程序将引发“找不到类”的常见异常。还需要访问 SQL Server 2005 AdventureWorks 示例数据库。
有关如何设置 classpath 的详细信息,请参阅使用 JDBC 驱动程序。
示例
在下面的示例中,示例代码在连接 URL 中设置 responseBuffering 连接属性和其他不同的连接属性,然后与 SQL Server 2005 AdventureWorks 数据库建立连接。接下来,示例代码创建示例数据并使用参数化查询更新 Production.Document 表。
此外,示例代码还演示如何使用 SQLServerStatement 类的 setResponseBuffering 方法设置自适应缓冲模式。请注意,如果您在连接属性中尚未设置 responseBuffering=adaptive,则只得调用此办法。
然后,对于 SQLServerStatement 对象使用一条 SQL 语句,示例代码将运行此 SQL 语句并将其返回的数据放入 SQLServerResultSet 对象中。
最后,示例代码将迭代遍历结果集中所包含的数据行,并使用 getCharacterStream 方法访问它所包含的某些数据。
import java.sql.*;
import java.io.*;
import com.microsoft.sqlserver.jdbc.SQLServerStatement;
public class useAdaptiveBuffering {
public static void main(String[] args) {
// Create a variable for the connection string.
String connectionUrl =
"jdbc:sqlserver://localhost:1433;" +
"databaseName=AdventureWorks;integratedSecurity=true;" +
"responseBuffering=adaptive";
// Declare the JDBC objects.
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
// Establish the connection.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
// Create test data as an example.
StringBuffer buffer = new StringBuffer(4000);
for (int i = 0; i < 4000; i++)
buffer.append( (char) ('A'));
PreparedStatement pstmt = con.prepareStatement(
"UPDATE Production.Document " +
"SET DocumentSummary = ? WHERE (DocumentID = 1)");
pstmt.setString(1, buffer.toString());
pstmt.executeUpdate();
pstmt.close();
// 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";
stmt = con.createStatement();
// If you have not already set the responseBuffering=adaptive in the
// connection properties, you can set the response buffering to adaptive
// on the statement level before executing the query, such as:
SQLServerStatement SQLstmt = (SQLServerStatement) stmt;
SQLstmt.setResponseBuffering("adaptive");
// Display the response buffering mode.
System.out.println("Response buffering mode has been set to " +
SQLstmt.getResponseBuffering());
// Get the updated data from the database and display it.
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 (Exception e) {
e.printStackTrace();
}
finally {
if (rs != null) try { rs.close(); } catch(Exception e) {}
if (stmt != null) try { stmt.close(); } catch(Exception e) {}
if (con != null) try { con.close(); } catch(Exception e) {}
}
}
}