Nota:
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
Java 数据库连接 (JDBC) 是用于连接数据库和执行查询的 Java API。 你可以使用 JDBC 连接到 Azure 数据资源管理器。 此功能是通过 Azure 数据资源管理器的 TDS 兼容终结点实现的,该终结点可以仿真 Microsoft SQL Server。 终结点支持 TDS 版本 7.x 和 8.0。
有关详细信息,请参阅 Azure 数据资源管理器中的 SQL Server 仿真概述。
使用 JDBC 进行连接
以下步骤说明如何使用 JDBC 连接到 Azure 数据资源管理器。
使用
mssql-jdbcJAR、adal4jJAR 及其所有依赖项创建应用程序。 下面是使用mssql-jdbc版本7.0.0和adal4j版本1.6.3时所需的依赖项列表。mssql-jdbc-7.0.0.jre8.jar adal4j-1.6.3.jar accessors-smart-1.2.jar activation-1.1.jar asm-5.0.4.jar commons-codec-1.11.jar commons-lang3-3.5.jar gson-2.8.0.jar javax.mail-1.6.1.jar jcip-annotations-1.0-1.jar json-smart-2.3.jar lang-tag-1.4.4.jar nimbus-jose-jwt-6.5.jar oauth2-oidc-sdk-5.64.4.jar slf4j-api-1.7.21.jar创建一个应用程序来使用 JDBC 驱动程序类 com.microsoft.sqlserver.jdbc.SQLServerDriver。 可以使用以下格式的连接字符串进行连接。 请将
<cluster_name.region>替换为你的群集名称和群集区域,并将<database_name>替换为你的数据库名称。jdbc:sqlserver://<cluster_name.region>.kusto.chinacloudapi.cn:1433;database=<database_name>;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.kusto.chinacloudapi.cn;loginTimeout=30;authentication=ActiveDirectoryIntegrated
JDBC 用户身份验证
以下示例演示了如何使用 Microsoft Entra ID 和 JDBC 以编程方式对用户主体进行身份验证。
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import com.microsoft.aad.msal4j.*;
public class Sample {
public static void main(String[] args) throws Exception {
IAuthenticationResult authenticationResult = futureAuthenticationResult.get();
SQLServerDataSource ds = new SQLServerDataSource();
ds.setServerName("<cluster_DNS>");
ds.setDatabaseName("<database_name>");
ds.setHostNameInCertificate("*.kusto.chinacloudapi.cn"); // Or appropriate regional domain.
ds.setAuthentication("ActiveDirectoryIntegrated");
try (Connection connection = ds.getConnection();
Statement stmt = connection.createStatement();) {
ResultSet rs = stmt.executeQuery("<T-SQL_query>");
/*
Read query result.
*/
} catch (Exception e) {
System.out.println();
e.printStackTrace();
}
}
}
JDBC 应用程序身份验证
以下示例演示了如何使用 Microsoft Entra ID 和 JDBC 以编程方式对应用程序主体进行身份验证。
import java.sql.*;
import com.microsoft.sqlserver.jdbc.*;
import com.microsoft.aad.msal4j.*;
import java.net.MalformedURLException;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class Sample {
public static void main(String[] args) throws Throwable {
// Can also use tenant name.
String authorityUrl = "https://login.partner.microsoftonline.cn/<tenant_ID>";
Set<String> scopes = new HashSet<>();
scopes.add("https://<cluster_DNS>/.default");
IConfidentialClientApplication clientApplication = ConfidentialClientApplication.builder("<application_client_ID>", ClientCredentialFactory.createFromSecret("<application_key>")).authority(authorityUrl).build();
CompletableFuture<IAuthenticationResult> futureAuthenticationResult = clientApplication.acquireToken(ClientCredentialParameters.builder(scopes).build());
IAuthenticationResult authenticationResult = futureAuthenticationResult.get();
SQLServerDataSource ds = new SQLServerDataSource();
ds.setServerName("<cluster_DNS>");
ds.setDatabaseName("<database_name>");
ds.setAccessToken(authenticationResult.accessToken());
connection = ds.getConnection();
statement = connection.createStatement();
ResultSet rs = statement.executeQuery("<T-SQL_query>");
/*
Read query result.
*/
}
}