|
| 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 | + |
| 8 | +#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. |
| 9 | +--- |
| 10 | + |
| 11 | +## 1 Introduction |
| 12 | + |
| 13 | +It is often required to use and access data from External Database directly in your Mendix App.This how-to describes how to easily and seamlessly connect and use data from an external database using [External Database Connector](https://marketplace.mendix.com/link/component/219862). |
| 14 | + |
| 15 | +External Database Connector supports connecting to below databases: |
| 16 | + |
| 17 | +- MSSQL |
| 18 | +- MySQL |
| 19 | +- PostgreSQL |
| 20 | +- Oracle |
| 21 | + |
| 22 | +This how-to teaches you how to do the following: |
| 23 | + |
| 24 | +* Connect your Mendix App with an external Database. |
| 25 | +* Create and execute different SQL Queries. |
| 26 | + |
| 27 | +Let's try to build below use case using External Database Connector in Mendix App: |
| 28 | + |
| 29 | +Connect to an external database(MySQL) and retrieve list of Requested Products from database, also as a user you can Request for a Product using Mendix App. |
| 30 | + |
| 31 | +## 2 Prerequisites |
| 32 | + |
| 33 | +Let's assume that you already have Mendix app. |
| 34 | + |
| 35 | +* Download the [External Database Connector](https://marketplace.mendix.com/link/component/219862) from the Mendix Marketplace into your app. |
| 36 | + |
| 37 | +* **username** and **password** for signing into the external database |
| 38 | +* Connection details: **Host**, **Port**, **Database name** |
| 39 | + |
| 40 | +if additional connection properties are required to connect, you can alternatively use **“JDBC Connection String”**. |
| 41 | + |
| 42 | +## Steps to follow: |
| 43 | + |
| 44 | +### 1.Establish connection between External database and Mendix app |
| 45 | + |
| 46 | +1. Create new document of type **“External Database document.”**(File -> New Document -> External Database Connection or Rightclick on Module -> Add other -> External Database Connection) |
| 47 | + |
| 48 | +2. Select Database and add Connection details to connect to Database |
| 49 | + |
| 50 | +{{< figure src="/attachments/howto/integration/use-the-external-database-connector/1.png" >}} |
| 51 | + |
| 52 | +3. Use **“Test Connection”** to validate connection of Mendix app with External Database |
| 53 | + |
| 54 | +{{< figure src="/attachments/howto/integration/use-the-external-database-connector/2.png" >}} |
| 55 | + |
| 56 | +Hurray Connection between your External Database and Mendix app is established successfully. |
| 57 | + |
| 58 | +Click on “Save” to save connection details in 3 constants |
| 59 | + |
| 60 | +- DBSource |
| 61 | +- DBUsername |
| 62 | +- DBPassword |
| 63 | + |
| 64 | +### 2 Explore Tables and Views of a connected Database: |
| 65 | + |
| 66 | +Once connection is successful and saved , you can explore and search different **Tables & Views** of external database in External Database Connection document. |
| 67 | + |
| 68 | +{{< figure src="/attachments/howto/integration/use-the-external-database-connector/3.png" >}} |
| 69 | + |
| 70 | +### 3 Create and Validate SQL Queries with Parameters: |
| 71 | + |
| 72 | +a. Create new query by entering **SQL Query** in query editor box. |
| 73 | +b. Add **Parameters** if you want to use constants or variables in your query during runtime(use curly braces to include parameters in query). |
| 74 | +c. Testvalue should be assigned to the parameter using **Add Parameter**. |
| 75 | +d. **Run Query** to validate query and see the response. |
| 76 | + |
| 77 | +for example: |
| 78 | +Below query retrieves list of RequestedProductRequirement where ProductLine is "Planes" |
| 79 | +SQL Query : |
| 80 | +`Select requestedProductRequirement from productlines where productLine = {productLine}` |
| 81 | + |
| 82 | +{{< figure src="/attachments/howto/integration/use-the-external-database-connector/4.png" >}} |
| 83 | + |
| 84 | +### 4a Save Query & Create Enity: |
| 85 | + |
| 86 | +a.For query which returns resultset for example Select query, use **Use Response** to create mapping. |
| 87 | +b.In **Response Structure** you can see Entity preview which will be created with mapping to the corresponding query response. |
| 88 | +c. Click on **Save Query & Create Enity** to create new Entity in domain model and Save Query with mapping. |
| 89 | + |
| 90 | +{{< figure src="/attachments/howto/integration/use-the-external-database-connector/5.png" >}} |
| 91 | + |
| 92 | +### 4b Save Query: |
| 93 | + |
| 94 | +a.For queries which doesnot return resultset, |
| 95 | +Number of affected rows will be displayed as a Reponse. |
| 96 | +For example: |
| 97 | +`INSERT INTO classicmodels.productlines(productLine, requestedProductRequirement)VALUES({productLine}, {requestedProductRequirement})` |
| 98 | +b.Click **Save Query** |
| 99 | +{{< figure src="/attachments/howto/integration/use-the-external-database-connector/6.png" >}} |
| 100 | + |
| 101 | +**For all DML Queries, changes made to database in Mendix Design phase are automatically rolled back** |
| 102 | + |
| 103 | +### 5 Use “Query External Database” activity: |
| 104 | + |
| 105 | +a.Use "Query External Database" activity from toolbox in the microflow. |
| 106 | +b.Double click on activity and Select created External Database Connection document. |
| 107 | +c.All the queries created for the selected document, will appear in the dropdown, select the required one. |
| 108 | +d.Assign values to the parameters using "Expression editor". |
| 109 | +e.Output for the selected queries will be auto-populated based on the mapping of selected query. |
| 110 | + |
| 111 | +{{< figure src="/attachments/howto/integration/use-the-external-database-connector/7.png" >}} |
| 112 | + |
| 113 | +With these steps you are ready to use data in your Mendix App. |
| 114 | + |
| 115 | +{{% alert color="warning" %}} |
| 116 | +It is your responsibility to apply the proper security, as this action can allow for SQL injection into your Mendix application. Among others things, do not use user-supplied or environment-supplied variables in your SQL statement; if possible, you should prefer them to be static. |
| 117 | +{{% /alert %}} |
0 commit comments