此 Microsoft SQL Server JDBC Driver 示例应用程序说明了如何更新数据库中的大型列。
此示例的代码文件名为 updateLargeData.java,该文件可在以下位置找到:
<安装目录>\sqljdbc_<版本>\<语言>\help\samples\adaptive
要求
要运行此示例应用程序,需要访问 SQL Server 2005 AdventureWorks 示例数据库。还必须将 classpath 设置为包含 sqljdbc4.jar 文件。如果 classpath 缺少 sqljdbc4.jar 项,示例应用程序将引发“找不到类”的常见异常。有关如何设置 classpath 的详细信息,请参阅使用 JDBC 驱动程序。
示例
在下面的示例中,示例代码建立到 SQL Server 2005 AdventureWorks 数据库的连接。接下来,示例代码创建一个 Statement 对象并使用 isWrapperFor 方法来检查 Statement 对象是否是指定的 SQLServerStatement 类的包装。unwrap 方法用于访问特定于驱动程序的响应缓冲方法。
接下来,示例代码使用 SQLServerStatement 类的 setResponseBuffering 方法将响应缓冲模式设置为“adaptive”,并演示如何获取自适应缓冲模式。
然后,它运行 SQL 语句,并将返回的数据放入可更新的 SQLServerResultSet 对象中。
最后,示例代码将循环访问结果集中包含的数据行。如果找到空的文档摘要,将结合使用 updateString 和 updateRow 方法来更新数据行,并再次将它保存到数据库中。如果已有数据,将使用 getString 方法来显示所包含的部分数据。
请注意,从 JDBC Driver 2.0 发行版开始,驱动程序的默认行为改为了“adaptive.”。但是,对于只进的可更新结果集,以及当结果集中的数据大于应用程序内存时,应用程序必须使用 SQLServerStatement 类的 setResponseBuffering 方法显式设置自适应缓冲模式。
import java.sql.*; import java.io.*; import com.microsoft.sqlserver.jdbc.SQLServerStatement; public class updateLargeData { public static void main(String[] args) { // Create a variable for the connection string. String connectionUrl = "jdbc:sqlserver://localhost:1433;" + "databaseName=AdventureWorks;integratedSecurity=true;"; // Declare the JDBC objects. Connection con = null; Statement stmt = null; ResultSet rs = null; Reader reader = null; try { // Establish the connection. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); con = DriverManager.getConnection(connectionUrl); stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); // Since the summaries could be large, make sure that // the driver reads them incrementally from a database, // even though a server cursor is used for the updatable result sets. // The recommended way to access the Microsoft SQL Server JDBC Driver // specific methods is to use the JDBC 4.0 Wrapper functionality. // The following code statement demonstrates how to use the // Statement.isWrapperFor and Statement.unwrap methods // to access the driver specific response buffering methods. if (stmt.isWrapperFor(com.microsoft.sqlserver.jdbc.SQLServerStatement.class)) { SQLServerStatement SQLstmt = stmt.unwrap(com.microsoft.sqlserver.jdbc.SQLServerStatement.class); SQLstmt.setResponseBuffering("adaptive"); System.out.println("Response buffering mode has been set to " + SQLstmt.getResponseBuffering()); } // Select all of the document summaries. rs = stmt.executeQuery("SELECT Title, DocumentSummary FROM Production.Document"); // Update each document summary. while (rs.next()) { // Retrieve the original document summary. reader = rs.getCharacterStream("DocumentSummary"); if (reader == null) { // Update the document summary. System.out.println("Updating " + rs.getString("Title")); rs.updateString("DocumentSummary", "Work in progress"); rs.updateRow(); } else { // Do something with the chunk of the data that was // read. System.out.println("reading " + rs.getString("Title")); reader.close(); reader = null; } } } // Handle any errors that may have occurred. catch (Exception e) { e.printStackTrace(); } finally { if (reader != null) try { reader.close(); } catch(Exception e) {} 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) {} } } }