Database Connector – Get Started – Mule 4

Anypoint Connector for Database (Database Connector) establishes communication between your Mule app and a relational database.

Database Connector can connect to almost any Java Database Connectivity (JDBC) relational database and run SQL operations. You can specify Dataweave expressions in connector fields and configure attributes dynamically, depending on the database configuration you use. An application can support multi-tenant scenarios using the same configuration element, changing the connection attributes based on, for example, information coming from each request.

You can perform predefined queries, dynamically constructed queries, and template queries that are self-sufficient and customizable. You can perform multiple SQL requests in a single bulk update and make Data Definition Language (DDL) requests that alter the data structure rather than the data itself.

About Connectors

Anypoint connectors are Mule runtime engine extensions that enable you to connect to APIs and resources on external systems, such as Salesforce, Database, ServiceNow, and Twitter.

Prerequisites

Before creating an app, you must have access to the database target resource and Anypoint Platform. You must also understand how to create a Mule app using Flow Designer or Anypoint Studio. For more info Mulesoft Training

Supported Database Types

The Database Connector has connection providers that automatically set the driver class name and create JDBC URLs with the given parameters for the following databases:

  • MySQL
  • Oracle
  • Microsoft SQL Server

You can set up other JDBC databases using a generic JDBC configuration. You can also reference a JDBC DataSource object or a XADataSource object, which is required for XA transactions. You typically create the object in Studio using Spring.

Database Listeners and Operations

The Database connector provides a listener to read from a database in the data source section of a flow. You can execute other operations to read and write to a database anywhere in the Process section. For example, instead of writing single records to a database, bulk operations allow you to modify a collection of records by supplying the SQL to modify each record in the collection.

Other operations allow you to carry out Data Definition Language (DDL) operations, execute stored procedures, or execute entire SQL scripts at once.

Querying a MySQL Database

Illustrates how to use the database connector to connect to a MySQL database. After reading this document and creating and running the example in Mule, you should be able to leverage what you have learned to create an application that connects to a MySQL database.

6596a140-QueryingMySQLDatabase.png

Prerequisites

This document assumes that you are familiar with databases, SQL, HTTP, Mule, Anypoint connectors, Anypoint Studio, elements in a Mule flow, and global elements.

Example Use Case

In the Mule application, an inbound HTTP connector listens for HTTP GET requests in the form: of http://:8081/?lastname=. The HTTP connector passes the value of as one of the message properties to a database connector. The database connector is configured to extract this value and use it in this SQL query:

select first_name from employees where last_name = :lastName

The parameterized query uses the reference to the value of the parameter passed to the HTTP connector. If the HTTP connector receives http://localhost:8081/?lastname=Smith, the SQL query selects the firstname from employees where lastname = Smith.

The database connector instructs the database server to run the SQL query, retrieves the result of the query, and passes it to the Transform message processor which converts the result to JSON. Because the HTTP connector is configured as request-response, the result is returned to the originating HTTP client. Learn more from Mule Training

Set Up and Run the Example

  1. Download and set up the MySQL driver in its default location. You can even check out some of the YouTube videos for assistance with this step.
  2. Start the MySQL server from System Preferences.
  3. Creating the MySQL Database: View the script in the next section of this topic. Navigate to the MySQL driver in a command terminal and paste the script to create a MySQL database called Company that has tables for employees and roles. The script also creates a password protected user and grants it access to the database. username: generatedata; password:generatedata
  4. Open the example project in Anypoint Studio from Anypoint Exchange.
  5. In your application in Studio, click the Global Elements tab. Double-click the HTTP Listener global element to open its Global Element Properties panel. Change the contents of the Port field to the required HTTP port for example, 8081.
  6. Open querying-a-mysql-database.xml file located in src/main/mule directory. Open Database Configuration in Global Elements tab and configure the connection attributes: Host: localhostPort: 3306User: generatedataPassword: generatedataDatabase: company
  7. Configuring the Database Connector for this Example:In this example, the database connector retrieves data from a MySQL database listening on port 3306, the default for MySQL. Ensure that MySQLConfiguration points to the local JDBC MySQL server on your machine.
  8. Run the example application in Anypoint Studio or Standalone
  9. Got to your web browser and type in the following request: http://localhost:8081/?lastname=Puckett You should get the following JSON response: [{“firstname”:”Chava”},{“firstname”:”Quentin”}]

Running the Script on a MySQL Server

Save the MySQL script that follows in the next section to a location on your hard drive.

  1. Open a terminal and run the following command: mysql -u root -D mysql -p
    You are prompted for the MySQL root user’s password. After you type the password, you should see a MySQL prompt: mysql>
  2. Run the MySQL script with the following command, where is the full path and filename to the script, such as /home/joe/create.sample.db.sql. source ; MySQL creates the user, database and tables specified on the script.
  3. To verify the tables, run: use company;show tables;
    The show tables command produces output similar to the following:
    +——————-+ | Tables_in_company | +——————-+ | employees | | roles | +——————-+ 2 rows in set (0.00 sec)
  4. For information about a table, run describe . To see the full contents of a table, run the standard SQL statement select * from .

To exit MySQL, type quit;

To get in-depth knowledge, enroll for a live free demo on Mulesoft Online Training

Leave a comment

Design a site like this with WordPress.com
Get started