-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathupdating-large-data-sample_1.java
More file actions
87 lines (65 loc) · 3.5 KB
/
updating-large-data-sample_1.java
File metadata and controls
87 lines (65 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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://<server>:<port>;databaseName=AdventureWorks;user=<user>;password=<password>";
// Establish the connection.
try (Connection con = DriverManager.getConnection(connectionUrl); Statement stmt = con.createStatement();
Statement stmt1 = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);) {
createTable(stmt);
// Since the summaries could be large, we should 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 JDBC Driver for SQL Server
// 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.
try (ResultSet rs = stmt1.executeQuery("SELECT Title, DocumentSummary FROM Document_JDBC_Sample")) {
// Update each document summary.
while (rs.next()) {
// Retrieve the original document summary.
try (Reader 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();
}
}
}
}
}
// Handle any errors that may have occurred.
catch (Exception e) {
e.printStackTrace();
}
}
private static void createTable(Statement stmt) throws SQLException {
stmt.execute("if exists (select * from sys.objects where name = 'Document_JDBC_Sample')"
+ "drop table Document_JDBC_Sample");
String sql = "CREATE TABLE Document_JDBC_Sample (" + "[DocumentID] [int] NOT NULL identity,"
+ "[Title] [char](50) NOT NULL," + "[DocumentSummary] [varchar](max) NULL)";
stmt.execute(sql);
sql = "INSERT Document_JDBC_Sample VALUES ('title1','summary1') ";
stmt.execute(sql);
sql = "INSERT Document_JDBC_Sample (title) VALUES ('title2') ";
stmt.execute(sql);
sql = "INSERT Document_JDBC_Sample (title) VALUES ('title3') ";
stmt.execute(sql);
sql = "INSERT Document_JDBC_Sample VALUES ('title4','summary3') ";
stmt.execute(sql);
}
}