Skip to content

Commit e1a480a

Browse files
committed
Bringing even with master.
2 parents a693b5e + 5c398ec commit e1a480a

25 files changed

Lines changed: 1013 additions & 432 deletions

docs/connect/jdbc/code-samples/basic-data-types-sample.md

Lines changed: 60 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Basic Data Types Sample | Microsoft Docs"
33
ms.custom: ""
4-
ms.date: "07/11/2018"
4+
ms.date: "07/31/2018"
55
ms.prod: sql
66
ms.prod_service: connectivity
77
ms.reviewer: ""
@@ -16,53 +16,37 @@ ms.author: genemi
1616
manager: craigg
1717
---
1818
# Basic Data Types Sample
19+
1920
[!INCLUDE[Driver_JDBC_Download](../../../includes/driver_jdbc_download.md)]
2021

21-
This [!INCLUDE[jdbcNoVersion](../../../includes/jdbcnoversion_md.md)] sample application demonstrates how to use result set getter methods to retrieve basic [!INCLUDE[ssNoVersion](../../../includes/ssnoversion_md.md)] data type values, and how to use result set update methods to update those values.
22-
23-
The code file for this sample is named BasicDT.java, and it can be found in the following location:
24-
25-
\<*installation directory*>\sqljdbc_\<*version*>\\<*language*>\samples\datatypes
22+
This [!INCLUDE[jdbcNoVersion](../../../includes/jdbcnoversion_md.md)] sample application demonstrates how to use result set getter methods to retrieve basic [!INCLUDE[ssNoVersion](../../../includes/ssnoversion_md.md)] data type values, and how to use result set update methods to update those values.
2623

24+
The code file for this sample is named BasicDataTypes.java, and it can be found in the following location:
25+
26+
```
27+
\<installation directory>\sqljdbc_<version>\<language>\samples\datatypes
28+
```
29+
2730
## Requirements
28-
To run this sample application, you must set the classpath to include the mssql-jdbc jar file. You'll also need access to the [!INCLUDE[ssSampleDBnormal](../../../includes/sssampledbnormal_md.md)] sample database. For more information about how to set the classpath, see [Using the JDBC Driver](../../../connect/jdbc/using-the-jdbc-driver.md).
29-
30-
Create the following table and sample data in the [!INCLUDE[ssSampleDBnormal](../../../includes/sssampledbnormal_md.md)] sample database:
31-
32-
```sql
33-
use AdventureWorks
34-
CREATE TABLE DataTypesTable
35-
(Col1 int IDENTITY,
36-
Col2 char,
37-
Col3 varchar(50),
38-
Col4 bit,
39-
Col5 decimal(18, 2),
40-
Col6 money,
41-
Col7 datetime,
42-
Col8 date,
43-
Col9 time,
44-
Col10 datetime2,
45-
Col11 datetimeoffset
46-
);
47-
48-
INSERT INTO DataTypesTable
49-
VALUES ('A', 'Some text.', 0, 15.25, 10.00, '01/01/2006 23:59:59.991', '01/01/2006', '23:59:59', '01/01/2006 23:59:59.12345', '01/01/2006 23:59:59.12345 -1:00')
50-
```
31+
32+
To run this sample application, you must set the classpath to include the mssql-jdbc jar file. For more information about how to set the classpath, see [Using the JDBC Driver](../../../connect/jdbc/using-the-jdbc-driver.md).
5133

5234
> [!NOTE]
53-
> The [!INCLUDE[jdbcNoVersion](../../../includes/jdbcnoversion_md.md)] provides mssql-jdbc class library files to be used depending on your preferred Java Runtime Environment (JRE) settings. For more information about which JAR file to choose, see [System Requirements for the JDBC Driver](../../../connect/jdbc/system-requirements-for-the-jdbc-driver.md).
54-
55-
## Example
56-
In the following example, the sample code makes a connection to the [!INCLUDE[ssSampleDBnormal](../../../includes/sssampledbnormal_md.md)] database, and then retrieves a single row of data from the DataTypesTable test table. The custom displayRow method is then called to display all the data in the result set using various get\<Type> methods of the [SQLServerResultSet](../../../connect/jdbc/reference/sqlserverresultset-class.md) class.
35+
> The [!INCLUDE[jdbcNoVersion](../../../includes/jdbcnoversion_md.md)] provides mssql-jdbc class library files to be used depending on your preferred Java Runtime Environment (JRE) settings. For more information about which JAR file to choose, see [System Requirements for the JDBC Driver](../../../connect/jdbc/system-requirements-for-the-jdbc-driver.md).
5736
58-
Next, the sample uses various update\<Type> methods of the SQLServerResultSet class to update the data in the result set, and then calls the [updateRow](../../../connect/jdbc/reference/updaterow-method-sqlserverresultset.md) method to persist that data back to the database.
37+
## Example
38+
39+
In the following example, the sample code makes a connection to the database, and then retrieves a single row of data from the DataTypesTable test table. The custom displayRow method is then called to display all the data in the result set using various get\<Type> methods of the [SQLServerResultSet](../../../connect/jdbc/reference/sqlserverresultset-class.md) class.
5940

60-
Finally, the sample refreshes the data in the result set and then calls the custom displayRow method again to display it.
41+
Next, the sample uses various update\<Type> methods of the SQLServerResultSet class to update the data in the result set, and then calls the [updateRow](../../../connect/jdbc/reference/updaterow-method-sqlserverresultset.md) method to persist that data back to the database.
6142

43+
Finally, the sample refreshes the data in the result set and then calls the custom displayRow method again to display it.
44+
6245
```java
6346
import java.sql.Connection;
6447
import java.sql.Date;
6548
import java.sql.DriverManager;
49+
import java.sql.PreparedStatement;
6650
import java.sql.ResultSet;
6751
import java.sql.SQLException;
6852
import java.sql.Statement;
@@ -73,16 +57,21 @@ import com.microsoft.sqlserver.jdbc.SQLServerResultSet;
7357

7458
import microsoft.sql.DateTimeOffset;
7559

76-
public class BasicDT {
60+
public class BasicDataTypes {
61+
private static final String tableName = "DataTypesTable";
62+
7763
public static void main(String[] args) {
7864

7965
// Create a variable for the connection string.
80-
String connectionUrl = "jdbc:sqlserver://<server>:<port>;databaseName=AdventureWorks;user=<user>;password=<password>";
66+
String connectionUrl = "jdbc:sqlserver://<server>:<port>;databaseName=<database>;user=<user>;password=<password>";
8167

8268
try (Connection con = DriverManager.getConnection(connectionUrl);
8369
Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);) {
8470

85-
String SQL = "SELECT * FROM DataTypesTable";
71+
dropAndCreateTable(stmt);
72+
insertOriginalData(con);
73+
74+
String SQL = "SELECT * FROM " + tableName;
8675
ResultSet rs = stmt.executeQuery(SQL);
8776
rs.next();
8877
displayRow("ORIGINAL DATA", rs);
@@ -132,10 +121,38 @@ public class BasicDT {
132121
((SQLServerResultSet) rs).getDateTimeOffset(11)); // SQL datetimeoffset type.
133122
System.out.println();
134123
}
124+
125+
private static void dropAndCreateTable(Statement stmt) throws SQLException {
126+
stmt.executeUpdate("if object_id('" + tableName + "','U') is not null" + " drop table " + tableName);
127+
128+
String sql = "create table " + tableName + " (" + "c1 int, " + "c2 char(20), " + "c3 varchar(20), " + "c4 bit, "
129+
+ "c5 decimal(10,5), " + "c6 money, " + "c7 datetime, " + "c8 date, " + "c9 time(7), "
130+
+ "c10 datetime2(7), " + "c11 datetimeoffset(7), " + ");";
131+
132+
stmt.execute(sql);
133+
}
134+
135+
private static void insertOriginalData(Connection con) throws SQLException {
136+
String sql = "insert into " + tableName + " values( " + "?,?,?,?,?,?,?,?,?,?,?" + ")";
137+
try (PreparedStatement pstmt = con.prepareStatement(sql)) {
138+
pstmt.setObject(1, 100);
139+
pstmt.setObject(2, "original text");
140+
pstmt.setObject(3, "original text");
141+
pstmt.setObject(4, false);
142+
pstmt.setObject(5, 12.34);
143+
pstmt.setObject(6, 56.78);
144+
pstmt.setObject(7, new java.util.Date(1453500034839L));
145+
pstmt.setObject(8, new java.util.Date(1453500034839L));
146+
pstmt.setObject(9, new java.util.Date(1453500034839L));
147+
pstmt.setObject(10, new java.util.Date(1453500034839L));
148+
pstmt.setObject(11, new java.util.Date(1453500034839L));
149+
pstmt.execute();
150+
}
151+
}
135152
}
136-
```
137-
153+
```
154+
138155
## See Also
139-
[Working with Data Types &#40;JDBC&#41;](../../../connect/jdbc/working-with-data-types-jdbc.md)
140-
141-
156+
157+
[Working with Data Types &#40;JDBC&#41;](../../../connect/jdbc/code-samples/working-with-data-types-jdbc.md)
158+
Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Caching Result Set Data Sample | Microsoft Docs"
33
ms.custom: ""
4-
ms.date: "07/11/2018"
4+
ms.date: "07/31/2018"
55
ms.prod: sql
66
ms.prod_service: connectivity
77
ms.reviewer: ""
@@ -16,46 +16,53 @@ ms.author: genemi
1616
manager: craigg
1717
---
1818
# Caching Result Set Data Sample
19+
1920
[!INCLUDE[Driver_JDBC_Download](../../../includes/driver_jdbc_download.md)]
2021

21-
This [!INCLUDE[jdbcNoVersion](../../../includes/jdbcnoversion_md.md)] sample application demonstrates how to retrieve a large set of data from a database, and then control the number of rows of data that are cached on the client by using the [setFetchSize](../../../connect/jdbc/reference/setfetchsize-method-sqlserverresultset.md) method of the [SQLServerResultSet](../../../connect/jdbc/reference/sqlserverresultset-class.md) object.
22+
This [!INCLUDE[jdbcNoVersion](../../../includes/jdbcnoversion_md.md)] sample application demonstrates how to retrieve a large set of data from a database, and then control the number of rows of data that are cached on the client by using the [setFetchSize](../../../connect/jdbc/reference/setfetchsize-method-sqlserverresultset.md) method of the [SQLServerResultSet](../../../connect/jdbc/reference/sqlserverresultset-class.md) object.
2223

2324
> [!NOTE]
24-
> Limiting the number of rows cached on the client is different from limiting the total number of rows that a result set can contain. To control the total number of rows that are contained in a result set, use the [setMaxRows](../../../connect/jdbc/reference/setmaxrows-method-sqlserverstatement.md) method of the [SQLServerStatement](../../../connect/jdbc/reference/sqlserverstatement-class.md) object, which is inherited by both the [SQLServerPreparedStatement](../../../connect/jdbc/reference/sqlserverpreparedstatement-class.md) and [SQLServerCallableStatement](../../../connect/jdbc/reference/sqlservercallablestatement-class.md) objects.
25+
> Limiting the number of rows cached on the client is different from limiting the total number of rows that a result set can contain. To control the total number of rows that are contained in a result set, use the [setMaxRows](../../../connect/jdbc/reference/setmaxrows-method-sqlserverstatement.md) method of the [SQLServerStatement](../../../connect/jdbc/reference/sqlserverstatement-class.md) object, which is inherited by both the [SQLServerPreparedStatement](../../../connect/jdbc/reference/sqlserverpreparedstatement-class.md) and [SQLServerCallableStatement](../../../connect/jdbc/reference/sqlservercallablestatement-class.md) objects.
2526
26-
To set a limit on the number of rows cached on the client, you must first use a server-side cursor when you create one of the Statement objects by specifically stating the cursor type to use when creating the Statement object. For example, the JDBC driver provides the TYPE_SS_SERVER_CURSOR_FORWARD_ONLY cursor type, which is a fast forward-only, read-only server-side cursor for use with [!INCLUDE[ssNoVersion](../../../includes/ssnoversion_md.md)] databases.
27+
To set a limit on the number of rows cached on the client, you must first use a server-side cursor when you create one of the Statement objects by specifically stating the cursor type to use when creating the Statement object. For example, the JDBC driver provides the TYPE_SS_SERVER_CURSOR_FORWARD_ONLY cursor type, which is a fast forward-only, read-only server-side cursor for use with [!INCLUDE[ssNoVersion](../../../includes/ssnoversion_md.md)] databases.
2728

2829
> [!NOTE]
29-
> An alternative to using the SQL Server specific cursor type is to use the selectMethod connection string property, setting its value to "cursor". For more information about the cursor types supported by the JDBC driver, see [Understanding Cursor Types](../../../connect/jdbc/understanding-cursor-types.md).
30-
31-
After you have run the query in the Statement object and the data is returned to the client as a result set, you can call the setFetchSize method to control how much data is retrieved from the database at one time. For example, if you have a table that 100 rows of data, and you set the fetch size to 10, only 10 rows of data will be cached on the client at any time. Although this will slow down the speed at which the data is processed, it has the advantage of using less memory on the client, which can be especially useful when you need to process large amounts of data.
30+
> An alternative to using the SQL Server specific cursor type is to use the selectMethod connection string property, setting its value to "cursor". For more information about the cursor types supported by the JDBC driver, see [Understanding Cursor Types](../../../connect/jdbc/understanding-cursor-types.md).
3231
33-
The code file for this sample is named CacheRS.java, and it can be found in the following location:
34-
35-
\<*installation directory*>\sqljdbc_\<*version*>\\<*language*>\samples\resultsets
32+
After you have run the query in the Statement object and the data is returned to the client as a result set, you can call the setFetchSize method to control how much data is retrieved from the database at one time. For example, if you have a table that 100 rows of data, and you set the fetch size to 10, only 10 rows of data will be cached on the client at any time. Although this will slow down the speed at which the data is processed, it has the advantage of using less memory on the client, which can be especially useful when you need to process large amounts of data.
3633

34+
The code file for this sample is named CacheResultSet.java, and it can be found in the following location:
35+
36+
```bash
37+
\<installation directory>\sqljdbc_<version>\<language>\samples\resultsets
38+
```
39+
3740
## Requirements
38-
To run this sample application, you must set the classpath to include the mssql-jdbc jar file. You'll also need access to the [!INCLUDE[ssSampleDBnormal](../../../includes/sssampledbnormal_md.md)] sample database. For more information about how to set the classpath, see [Using the JDBC Driver](../../../connect/jdbc/using-the-jdbc-driver.md).
41+
42+
To run this sample application, you must set the classpath to include the mssql-jdbc jar file. You'll also need access to the [!INCLUDE[ssSampleDBnormal](../../../includes/sssampledbnormal_md.md)] sample database. For more information about how to set the classpath, see [Using the JDBC Driver](../../../connect/jdbc/using-the-jdbc-driver.md).
3943

4044
> [!NOTE]
41-
> The [!INCLUDE[jdbcNoVersion](../../../includes/jdbcnoversion_md.md)] provides mssql-jdbc class library files to be used depending on your preferred Java Runtime Environment (JRE) settings. For more information about which JAR file to choose, see [System Requirements for the JDBC Driver](../../../connect/jdbc/system-requirements-for-the-jdbc-driver.md).
42-
45+
> The [!INCLUDE[jdbcNoVersion](../../../includes/jdbcnoversion_md.md)] provides mssql-jdbc class library files to be used depending on your preferred Java Runtime Environment (JRE) settings. For more information about which JAR file to choose, see [System Requirements for the JDBC Driver](../../../connect/jdbc/system-requirements-for-the-jdbc-driver.md).
46+
4347
## Example
44-
In the following example, the sample code makes a connection to the [!INCLUDE[ssSampleDBnormal](../../../includes/sssampledbnormal_md.md)] sample database. Then it uses an SQL statement with the [SQLServerStatement](../../../connect/jdbc/reference/sqlserverstatement-class.md) object, specifies the server-side cursor type, and then runs the SQL statement and places the data that it returns into a SQLServerResultSet object.
45-
46-
Next, the sample code calls the custom timerTest method, passing as arguments the fetch size to use and the result set. The timerTest method then sets the fetch size of the result set by using the setFetchSize method, sets the start time of the test, and then iterates through the result set with a `While` loop. As soon as the `While` loop is exited, the code sets the stop time of the test, and then displays the result of the test including the fetch size, the number of rows processed, and the time it took to execute the test.
48+
49+
In the following example, the sample code makes a connection to the [!INCLUDE[ssSampleDBnormal](../../../includes/sssampledbnormal_md.md)] sample database. Then it uses an SQL statement with the [SQLServerStatement](../../../connect/jdbc/reference/sqlserverstatement-class.md) object, specifies the server-side cursor type, and then runs the SQL statement and places the data that it returns into a SQLServerResultSet object.
4750

51+
Next, the sample code calls the custom timerTest method, passing as arguments the fetch size to use and the result set. The timerTest method then sets the fetch size of the result set by using the setFetchSize method, sets the start time of the test, and then iterates through the result set with a `While` loop. As soon as the `While` loop is exited, the code sets the stop time of the test, and then displays the result of the test including the fetch size, the number of rows processed, and the time it took to execute the test.
52+
4853
```java
4954
import java.sql.Connection;
5055
import java.sql.DriverManager;
5156
import java.sql.ResultSet;
5257
import java.sql.SQLException;
5358
import java.sql.Statement;
59+
import java.util.ArrayList;
5460

5561
import com.microsoft.sqlserver.jdbc.SQLServerResultSet;
5662

57-
public class CacheRS {
63+
public class CacheResultSet {
5864

65+
@SuppressWarnings("serial")
5966
public static void main(String[] args) {
6067

6168
// Create a variable for the connection string.
@@ -66,30 +73,20 @@ public class CacheRS {
6673

6774
String SQL = "SELECT * FROM Sales.SalesOrderDetail;";
6875

69-
// Perform a fetch for every row in the result set.
70-
ResultSet rs = stmt.executeQuery(SQL);
71-
timerTest(1, rs);
72-
rs.close();
73-
74-
// Perform a fetch for every tenth row in the result set.
75-
rs = stmt.executeQuery(SQL);
76-
timerTest(10, rs);
77-
rs.close();
78-
79-
// Perform a fetch for every 100th row in the result set.
80-
rs = stmt.executeQuery(SQL);
81-
timerTest(100, rs);
82-
rs.close();
83-
84-
// Perform a fetch for every 1000th row in the result set.
85-
rs = stmt.executeQuery(SQL);
86-
timerTest(1000, rs);
87-
rs.close();
88-
89-
// Perform a fetch for every 128th row (the default) in the result set.
90-
rs = stmt.executeQuery(SQL);
91-
timerTest(0, rs);
92-
rs.close();
76+
for (int n : new ArrayList<Integer>() {
77+
{
78+
add(1);
79+
add(10);
80+
add(100);
81+
add(1000);
82+
add(0);
83+
}
84+
}) {
85+
// Perform a fetch for every nth row in the result set.
86+
try (ResultSet rs = stmt.executeQuery(SQL)) {
87+
timerTest(n, rs);
88+
}
89+
}
9390
}
9491
// Handle any errors that may have occurred.
9592
catch (SQLException e) {
@@ -123,9 +120,8 @@ public class CacheRS {
123120
System.out.println();
124121
}
125122
}
126-
```
127-
123+
```
124+
128125
## See Also
129-
[Working with Result Sets](../../../connect/jdbc/working-with-result-sets.md)
130-
131-
126+
127+
[Working with Result Sets](../../../connect/jdbc/code-samples/working-with-result-sets.md)

0 commit comments

Comments
 (0)