You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/mysql/connect-java.md
+15-7Lines changed: 15 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,20 +12,24 @@ ms.date: 12/02/2019
12
12
13
13
# Quickstart: Use Java to connect to and query data in Azure Database for MySQL
14
14
15
-
This quickstart demonstrates how to connect to an Azure Database for MySQL by using a Java application and the JDBC driver [MariaDB Connector/J](https://mariadb.com/kb/en/library/mariadb-connector-j/). It shows how to use SQL statements to query, insert, update, and delete data in the database. This article assumes that you are familiar with developing using Java and that you are new to working with Azure Database for MySQL.
15
+
In this quickstart, you connect to an Azure Database for MySQL by using a Java application and the JDBC driver MariaDB Connector/J. You then use SQL statements to query, insert, update, and delete data in the database from Mac, Ubuntu Linux, and Windows platforms.
16
+
17
+
This topic assumes that you're familiar with developing using Java, but you're new to working with Azure Database for MySQL.
16
18
17
19
## Prerequisites
18
-
1. This quickstart uses the resources created in either of these guides as a starting point:
19
-
-[Create an Azure Database for MySQL server using Azure portal](./quickstart-create-mysql-server-database-using-azure-portal.md)
20
-
-[Create an Azure Database for MySQL server using Azure CLI](./quickstart-create-mysql-server-database-using-azure-cli.md)
21
20
22
-
2. Ensure your Azure Database for MySQL connection security is configured with the firewall opened and SSL settings adjusted for your application to connect successfully.
21
+
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio).
22
+
- An Azure Database for MySQL server. [Create an Azure Database for MySQL server using Azure portal](quickstart-create-mysql-server-database-using-azure-portal.md) or [Create an Azure Database for MySQL server using Azure CLI](quickstart-create-mysql-server-database-using-azure-cli.md).
23
+
- Azure Database for MySQL connection security is configured with the firewall opened and SSL connection settings configured for your application.
24
+
25
+
## Obtain the MariaDB connector
23
26
24
-
3.Obtain the MariaDB Connector/J connector using one of the following approaches:
27
+
Obtain the [MariaDB Connector/J](https://mariadb.com/kb/en/library/mariadb-connector-j/) connector using one of the following approaches:
25
28
- Use the Maven package [mariadb-java-client](https://search.maven.org/search?q=a:mariadb-java-client) to include the [mariadb-java-client dependency](https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client) in the POM file for your project.
26
-
- Download the JDBC driver [MariaDB Connector/J](https://downloads.mariadb.org/connector-java/) and include the JDBC jar file (for example mariadb-java-client-2.4.3.jar) into your application classpath. If you have trouble with classpaths, consult your environment's documentation for class path specifics, such as [Apache Tomcat](https://tomcat.apache.org/tomcat-7.0-doc/class-loader-howto.html) or [Java SE](https://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html)
29
+
- Download the JDBC driver [MariaDB Connector/J](https://downloads.mariadb.org/connector-java/) and include the JDBC jar file (for example mariadb-java-client-2.4.3.jar) into your application class path. Consult your environment's documentation for class path specifics, such as [Apache Tomcat](https://tomcat.apache.org/tomcat-7.0-doc/class-loader-howto.html) or [Java SE](https://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html)
27
30
28
31
## Get connection information
32
+
29
33
Get the connection information needed to connect to the Azure Database for MySQL. You need the fully qualified server name and login credentials.
30
34
31
35
1. Log in to the [Azure portal](https://portal.azure.com/).
@@ -35,6 +39,7 @@ Get the connection information needed to connect to the Azure Database for MySQL
35
39

36
40
37
41
## Connect, create table, and insert data
42
+
38
43
Use the following code to connect and load the data using the function with an **INSERT** SQL statement. The [getConnection()](https://mariadb.com/kb/en/library/about-mariadb-connector-j/#using-drivermanager) method is used to connect to MySQL. Methods [createStatement()](https://mariadb.com/kb/en/library/about-mariadb-connector-j/#creating-a-table-on-a-mariadb-or-mysql-server) and execute() are used to drop and create the table. The prepareStatement object is used to build the insert commands, with setString() and setInt() to bind the parameter values. Method executeUpdate() runs the command for each set of parameters to insert the values.
39
44
40
45
Replace the host, database, user, and password parameters with the values that you specified when you created your own server and database.
@@ -137,6 +142,7 @@ public class CreateTableInsertRows {
137
142
```
138
143
139
144
## Read data
145
+
140
146
Use the following code to read the data with a **SELECT** SQL statement. The [getConnection()](https://mariadb.com/kb/en/library/about-mariadb-connector-j/#using-drivermanager) method is used to connect to MySQL. Methods [createStatement()](https://mariadb.com/kb/en/library/about-mariadb-connector-j/#creating-a-table-on-a-mariadb-or-mysql-server) and executeQuery() are used to connect and run the select statement. The results are processed using a ResultSet object.
141
147
142
148
Replace the host, database, user, and password parameters with the values that you specified when you created your own server and database.
@@ -224,6 +230,7 @@ public class ReadTable {
224
230
```
225
231
226
232
## Update data
233
+
227
234
Use the following code to change the data with an **UPDATE** SQL statement. The [getConnection()](https://mariadb.com/kb/en/library/about-mariadb-connector-j/#using-drivermanager) method is used to connect to MySQL. The methods [prepareStatement()](https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html) and executeUpdate() are used to prepare and run the update statement.
228
235
229
236
Replace the host, database, user, and password parameters with the values that you specified when you created your own server and database.
@@ -306,6 +313,7 @@ public class UpdateTable {
306
313
```
307
314
308
315
## Delete data
316
+
309
317
Use the following code to remove data with a **DELETE** SQL statement. The [getConnection()](https://mariadb.com/kb/en/library/about-mariadb-connector-j/#using-drivermanager) method is used to connect to MySQL. The methods [prepareStatement()](https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html) and executeUpdate() are used to prepare and run the delete statement.
310
318
311
319
Replace the host, database, user, and password parameters with the values that you specified when you created your own server and database.
Copy file name to clipboardExpand all lines: articles/mysql/connect-nodejs.md
+18-8Lines changed: 18 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,21 +10,22 @@ ms.topic: quickstart
10
10
ms.date: 12/02/2019
11
11
---
12
12
# Quickstart: Use Node.js to connect and query data in Azure Database for MySQL
13
-
This quickstart demonstrates how to connect to an Azure Database for MySQL using [Node.js](https://nodejs.org/) from Windows, Ubuntu Linux, and Mac platforms. It shows how to use SQL statements to query, insert, update, and delete data in the database. This topic assumes that you are familiar with developing using Node.js and that you are new to working with Azure Database for MySQL.
13
+
14
+
In this quickstart, you connect to an Azure Database for MySQL by using Node.js. You then use SQL statements to query, insert, update, and delete data in the database from Mac, Ubuntu Linux, and Windows platforms.
15
+
16
+
This topic assumes that you're familiar with developing using Node.js, but you're new to working with Azure Database for MySQL.
14
17
15
18
## Prerequisites
16
-
This quickstart uses the resources created in either of these guides as a starting point:
17
-
-[Create an Azure Database for MySQL server using Azure portal](./quickstart-create-mysql-server-database-using-azure-portal.md)
18
-
-[Create an Azure Database for MySQL server using Azure CLI](./quickstart-create-mysql-server-database-using-azure-cli.md)
19
19
20
-
You also need to:
21
-
- Install the [Node.js](https://nodejs.org) runtime.
22
-
- Install [mysql](https://www.npmjs.com/package/mysql) package to connect to MySQL from the Node.js application.
20
+
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio).
21
+
- An Azure Database for MySQL server. [Create an Azure Database for MySQL server using Azure portal](quickstart-create-mysql-server-database-using-azure-portal.md) or [Create an Azure Database for MySQL server using Azure CLI](quickstart-create-mysql-server-database-using-azure-cli.md).
23
22
24
23
## Install Node.js and the MySQL connector
25
-
Depending on your platform, follow the instructions in the appropriate section to install Node.js. Use npm to install the mysql package and its dependencies into your project folder.
24
+
25
+
Depending on your platform, follow the instructions in the appropriate section to install [Node.js](https://nodejs.org). Use npm to install the [mysql](https://www.npmjs.com/package/mysql) package and its dependencies into your project folder.
26
26
27
27
### **Windows**
28
+
28
29
1. Visit the [Node.js downloads page](https://nodejs.org/en/download/), and then select your desired Windows installer option.
29
30
2. Make a local project folder such as `nodejsmysql`.
30
31
3. Open the command prompt, and then change directory into the project folder, such as `cd c:\nodejsmysql\`
@@ -39,6 +40,7 @@ Depending on your platform, follow the instructions in the appropriate section t
39
40
5. Verify the installation by checking the `npm list` output text. The version number may vary as new patches are released.
40
41
41
42
### **Linux (Ubuntu)**
43
+
42
44
1. Run the following commands to install **Node.js** and **npm** the package manager for Node.js.
43
45
44
46
```bash
@@ -56,6 +58,7 @@ Depending on your platform, follow the instructions in the appropriate section t
56
58
3. Verify the installation by checking npm list output text. The version number may vary as new patches are released.
57
59
58
60
### **Mac OS**
61
+
59
62
1. Enter the following commands to install **brew**, an easy-to-use package manager for Mac OS X and **Node.js**.
60
63
61
64
```bash
@@ -74,6 +77,7 @@ Depending on your platform, follow the instructions in the appropriate section t
74
77
3. Verify the installation by checking the `npm list` output text. The version number may vary as new patches are released.
75
78
76
79
## Get connection information
80
+
77
81
Get the connection information needed to connect to the Azure Database for MySQL. You need the fully qualified server name and login credentials.
78
82
79
83
1. Log in to the [Azure portal](https://portal.azure.com/).
@@ -83,12 +87,14 @@ Get the connection information needed to connect to the Azure Database for MySQL
83
87

84
88
85
89
## Running the JavaScript code in Node.js
90
+
86
91
1. Paste the JavaScript code into text files, and then save it into a project folder with file extension .js (such as C:\nodejsmysql\createtable.js or /home/username/nodejsmysql/createtable.js).
87
92
2. Open the command prompt or bash shell, and then change directory into your project folder `cd nodejsmysql`.
88
93
3. To run the application, enter the node command followed by the file name, such as `node createtable.js`.
89
94
4. On Windows, if the node application is not in your environment variable path, you may need to use the full path to launch the node application, such as `"C:\Program Files\nodejs\node.exe" createtable.js`
90
95
91
96
## Connect, create table, and insert data
97
+
92
98
Use the following code to connect and load the data by using **CREATE TABLE** and **INSERT INTO** SQL statements.
93
99
94
100
The [mysql.createConnection()](https://github.com/mysqljs/mysql#establishing-connections) method is used to interface with the MySQL server. The [connect()](https://github.com/mysqljs/mysql#establishing-connections) function is used to establish the connection to the server. The [query()](https://github.com/mysqljs/mysql#performing-queries) function is used to execute the SQL query against MySQL database.
@@ -156,6 +162,7 @@ function queryDatabase(){
156
162
```
157
163
158
164
## Read data
165
+
159
166
Use the following code to connect and read the data by using a **SELECT** SQL statement.
160
167
161
168
The [mysql.createConnection()](https://github.com/mysqljs/mysql#establishing-connections) method is used to interface with the MySQL server. The [connect()](https://github.com/mysqljs/mysql#establishing-connections) method is used to establish the connection to the server. The [query()](https://github.com/mysqljs/mysql#performing-queries) method is used to execute the SQL query against MySQL database. The results array is used to hold the results of the query.
@@ -208,6 +215,7 @@ function readData(){
208
215
```
209
216
210
217
## Update data
218
+
211
219
Use the following code to connect and read the data by using an **UPDATE** SQL statement.
212
220
213
221
The [mysql.createConnection()](https://github.com/mysqljs/mysql#establishing-connections) method is used to interface with the MySQL server. The [connect()](https://github.com/mysqljs/mysql#establishing-connections) method is used to establish the connection to the server. The [query()](https://github.com/mysqljs/mysql#performing-queries) method is used to execute the SQL query against MySQL database.
@@ -256,6 +264,7 @@ function updateData(){
256
264
```
257
265
258
266
## Delete data
267
+
259
268
Use the following code to connect and read the data by using a **DELETE** SQL statement.
260
269
261
270
The [mysql.createConnection()](https://github.com/mysqljs/mysql#establishing-connections) method is used to interface with the MySQL server. The [connect()](https://github.com/mysqljs/mysql#establishing-connections) method is used to establish the connection to the server. The [query()](https://github.com/mysqljs/mysql#performing-queries) method is used to execute the SQL query against MySQL database.
@@ -304,5 +313,6 @@ function deleteData(){
304
313
```
305
314
306
315
## Next steps
316
+
307
317
> [!div class="nextstepaction"]
308
318
> [Migrate your database using Export and Import](./concepts-migrate-import-export.md)
0 commit comments