|
| 1 | +--- |
| 2 | +title: "Use the External Database Connector" |
| 3 | +url: /howto/integration/use-the-external-database-connector |
| 4 | +weight: 21 |
| 5 | +description: "Overview of the External Database Connector in Studio Pro" |
| 6 | + |
| 7 | +#If moving or renaming this doc file, implement a temporary redirect and let the respective team (buildpack) know they should update the URL in the product. See Mapping to Products for more details. |
| 8 | +--- |
| 9 | + |
| 10 | +## 1 Introduction |
| 11 | + |
| 12 | +Use the [External Database Connector](https://marketplace.mendix.com/link/component/219862) to connect, retrieve, and insert data into your Mendix app. |
| 13 | + |
| 14 | +The External Database Connector supports connections to the following databases: |
| 15 | + |
| 16 | +* MSSQL |
| 17 | +* MySQL |
| 18 | +* PostgreSQL |
| 19 | +* Oracle |
| 20 | + |
| 21 | +This how-to teaches you how to do the following: |
| 22 | + |
| 23 | +* Connect your Mendix App to an external database |
| 24 | +* Create and validate SQL Queries |
| 25 | +* Use created queries in the **Query External Database** activity |
| 26 | + |
| 27 | +## 2 Prerequisites |
| 28 | + |
| 29 | +Download the [External Database Connector](https://marketplace.mendix.com/link/component/219862) into your app. Make sure you have the following details for your external connection: |
| 30 | + |
| 31 | +* **Username** and **Password** for signing into the external database |
| 32 | +* Connection details: **Host**, **Port**, **Database name** |
| 33 | + |
| 34 | +If additional connection properties are required to connect, you can alternatively use **JDBC Connection String**. |
| 35 | + |
| 36 | +## 3 Connect to the external database |
| 37 | + |
| 38 | +### 3.1 Establish connection between the external database and Mendix app |
| 39 | + |
| 40 | +1. Right-click the module you want to add the external database document to and click **Add other** > **External database connection**. |
| 41 | + |
| 42 | +2. Select the database you want to connect to and add the connection details the Database Connection wizard. |
| 43 | + |
| 44 | +{{< figure src="/attachments/howto/integration/use-the-external-database-connector/1.png" >}} |
| 45 | + |
| 46 | +3. Click **“Test Connection”** to validate the connection to the external database. |
| 47 | + |
| 48 | +{{< figure src="/attachments/howto/integration/use-the-external-database-connector/2.png" >}} |
| 49 | + |
| 50 | +Click **Save** to save the connection details, which are stored in 3 constants: |
| 51 | + |
| 52 | +- `\<Document Name\>_DBSource` |
| 53 | +- `\<Document Name\>_DBUsername` |
| 54 | +- `\<Document Name\>_DBPassword` |
| 55 | + |
| 56 | +For example: `*Database*_DBsource.` |
| 57 | + |
| 58 | +### 3.2 Explore Tables and Views of a Connected Database |
| 59 | + |
| 60 | +Once the connection is successful and saved, you can explore and search the **Tables & Views** of the external database. |
| 61 | + |
| 62 | +{{< figure src="/attachments/howto/integration/use-the-external-database-connector/3.png" >}} |
| 63 | + |
| 64 | +### 3.3 Create and Validate SQL Queries with Parameters |
| 65 | + |
| 66 | +1. Create a new query by entering an SQL query in the **SQL query** field. |
| 67 | + |
| 68 | +2. Click **Add Parameter** if you want to use constants or variables in your query during runtime. Use curly braces to include a parameter in the query. |
| 69 | + |
| 70 | +3. Assign a Test Value to each parameter. |
| 71 | + |
| 72 | +4. Click **Run Query** to validate the query and view the response. |
| 73 | + |
| 74 | +For example, the query below retrieves a list of RequestedProductRequirement where the ProductLine is "Planes" |
| 75 | + |
| 76 | +SQL Query: |
| 77 | +`Select requestedProductRequirement from productlines where productLine = {productLine}` |
| 78 | + |
| 79 | +{{< figure src="/attachments/howto/integration/use-the-external-database-connector/4.png" >}} |
| 80 | + |
| 81 | +## 4. Save Query |
| 82 | + |
| 83 | +### 4.1 Save Query to Retrieve Data: |
| 84 | + |
| 85 | +1. Click **Use Response** to view the response data and mapping. |
| 86 | + |
| 87 | +2. In the **Response Structure** tab, you can view the entity. |
| 88 | + |
| 89 | +3. Click **Save Query & Create Entity** to save the query and the newly created entity in the domain model. |
| 90 | + |
| 91 | +{{< figure src="/attachments/howto/integration/use-the-external-database-connector/5.png" >}} |
| 92 | + |
| 93 | +### 5 Save DML Query |
| 94 | + |
| 95 | +1. For DML queries, *Number of affected rows* will be displayed as a reponse. |
| 96 | + |
| 97 | +For example, `INSERT INTO classicmodels.productlines(productLine, requestedProductRequirement)VALUES({productLine}, {requestedProductRequirement})` |
| 98 | + |
| 99 | +2. Click **Save Query**. |
| 100 | + |
| 101 | +{{< figure src="/attachments/howto/integration/use-the-external-database-connector/6.png" >}} |
| 102 | + |
| 103 | +{{% alert type="info" %}} For all DML Queries, changes made to database in Mendix Design phase are automatically rolled back. {% /alert %}} |
| 104 | + |
| 105 | +### 6 Use the Query External Database Activity |
| 106 | + |
| 107 | +1. Add the **Query external database** activity into your microflow. |
| 108 | + |
| 109 | +2. Double-click on the activity and select the new external database document. |
| 110 | + |
| 111 | +3. In the **Query** field, select the required query from the drop-down. |
| 112 | + |
| 113 | +4. Assign values to the parameters using the **Expression editor**. |
| 114 | + |
| 115 | +5. Output details for the selected query auto-populate. |
| 116 | + |
| 117 | +{{< figure src="/attachments/howto/integration/use-the-external-database-connector/7.png" >}} |
| 118 | + |
| 119 | +You are now ready to use data from an external database in your Mendix App. |
| 120 | + |
| 121 | +{{% alert color="warning" %}} |
| 122 | +Make sure to use secure measures, as this action can allow for SQL injection into your app. Do not use user-supplied or environment-supplied variables in your SQL statement; if possible, they should be static. |
| 123 | +{{% /alert %}} |
0 commit comments