Data Discovery and Classification
Data Discovery and Classification
File: ExecuteStoredProcedures.java
Summary: This Microsoft JDBC Driver for SQL Server sample application
that demonstrates how to extract 'SQL Data Discovery and
Classification' information from ResultSet.
---------------------------------------------------------------------
This file is part of the Microsoft JDBC Driver for SQL Server Code Samples.
Copyright (C) Microsoft Corporation. All rights reserved.
THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
=====================================================================*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import com.microsoft.sqlserver.jdbc.SQLServerResultSet;
import com.microsoft.sqlserver.jdbc.dataclassification.SensitivityProperty;
/**
* SQL Server feature documentation reference:
* https://docs.microsoft.com/en-us/sql/relational-databases/security/sql-data-
discovery-and-classification?view=sql-server-2017
*/
public class DataDiscoveryAndClassification {
/**
* Creates table for the test and sets tags for Sensitivity Classification
*
* @param stmt
* Statement to work with
* @param tableName
* Table to be created
* @throws SQLException
* If an exception occurs
*/
private static void createTable(Statement stmt, String tableName) throws
SQLException {
// Creates table for storing Supplier data
stmt.execute("CREATE TABLE " + tableName + " (" + "[Id] [int] IDENTITY(1,1)
NOT NULL,"
+ "[CompanyName] [nvarchar](40) NOT NULL," + "[ContactName]
[nvarchar](50) NULL,"
+ "[ContactTitle] [nvarchar](40) NULL," + "[City] [nvarchar](40)
NULL,"
+ "[Country] [nvarchar](40) NULL," + "[Phone] [nvarchar](30) MASKED
WITH (FUNCTION = 'default()') NULL,"
+ "[Fax] [nvarchar](30) MASKED WITH (FUNCTION = 'default()') NULL,"
+ "CONSTRAINT [PK_" + tableName
+ "] PRIMARY KEY CLUSTERED" + "([Id] ASC "
+ ")WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) ON
[PRIMARY]" + ") ON [PRIMARY]");
/**
* Runs query to fetch ResultSet from target table
*
* @param stmt
* Statement to work with
* @param tableName
* Name of table to fetch results from
* @throws SQLException
* If an exception occurs
*/
private static void runTests(Statement stmt, String tableName) throws
SQLException {
String query = "SELECT * FROM " + tableName;
try (SQLServerResultSet rs = (SQLServerResultSet) stmt.executeQuery(query))
{
printSensitivityClassification(rs);
}
}
/**
* Prints Sensitivity Classification data as received in ResultSet
*
* @param rs
* Active ResultSet to read data from
* @throws SQLException
* If an exception occurs
*/
private static void printSensitivityClassification(SQLServerResultSet rs)
throws SQLException {
if (null != rs.getSensitivityClassification()) {
for (int columnPos = 0; columnPos <
rs.getSensitivityClassification().getColumnSensitivities().size();
columnPos++) {
for (SensitivityProperty sp :
rs.getSensitivityClassification().getColumnSensitivities().get(columnPos)
.getSensitivityProperties()) {
if (sp.getLabel() != null) {
System.out.println("Labels received for Column : " +
columnPos);
System.out.println("Label ID: " + sp.getLabel().getId());
System.out.println("Label Name: " +
sp.getLabel().getName());
System.out.println();
}
if (sp.getInformationType() != null) {
System.out.println("Information Types received for Column :
" + columnPos);
System.out.println("Information Type ID: " +
sp.getInformationType().getId());
System.out.println("Information Type Name: " +
sp.getInformationType().getName());
System.out.println();
}
}
}
}
}
/**
* Drops the table created for test
*
* @param stmt
* Statement to work with
* @param tableName
* Table Name to be used
* @throws SQLException
* If an exception occurs
*/
private static void drop_table(Statement stmt, String tableName) throws
SQLException {
stmt.execute("DROP TABLE " + tableName);
}
}