Contact Us 1-800-596-4880

Connecting to a Database from the App

logo cloud IDE Cloud IDE

logo desktop IDE Desktop IDE

Open Beta Release: The cloud IDE is in open beta. Any use of Anypoint Code Builder in its beta state is subject to the applicable beta services terms and conditions, available from the IDE.

Use Anypoint Connector for Database (Database Connector) to connect to an external database from your Mule flow.

Before You Begin

Open Your American Flights Application

  1. In Anypoint Code Builder, open american-ws-anypoint-code-builder.xml.

  2. Remove the <set-payload> component from the configuration XML:

    <set-payload value="Flight info" doc:name="Set Response" />

Add a Database Driver Dependency

The Database Connector supports a variety of Java Database Connectivity (JDBC) databases.

Provide the dependency for the mySQL driver:

  1. Open the Explorer menu by pressing Cmd+Shift+e (Mac) or Ctrl+Shift+e (Windows).

  2. Open the pom.xml file.

  3. Add a new dependency within the <dependencies/> element:

    <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <version>8.0.29</version>
    </dependency>
    pom.xml with new dependency highlighted
  4. If you receive a popup with the message A build file was modified. Do you want to synchronize the Java classpath/configuration?, select Yes.

  5. In the pom.xml, replace the existing <configuration/> element within your mule-maven-plugin with a new shared library:

    <plugin>
        <groupId>org.mule.tools.maven</groupId>
        <artifactId>mule-maven-plugin</artifactId>
        <version>${mule.maven.plugin.version}</version>
        <extensions>true</extensions>
        <configuration>
         <sharedLibraries>
           <sharedLibrary>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
           </sharedLibrary>
         </sharedLibraries>
        </configuration>
    </plugin>
  6. If requested, accept Yes to sync the Java classpath.

  7. Proceed to Add a Database Connector Configuration.

Add a Database Connector Configuration

  1. In Anypoint Code Builder, open american-ws-anypoint-code-builder.xml.

  2. In the configuration XML, add the global <db:config/> snippet.

    1. Place your cursor before the opening <flow> tag.

    2. Press Ctrl+Spacebar (Mac) or Cmd+Spacebar (Windows).

    3. Type mysql to search for the snippet.

    4. Select the snippet db:mysql-config.

      Snippet XML before the opening <flow> tag:
      <db:config name="Mysql_Database_Config" >
        <db:my-sql-connection host="${mysql.host}" port="${mysql.port}"
            user="${mysql.username}" password="${mysql.password}"
            database="${mysql.database}" />
      </db:config>
      
      <flow name="getFlights" >
      ...
    5. Edit the attributes in <db:config/> to produce this XML configuration:

      <db:config name="Database_Config" doc:name="mySQL DB">
          <db:my-sql-connection
            host="mudb.learn.mulesoft.com"
            port="3306"
            user="mule"
            password="mule"
            database="training" />
      </db:config>
  3. Review the complete configuration XML:

    <http:listener-config name="inbound-request" doc:name="HTTP Config">
        <http:listener-connection host="0.0.0.0" port="8081" />
    </http:listener-config>
    
    <db:config name="Database_Config" doc:name="mySQL DB">
        <db:my-sql-connection host="mudb.learn.mulesoft.com" port="3306"
            user="mule" password="mule" database="training" />
    </db:config>
    
    <flow name="getFlights">
        <http:listener path="flights" config-ref="inbound-request"
              doc:name="HTTP /flights" />
    </flow>
  4. Proceed to Write a Query to Return All Flights.

Write a Query to Return All Flights

  1. From the canvas UI, click the (Add component) icon after the HTTP Listener component.

  2. Add the Select operation for Anypoint Connector for Database (Database Connector).

    Either search for Select or navigate to the operation from Connectors > Database > Select.

  3. Provide this configuration for the snippet:

    • From the canvas UI

    • From the configuration XML

    Click the Select operation to open its configuration panel, and set the Select attributes to the following values:

    Configure the Select connector through its configuration panel.
    1 Change the connector name to Query Flights.
    2 Select Database_Config from the connector configuration dropdown menu.
    3 Set the Query attribute to Select * FROM american.

    In <db:select/>, set the attributes to the values provided in the example.

    <db:select doc:name="Query Flights" config-ref="Database_Config" >
      <db:sql>
        <![CDATA[Select * FROM american]]>
      </db:sql>
    </db:select>

    Notice that the config-ref attribute references the database connection configuration in <db:config/> by its name.

  4. Review the complete configuration XML:

    <http:listener-config name="inbound-request" doc:name="HTTP Config">
        <http:listener-connection host="0.0.0.0" port="8081" />
    </http:listener-config>
    
    <db:config name="Database_Config" doc:name="mySQL DB">
        <db:my-sql-connection host="mudb.learn.mulesoft.com" port="3306"
            user="mule" password="mule" database="training" />
    </db:config>
    
    <flow name="getFlights">
        <http:listener path="flights" config-ref="inbound-request" doc:name="HTTP /flights" />
        <db:select doc:name="Query Flights" config-ref="Database_Config" doc:id="qcnfxf" >
            <db:sql>
                <![CDATA[Select * FROM american]]>
            </db:sql>
        </db:select>
    </flow>
  5. Proceed to Transforming Flight Data with DataWeave to learn how to use DataWeave to make the database response match examples in your American Flight API specification.