Skip to content

Commit 2030095

Browse files
authored
Merge pull request #259620 from wchigit/add-pom
Add pom for sql java
2 parents 3a358ac + f7c3357 commit 2030095

File tree

2 files changed

+69
-44
lines changed

2 files changed

+69
-44
lines changed

articles/service-connector/includes/code-sql-me-id.md

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
author: xiaofanzhou
33
ms.service: service-connector
44
ms.topic: include
5-
ms.date: 10/26/2023
5+
ms.date: 11/28/2023
66
ms.author: xiaofanzhou
77
---
88

@@ -29,33 +29,48 @@ ms.author: xiaofanzhou
2929

3030
### [Java](#tab/sql-me-id-java)
3131

32-
Get the Azure SQL Database connection string from the environment variable added by Service Connector.
33-
34-
```java
35-
import java.sql.Connection;
36-
import java.sql.ResultSet;
37-
import java.sql.Statement;
38-
39-
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
40-
41-
public class Main {
42-
public static void main(String[] args) {
43-
// AZURE_SQL_CONNECTIONSTRING should be one of the following:
44-
// For system-assigned managed identity: "jdbc:sqlserver://{SQLName}.database.windows.net:1433;databaseName={SQLDbName};authentication=ActiveDirectoryMSI;"
45-
// For user-assigned managed identity: "jdbc:sqlserver://{SQLName}.database.windows.net:1433;databaseName={SQLDbName};msiClientId={UserAssignedMiClientId};authentication=ActiveDirectoryMSI;"
46-
// For service principal: "jdbc:sqlserver://{SQLName}.database.windows.net:1433;databaseName={SQLDbName};user={ServicePrincipalClientId};password={spSecret};authentication=ActiveDirectoryServicePrincipal;"
47-
String connectionString = System.getenv("AZURE_SQL_CONNECTIONSTRING");
48-
SQLServerDataSource ds = new SQLServerDataSource();
49-
ds.setURL(connectionString);
50-
try (Connection connection = ds.getConnection()) {
51-
System.out.println("Connected successfully.");
52-
} catch (SQLException e) {
53-
e.printStackTrace();
32+
1. Add the following dependencies in your *pom.xml* file:
33+
34+
```xml
35+
<dependency>
36+
<groupId>com.microsoft.sqlserver</groupId>
37+
<artifactId>mssql-jdbc</artifactId>
38+
<version>10.2.0.jre11</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>com.azure</groupId>
42+
<artifactId>azure-identity</artifactId>
43+
<version>1.7.0</version>
44+
</dependency>
45+
```
46+
47+
1. Get the Azure SQL Database connection string from the environment variable added by Service Connector.
48+
49+
```java
50+
import java.sql.Connection;
51+
import java.sql.ResultSet;
52+
import java.sql.Statement;
53+
54+
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
55+
56+
public class Main {
57+
public static void main(String[] args) {
58+
// AZURE_SQL_CONNECTIONSTRING should be one of the following:
59+
// For system-assigned managed identity: "jdbc:sqlserver://{SQLName}.database.windows.net:1433;databaseName={SQLDbName};authentication=ActiveDirectoryMSI;"
60+
// For user-assigned managed identity: "jdbc:sqlserver://{SQLName}.database.windows.net:1433;databaseName={SQLDbName};msiClientId={UserAssignedMiClientId};authentication=ActiveDirectoryMSI;"
61+
// For service principal: "jdbc:sqlserver://{SQLName}.database.windows.net:1433;databaseName={SQLDbName};user={ServicePrincipalClientId};password={spSecret};authentication=ActiveDirectoryServicePrincipal;"
62+
String connectionString = System.getenv("AZURE_SQL_CONNECTIONSTRING");
63+
SQLServerDataSource ds = new SQLServerDataSource();
64+
ds.setURL(connectionString);
65+
try (Connection connection = ds.getConnection()) {
66+
System.out.println("Connected successfully.");
67+
} catch (SQLException e) {
68+
e.printStackTrace();
69+
}
5470
}
5571
}
56-
}
57-
```
58-
For more information, see [Connect using Microsoft Entra authentication](/sql/connect/jdbc/connecting-using-azure-active-directory-authentication).
72+
```
73+
For more information, see [Connect to Azure databases from App Service without secrets using a managed identity](/azure/app-service/tutorial-connect-msi-azure-database?tabs=sqldatabase%2Csystemassigned%2Cjava%2Cwindowsclient#3-modify-your-code).
5974
6075
### [SpringBoot](#tab/sql-me-id-spring)
6176

articles/service-connector/includes/code-sql-secret.md

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
author: wchigit
33
ms.service: service-connector
44
ms.topic: include
5-
ms.date: 10/26/2023
5+
ms.date: 11/28/2023
66
ms.author: wchi
77
---
88

@@ -26,28 +26,38 @@ ms.author: wchi
2626

2727
### [Java](#tab/sql-secret-java)
2828

29-
Get the Azure SQL Database connection string from the environment variable added by Service Connector.
29+
1. Add the following dependencies in your *pom.xml* file:
3030

31-
```java
32-
import java.sql.Connection;
33-
import java.sql.ResultSet;
34-
import java.sql.Statement;
31+
```xml
32+
<dependency>
33+
<groupId>com.microsoft.sqlserver</groupId>
34+
<artifactId>mssql-jdbc</artifactId>
35+
<version>10.2.0.jre11</version>
36+
</dependency>
37+
```
3538

36-
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
39+
1. Get the Azure SQL Database connection string from the environment variable added by Service Connector.
3740

38-
public class Main {
39-
public static void main(String[] args) {
40-
String connectionString = System.getenv("AZURE_SQL_CONNECTIONSTRING");
41-
SQLServerDataSource ds = new SQLServerDataSource();
42-
ds.setURL(connectionString);
43-
try (Connection connection = ds.getConnection()) {
44-
System.out.println("Connected successfully.");
45-
} catch (SQLException e) {
46-
e.printStackTrace();
41+
```java
42+
import java.sql.Connection;
43+
import java.sql.ResultSet;
44+
import java.sql.Statement;
45+
46+
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
47+
48+
public class Main {
49+
public static void main(String[] args) {
50+
String connectionString = System.getenv("AZURE_SQL_CONNECTIONSTRING");
51+
SQLServerDataSource ds = new SQLServerDataSource();
52+
ds.setURL(connectionString);
53+
try (Connection connection = ds.getConnection()) {
54+
System.out.println("Connected successfully.");
55+
} catch (SQLException e) {
56+
e.printStackTrace();
57+
}
4758
}
4859
}
49-
}
50-
```
60+
```
5161
5262
### [SpringBoot](#tab/sql-secret-spring)
5363
1. Add dependency in your 'pom.xml' file:

0 commit comments

Comments
 (0)