Skip to content

Commit 17ef7bf

Browse files
committed
edits
1 parent de55685 commit 17ef7bf

File tree

1 file changed

+37
-43
lines changed

1 file changed

+37
-43
lines changed

articles/mysql/flexible-server/connect-java.md

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.subservice: flexible-server
88
ms.custom: mvc, devcenter, devx-track-azurecli, mode-api
99
ms.topic: quickstart
1010
ms.devlang: java
11-
ms.date: 01/16/2021
11+
ms.date: 10/20/2022
1212
---
1313

1414
# Use Java and JDBC with Azure Database for MySQL Flexible Server
@@ -27,18 +27,15 @@ MySQL authentication uses accounts stored in MySQL. If you choose to use passwor
2727

2828
## Prerequisites
2929

30-
- An Azure account with an active subscription.
31-
32-
[!INCLUDE [flexible-server-free-trial-note](../includes/flexible-server-free-trial-note.md)]
30+
- An Azure account with an active subscription.
31+
[!INCLUDE [flexible-server-free-trial-note](../includes/flexible-server-free-trial-note.md)]
3332
- [Azure Cloud Shell](../../cloud-shell/quickstart.md) or [Azure CLI](/cli/azure/install-azure-cli). We recommend Azure Cloud Shell so you'll be logged in automatically and have access to all the tools you'll need.
3433
- A supported [Java Development Kit](/azure/developer/java/fundamentals/java-support-on-azure), version 8 (included in Azure Cloud Shell).
3534
- The [Apache Maven](https://maven.apache.org/) build tool.
3635

3736
## Prepare the working environment
3837

39-
We are going to use environment variables to limit typing mistakes, and to make it easier for you to customize the following configuration for your specific needs.
40-
41-
Set up those environment variables by using the following commands:
38+
First, use the following command to set up some environment variables.
4239

4340
### [Passwordless (Recommended)](#tab/passwordless)
4441

@@ -47,16 +44,16 @@ export AZ_RESOURCE_GROUP=database-workshop
4744
export AZ_DATABASE_NAME=<YOUR_DATABASE_NAME>
4845
export AZ_LOCATION=<YOUR_AZURE_REGION>
4946
export AZ_MYSQL_AD_NON_ADMIN_USERNAME=demo-non-admin
50-
export AZ_USER_IDENTITY_NAME=<YOUR_USER_ASSIGNED_MANAGEMED_IDENTITY_NAME>
47+
export AZ_USER_IDENTITY_NAME=<YOUR_USER_ASSIGNED_MANAGED_IDENTITY_NAME>
5148
export CURRENT_USERNAME=$(az ad signed-in-user show --query userPrincipalName -o tsv)
5249
export CURRENT_USER_OBJECTID=$(az ad signed-in-user show --query id -o tsv)
5350
```
5451

5552
Replace the placeholders with the following values, which are used throughout this article:
5653

57-
- `<YOUR_DATABASE_NAME>`: The name of your MySQL server. It should be unique across Azure.
54+
- `<YOUR_DATABASE_NAME>`: The name of your MySQL server, which should be unique across Azure.
5855
- `<YOUR_AZURE_REGION>`: The Azure region you'll use. You can use `eastus` by default, but we recommend that you configure a region closer to where you live. You can see the full list of available regions by entering `az account list-locations`.
59-
- `<YOUR_USER_ASSIGNED_MANAGEMED_IDENTITY_NAME>`: The name of your user assigned managed identity server. It should be unique across Azure.
56+
- `<YOUR_USER_ASSIGNED_MANAGED_IDENTITY_NAME>`: The name of your user assigned managed identity server, which should be unique across Azure.
6057

6158
### [Password](#tab/password)
6259

@@ -72,8 +69,8 @@ export AZ_MYSQL_NON_ADMIN_PASSWORD=<YOUR_MYSQL_NON_ADMIN_PASSWORD>
7269

7370
Replace the placeholders with the following values, which are used throughout this article:
7471

75-
- `<YOUR_DATABASE_NAME>`: The name of your MySQL server. It should be unique across Azure.
76-
- `<YOUR_AZURE_REGION>`: The Azure region you'll use. You can use `eastus` by default, but we recommend that you configure a region closer to where you live. You can have the full list of available regions by entering `az account list-locations`.
72+
- `<YOUR_DATABASE_NAME>`: The name of your MySQL server, which should be unique across Azure.
73+
- `<YOUR_AZURE_REGION>`: The Azure region you'll use. You can use `eastus` by default, but we recommend that you configure a region closer to where you live. You can see the full list of available regions by entering `az account list-locations`.
7774
- `<YOUR_MYSQL_ADMIN_PASSWORD>` and `<YOUR_MYSQL_NON_ADMIN_PASSWORD>`: The password of your MySQL database server. That password should have a minimum of eight characters. The characters should be from three of the following categories: English uppercase letters, English lowercase letters, numbers (0-9), and non-alphanumeric characters (!, $, #, %, and so on).
7875

7976
---
@@ -91,7 +88,7 @@ az group create \
9188

9289
### Create a MySQL server and set up admin user
9390

94-
The first thing we'll create is a managed MySQL server.
91+
The first thing you'll create is a managed MySQL server.
9592

9693
> [!NOTE]
9794
> You can read more detailed information about creating MySQL servers in [Create an Azure Database for MySQL server by using the Azure portal](./quickstart-create-server-portal.md).
@@ -115,25 +112,24 @@ az mysql flexible-server create \
115112
--output tsv
116113
```
117114

118-
Run the following command to create **User-Assigned Identity** for assigning:
115+
Run the following command to create a user-assigned identity for assigning:
119116

120117
```azurecli
121118
az identity create \
122-
--resource-group $AZ_RESOURCE_GROUP \
123-
--name $AZ_USER_IDENTITY_NAME
119+
--resource-group $AZ_RESOURCE_GROUP \
120+
--name $AZ_USER_IDENTITY_NAME
124121
```
125122

126123
> [!IMPORTANT]
127-
> After creating **User-Assigned Identity**, ask your **Global Administrator** or **Privileged Role Administrator** to grant three permissions: `User.Read.All`, `GroupMember.Read.All` and `Application.Read.ALL` for this identity.
128-
> More information refer [Active Directory authentication](/azure/mysql/flexible-server/concepts-azure-ad-authentication#permissions).
124+
> After creating the user-assigned identity, ask your *Global Administrator* or *Privileged Role Administrator* to grant the following permissions for this identity: `User.Read.All`, `GroupMember.Read.All`, and `Application.Read.ALL`. For more information, see the [Permissions](/azure/mysql/flexible-server/concepts-azure-ad-authentication#permissions) section of [Active Directory authentication](/azure/mysql/flexible-server/concepts-azure-ad-authentication).
129125
130126
Run the following command to assign the identity to MySQL server for creating Azure AD admin:
131127

132128
```azurecli
133129
az mysql flexible-server identity assign \
134-
--resource-group $AZ_RESOURCE_GROUP \
135-
--server-name $AZ_DATABASE_NAME \
136-
--identity $AZ_USER_IDENTITY_NAME
130+
--resource-group $AZ_RESOURCE_GROUP \
131+
--server-name $AZ_DATABASE_NAME \
132+
--identity $AZ_USER_IDENTITY_NAME
137133
```
138134

139135
Run the following command to set the Azure AD admin user:
@@ -169,19 +165,17 @@ This command creates a small MySQL server.
169165

170166
---
171167

172-
The MySQL server that you created has a empty database called **flexibleserverdb**. We will use this database for this article.
168+
The MySQL server that you created has a empty database called `flexibleserverdb`.
173169

174170
[Having any issues? Let us know.](https://github.com/MicrosoftDocs/azure-docs/issues)
175171

176172
### Configure a firewall rule for your MySQL server
177173

178174
Azure Database for MySQL instances are secured by default. They have a firewall that doesn't allow any incoming connection.
179175

180-
You can skip this step if you are using bash, because the `flexible-server create` command already detected your local IP address and set it on MySQL server.
176+
You can skip this step if you're using Bash because the `flexible-server create` command already detected your local IP address and set it on MySQL server.
181177

182-
But if you're connecting to your MySQL server from WSL on a Windows computer, you'll need to add the WSL host ID to your firewall.
183-
184-
Obtain the IP address of your host machine by running the following command in WSL:
178+
If you're connecting to your MySQL server from Windows Subsystem for Linux (WSL) on a Windows computer, you'll need to add the WSL host ID to your firewall. Obtain the IP address of your host machine by running the following command in WSL:
185179

186180
```bash
187181
cat /etc/resolv.conf
@@ -287,7 +281,7 @@ rm create_user.sql
287281

288282
### Create a new Java project
289283

290-
Using your favorite IDE, create a new Java project, and add a `pom.xml` file in its root directory:
284+
Using your favorite IDE, create a new Java project, and add a *pom.xml* file in its root directory:
291285

292286
#### [Passwordless connection (Recommended)](#tab/passwordless)
293287

@@ -352,7 +346,7 @@ Using your favorite IDE, create a new Java project, and add a `pom.xml` file in
352346

353347
---
354348

355-
This file is an [Apache Maven](https://maven.apache.org/) that configures our project to use:
349+
This file is an [Apache Maven](https://maven.apache.org/) file that configures your project to use:
356350

357351
- Java 8
358352
- A recent MySQL driver for Java
@@ -387,11 +381,11 @@ EOF
387381
---
388382

389383
> [!NOTE]
390-
> We append `?serverTimezone=UTC` to the configuration property `url`, to tell the JDBC driver to use the UTC date format (or Coordinated Universal Time) when connecting to the database. Otherwise, our Java server would not use the same date format as the database, which would result in an error.
384+
> The configuration property `url` has `?serverTimezone=UTC` appended to tell the JDBC driver to use the UTC date format (or Coordinated Universal Time) when connecting to the database. Otherwise, your Java server would not use the same date format as the database, which would result in an error.
391385
392386
### Create an SQL file to generate the database schema
393387

394-
We will use a *src/main/resources/`schema.sql`* file in order to create a database schema. Create that file, with the following content:
388+
You'll use a *src/main/resources/schema.sql* file in order to create a database schema. Create that file, with the following content:
395389

396390
```sql
397391
DROP TABLE IF EXISTS todo;
@@ -440,14 +434,14 @@ public class DemoApplication {
440434
statement.execute(scanner.nextLine());
441435
}
442436

443-
/*
444-
Todo todo = new Todo(1L, "configuration", "congratulations, you have set up JDBC correctly!", true);
437+
/*
438+
Todo todo = new Todo(1L, "configuration", "congratulations, you have set up JDBC correctly!", true);
445439
insertData(todo, connection);
446440
todo = readData(connection);
447441
todo.setDetails("congratulations, you have updated data!");
448442
updateData(todo, connection);
449443
deleteData(todo, connection);
450-
*/
444+
*/
451445

452446
log.info("Closing database connection");
453447
connection.close();
@@ -458,9 +452,9 @@ public class DemoApplication {
458452

459453
[Having any issues? Let us know.](https://github.com/MicrosoftDocs/azure-docs/issues)
460454

461-
This Java code will use the *application.properties* and the *schema.sql* files that we created earlier, in order to connect to the MySQL server and create a schema that will store our data.
455+
This Java code will use the *application.properties* and the *schema.sql* files that you created earlier, in order to connect to the MySQL server and create a schema that will store your data.
462456

463-
In this file, you can see that we commented methods to insert, read, update and delete data: we will code those methods in the rest of this article, and you will be able to uncomment them one after each other.
457+
In this file, you can see that we commented methods to insert, read, update and delete data: you'll code those methods in the rest of this article, and you'll be able to uncomment them one after each other.
464458

465459
> [!NOTE]
466460
> The database credentials are stored in the *user* and *password* properties of the *application.properties* file. Those credentials are used when executing `DriverManager.getConnection(properties.getProperty("url"), properties);`, as the properties file is passed as an argument.
@@ -476,7 +470,7 @@ You can now execute this main class with your favorite tool:
476470

477471
The application should connect to the Azure Database for MySQL, create a database schema, and then close the connection, as you should see in the console logs:
478472

479-
```
473+
```output
480474
[INFO ] Loading application properties
481475
[INFO ] Connecting to the database
482476
[INFO ] Database connection test: demo
@@ -581,7 +575,7 @@ insertData(todo, connection);
581575

582576
Executing the main class should now produce the following output:
583577

584-
```
578+
```output
585579
[INFO ] Loading application properties
586580
[INFO ] Connecting to the database
587581
[INFO ] Database connection test: demo
@@ -592,7 +586,7 @@ Executing the main class should now produce the following output:
592586

593587
### Reading data from Azure Database for MySQL
594588

595-
Let's read the data previously inserted, to validate that our code works correctly.
589+
Next, read the data previously inserted to validate that your code works correctly.
596590

597591
In the *src/main/java/DemoApplication.java* file, after the `insertData` method, add the following method to read data from the database:
598592

@@ -623,7 +617,7 @@ todo = readData(connection);
623617

624618
Executing the main class should now produce the following output:
625619

626-
```
620+
```output
627621
[INFO ] Loading application properties
628622
[INFO ] Connecting to the database
629623
[INFO ] Database connection test: demo
@@ -638,7 +632,7 @@ Executing the main class should now produce the following output:
638632

639633
### Updating data in Azure Database for MySQL
640634

641-
Let's update the data we previously inserted.
635+
Next, update the data you previously inserted.
642636

643637
Still in the *src/main/java/DemoApplication.java* file, after the `readData` method, add the following method to update data inside the database:
644638

@@ -666,7 +660,7 @@ updateData(todo, connection);
666660

667661
Executing the main class should now produce the following output:
668662

669-
```
663+
```output
670664
[INFO ] Loading application properties
671665
[INFO ] Connecting to the database
672666
[INFO ] Database connection test: demo
@@ -682,7 +676,7 @@ Executing the main class should now produce the following output:
682676

683677
### Deleting data in Azure Database for MySQL
684678

685-
Finally, let's delete the data we previously inserted.
679+
Finally, delete the data you previously inserted.
686680

687681
Still in the *src/main/java/DemoApplication.java* file, after the `updateData` method, add the following method to delete data inside the database:
688682

@@ -704,7 +698,7 @@ deleteData(todo, connection);
704698

705699
Executing the main class should now produce the following output:
706700

707-
```
701+
```output
708702
[INFO ] Loading application properties
709703
[INFO ] Connecting to the database
710704
[INFO ] Database connection test: demo

0 commit comments

Comments
 (0)