How To Connect To Azure SQL Database With MuleSoft

If you would like to understand the basics of MuleSoft, refer to this article before going next:

Database Schema and Procs

This is the database payout which will be queried out from the SQL server to MuleSoft.

How to Connect to Azure SQL Database with MuleSoft

Getting Started,

Start Anypoint Studio to get started.

Create a new Mule Project from the File tab.

How to Connect to Azure SQL Database with MuleSoft

Provide the project name and leave everything default and click Finish.

How to Connect to Azure SQL Database with MuleSoft

Now drag and drop HTTP Listener from Mule Palette to designer view.

How to Connect to Azure SQL Database with MuleSoft

How to Connect to Azure SQL Database with MuleSoft

Now configure Listener to set some properties. Click on Listeners and click on Add button to set the Connector configuration.

How to Connect to Azure SQL Database with MuleSoft

Keep everything the same, click the Test connection and OK button.

How to Connect to Azure SQL Database with MuleSoft

Enter Path value.

How to Connect to Azure SQL Database with MuleSoft

Setup JDBC

You'll need to do the following to connect the MS SQL server with Mulesoft.

Download and install the sqljdbc4.2.jar file. It is required for you to connect to the MS SQL database. http://www.java2s.com/Code/Jar/s/Downloadsqljdbc420jar.htm

Ensure the TCP/IP port is enabled in SQL configuration (default port is 1433).

Add sqljdbc4.2.jar into the build path of your Mule application. Right-click on your application, then do Build Path > Configure Build Path > Libraries > Add External Jars.

How to Connect to Azure SQL Database with MuleSoft

Go to Java Build Path> Libraries > JRE System Library (JDK 8 (Embedded)) > Add External JARs…

How to Connect to Azure SQL Database with MuleSoft

Locate the .jar file from the downloaded location and click Open.

How to Connect to Azure SQL Database with MuleSoft

Click Appy and close button.

Setup SQL Server

Search the database in Mule Palette, click on database > select, and drag and drop on Message flow.

How to Connect to Azure SQL Database with MuleSoft

Select database > Select and go to properties.

How to Connect to Azure SQL Database with MuleSoft

Configure the database connection and other required fields, and click the ADD button.

How to Connect to Azure SQL Database with MuleSoft

Enter required details like Name, connection, Host, Port, User Password, and Database name. I am using the Azure SQL server database for this sample, and click test connection and click OK.

How to Connect to Azure SQL Database with MuleSoft

Enter select query in SQL Query Text.

How to Connect to Azure SQL Database with MuleSoft

Search for Transform Message in Mule Palette,

Drag and drop Transform Message in Message Flow.

How to Connect to Azure SQL Database with MuleSoft

The last thing we must configure is the Transform Message to convert the data from the database to JSON. Use the following DataWeave script.

How to Connect to Azure SQL Database with MuleSoft

How to Connect to Azure SQL Database with MuleSoft

Run and deploy the application.

How to Connect to Azure SQL Database with MuleSoft

Open postman to see the API result set.

How to Connect to Azure SQL Database with MuleSoft

As you can see database output is in JSON format.

Conclusion

In this article, we learned how to connect MuleSoft with the Azure SQL database and get and display data in JSON format.


Similar Articles