SlideShare a Scribd company logo
By Anirban Sen Chowdhary
.
.
Let us consider we have a database table with following Data :-
Now we will create a simple Mule flow which will retrieve these
data using Mule Database connector with Groovy.
So first let us create a simple Mule flow, in which I will configure
my database configuration using spring beans as follows:-
<spring:beans>
<spring:bean id="DB_Source" name="DB_Source"
class="org.enhydra.jdbc.standard.StandardDataSource">
<spring:property name="url"
value="jdbc:sqlserver://ANIRBAN-
PCSQLEXPRESS:1433;databaseName=TestDB;user=sa;pass
word=*****" />
<spring:property name="driverName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
</spring:bean>
</spring:beans>
Then we will define our JDBC connector as follow :-
So it is very simple right ?
<jdbc-ee:connector name="Database_Global" dataSource-
ref="DB_Source" validateConnections="true"
queryTimeout="-1“ pollingFrequency="0"
doc:name="Database" />
And finally we will create a simple Mule flow which will
access this JDBC connectors using Groovy scripts as
follows :-
As you can see we are using the following in our Groovy
script to call our Database connector: -
muleContext.getRegistry().lookupConnector("Database_G
lobal");
So now, we can test our code .. so for that we need to
run our Mule application.
Once our application is deployed and running we can hit the following
url to retrieve the data on browser :-
http://localhost:8082/groovy/?id=1
As you can see here we are passing a query parameter in our url
which contains the id information id=1
If you closely look into the groovy script we have define
our SQL query in it :-
Object[] queryResult = (Object[]) qr.query(conn,
'SELECT * from table1 where
id=?',message.getInboundProperty('id').toIntege
r(), new ArrayHandler());
So, what it is doing, it is fetching the query parameter
id=1 here :-
message.getInboundProperty('id').toInteger()
and passing it into the SQL query .
So, after hitting the url we will get the following :-
In the same way you can use other SQL queries for you
Database operation using Groovy scripts.
Now, if we want to delete a row from the table we will be
using the same way, but this time the SQL query will be
changed as follow :-
<flow name="GroovyWithJDBCFlow1" doc:name="GroovyWithJDBCFlow1">
<http:inbound-endpoint exchange-pattern="request-response"
<scripting:component doc:name="Initialise Database">
<scripting:script engine="Groovy">
<scripting:text><![CDATA[
import org.apache.commons.dbutils.handlers.ArrayHandler;
jdbcConnector = muleContext.getRegistry().lookupConnector("Database_Global");
qr = jdbcConnector.getQueryRunner();
conn = jdbcConnector.getConnection();
Object[] queryResult = (Object[]) qr.query(conn, 'SELECT * from table1 where
id=?',message.getInboundProperty('id').toInteger(), new ArrayHandler());
message.setInvocationProperty('dataExists', queryResult.toString());
log.info('Response '+message.getInvocationProperty('dataExists'));
qr.update(conn, 'DELETE FROM table1 where ID=?',
message.getInboundProperty('id').toInteger());
return 'Data deleted from Database !!!';
]]>
</scripting:text>
</scripting:script>
</scripting:component>
</flow>
Now, since we have changed the flow with SQL query
DELETE FROM table1 where ID=? ,
We will be hitting the same url
http://localhost:8082/groovy/?id=1 on browser and
expect our code to delete the row with id=1
Now, let’s check what will happen. We will get the
following on browser :-
and let’s also check our table in our database. We will
get the following:-
As you can see above clearly that the row with id=1 has
been deleted.
So, now it’s your turn to experiment with Groovy script
and calling Mule JDBC connector with different SQL
Queries..
Calling database with groovy in mule

More Related Content

PPT
Anypoint data gateway
PPTX
Database component in mule demo
PPTX
Mule new jdbc component
PPT
Mule anypoint data gateway
PPTX
Mule with stored procedure
PPTX
For each component in mule demo
PPTX
Mule Soft ESB - SAP Outbound
PPTX
MuleSoft Consuming Soap Web Service - CXF jax-ws-client Module
Anypoint data gateway
Database component in mule demo
Mule new jdbc component
Mule anypoint data gateway
Mule with stored procedure
For each component in mule demo
Mule Soft ESB - SAP Outbound
MuleSoft Consuming Soap Web Service - CXF jax-ws-client Module

What's hot (18)

PPTX
Mule intelli j tips
PPTX
Database component in mule
PPT
Anypoint data gateway
PPTX
Caching and invalidating with managed store
PPTX
Mule accessing multiple database in parallel
PPTX
Send email attachment using smtp in mule esb
PPTX
Choice component in mule
PPTX
Quartz component in mule demo
PPTX
Stored procedure in Mule
PPTX
Mule soap
PPTX
Activemq installation and master slave setup using shared broker data
PPTX
Using mule with web services
PPTX
Mule esb
PDF
Introduction to JCA and MDB
PPTX
MuleSoft ESB Composite Source
PPT
Maven introduction in Mule
PDF
How to adapt MicroProfile API for generic Web applications
PPTX
Concepts in mule
Mule intelli j tips
Database component in mule
Anypoint data gateway
Caching and invalidating with managed store
Mule accessing multiple database in parallel
Send email attachment using smtp in mule esb
Choice component in mule
Quartz component in mule demo
Stored procedure in Mule
Mule soap
Activemq installation and master slave setup using shared broker data
Using mule with web services
Mule esb
Introduction to JCA and MDB
MuleSoft ESB Composite Source
Maven introduction in Mule
How to adapt MicroProfile API for generic Web applications
Concepts in mule
Ad

Similar to Calling database with groovy in mule (20)

PPTX
Integrate with database by groovy
PPTX
Mule jdbc
PPTX
Mule connectors-session1
PPTX
Muleesbcomponents1 160625154208
PPTX
Mule ESB Components
PPTX
PPTX
Mule connectors-part 1
PPTX
Database integrate with mule
PPTX
Mule data bases
DOCX
Mule with jdbc(my sql)
PPTX
Bean as Datasource
PPTX
Database component
 
PPTX
Mule Database component
PPTX
Database component
PPTX
Database component in mule
PPTX
Groovy demo
PPTX
Groovydemo 160721051742
PPTX
Mule esb db_2
PPTX
Mule esb db_1
PPTX
Mule core concepts
Integrate with database by groovy
Mule jdbc
Mule connectors-session1
Muleesbcomponents1 160625154208
Mule ESB Components
Mule connectors-part 1
Database integrate with mule
Mule data bases
Mule with jdbc(my sql)
Bean as Datasource
Database component
 
Mule Database component
Database component
Database component in mule
Groovy demo
Groovydemo 160721051742
Mule esb db_2
Mule esb db_1
Mule core concepts
Ad

More from Anirban Sen Chowdhary (20)

PPTX
Change the game with Game changer
PPTX
Ring central desktop app overview
PPTX
Overview in ringcentral digital line
PPTX
Some basics with ring central
PPTX
Ring central and python
PPTX
RingCentral application development overview
PPTX
Cloze connect ringcentral
PPTX
Overview on ring central errors part 4
PPTX
Setting up your ring central sandbox in steps
PPTX
Overview on ring central errors: part 2
PPTX
Overview on ring central errors
PPTX
Call recording overview ring central
PPTX
Ring central engaging with amazon alexa
PPTX
How ring central sdk changing the game
PPTX
When ring central connect salesforce
PPTX
Mule 4 connecting ring central
PPTX
Ring central sdk
PPTX
Ring central with okta
PPTX
Ring central connecting salesforce overview
PPTX
Ring central call logs overview (part 2)
Change the game with Game changer
Ring central desktop app overview
Overview in ringcentral digital line
Some basics with ring central
Ring central and python
RingCentral application development overview
Cloze connect ringcentral
Overview on ring central errors part 4
Setting up your ring central sandbox in steps
Overview on ring central errors: part 2
Overview on ring central errors
Call recording overview ring central
Ring central engaging with amazon alexa
How ring central sdk changing the game
When ring central connect salesforce
Mule 4 connecting ring central
Ring central sdk
Ring central with okta
Ring central connecting salesforce overview
Ring central call logs overview (part 2)

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
cuic standard and advanced reporting.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Electronic commerce courselecture one. Pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
MYSQL Presentation for SQL database connectivity
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPT
Teaching material agriculture food technology
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
cuic standard and advanced reporting.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Encapsulation_ Review paper, used for researhc scholars
Electronic commerce courselecture one. Pdf
Network Security Unit 5.pdf for BCA BBA.
Diabetes mellitus diagnosis method based random forest with bat algorithm
Reach Out and Touch Someone: Haptics and Empathic Computing
The Rise and Fall of 3GPP – Time for a Sabbatical?
MYSQL Presentation for SQL database connectivity
NewMind AI Weekly Chronicles - August'25 Week I
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Teaching material agriculture food technology
Building Integrated photovoltaic BIPV_UPV.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Advanced methodologies resolving dimensionality complications for autism neur...

Calling database with groovy in mule

  • 1. By Anirban Sen Chowdhary
  • 2. .
  • 3. .
  • 4. Let us consider we have a database table with following Data :-
  • 5. Now we will create a simple Mule flow which will retrieve these data using Mule Database connector with Groovy. So first let us create a simple Mule flow, in which I will configure my database configuration using spring beans as follows:- <spring:beans> <spring:bean id="DB_Source" name="DB_Source" class="org.enhydra.jdbc.standard.StandardDataSource"> <spring:property name="url" value="jdbc:sqlserver://ANIRBAN- PCSQLEXPRESS:1433;databaseName=TestDB;user=sa;pass word=*****" /> <spring:property name="driverName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> </spring:bean> </spring:beans>
  • 6. Then we will define our JDBC connector as follow :- So it is very simple right ? <jdbc-ee:connector name="Database_Global" dataSource- ref="DB_Source" validateConnections="true" queryTimeout="-1“ pollingFrequency="0" doc:name="Database" />
  • 7. And finally we will create a simple Mule flow which will access this JDBC connectors using Groovy scripts as follows :-
  • 8. As you can see we are using the following in our Groovy script to call our Database connector: - muleContext.getRegistry().lookupConnector("Database_G lobal"); So now, we can test our code .. so for that we need to run our Mule application. Once our application is deployed and running we can hit the following url to retrieve the data on browser :- http://localhost:8082/groovy/?id=1 As you can see here we are passing a query parameter in our url which contains the id information id=1
  • 9. If you closely look into the groovy script we have define our SQL query in it :- Object[] queryResult = (Object[]) qr.query(conn, 'SELECT * from table1 where id=?',message.getInboundProperty('id').toIntege r(), new ArrayHandler()); So, what it is doing, it is fetching the query parameter id=1 here :- message.getInboundProperty('id').toInteger() and passing it into the SQL query .
  • 10. So, after hitting the url we will get the following :-
  • 11. In the same way you can use other SQL queries for you Database operation using Groovy scripts. Now, if we want to delete a row from the table we will be using the same way, but this time the SQL query will be changed as follow :- <flow name="GroovyWithJDBCFlow1" doc:name="GroovyWithJDBCFlow1"> <http:inbound-endpoint exchange-pattern="request-response" <scripting:component doc:name="Initialise Database"> <scripting:script engine="Groovy"> <scripting:text><![CDATA[ import org.apache.commons.dbutils.handlers.ArrayHandler; jdbcConnector = muleContext.getRegistry().lookupConnector("Database_Global"); qr = jdbcConnector.getQueryRunner(); conn = jdbcConnector.getConnection(); Object[] queryResult = (Object[]) qr.query(conn, 'SELECT * from table1 where id=?',message.getInboundProperty('id').toInteger(), new ArrayHandler()); message.setInvocationProperty('dataExists', queryResult.toString()); log.info('Response '+message.getInvocationProperty('dataExists')); qr.update(conn, 'DELETE FROM table1 where ID=?', message.getInboundProperty('id').toInteger()); return 'Data deleted from Database !!!'; ]]> </scripting:text> </scripting:script> </scripting:component> </flow>
  • 12. Now, since we have changed the flow with SQL query DELETE FROM table1 where ID=? , We will be hitting the same url http://localhost:8082/groovy/?id=1 on browser and expect our code to delete the row with id=1 Now, let’s check what will happen. We will get the following on browser :-
  • 13. and let’s also check our table in our database. We will get the following:-
  • 14. As you can see above clearly that the row with id=1 has been deleted. So, now it’s your turn to experiment with Groovy script and calling Mule JDBC connector with different SQL Queries..