SlideShare a Scribd company logo
1 of 13
Integrate with
Database by
Groovy
There are several ways of using this database
component in Mule. But today I will be demonstrating a
simple way of using Mule Database connector with
Groovy.
.
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;password=*
****" />
<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_Gl
l");
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').toInteger
(), 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..

More Related Content

What's hot

MuleSoft ESB - CSV File to Database
MuleSoft ESB - CSV File to DatabaseMuleSoft ESB - CSV File to Database
MuleSoft ESB - CSV File to Databaseakashdprajapati
 
Junit in mule demo
Junit in mule demo Junit in mule demo
Junit in mule demo javeed_mhd
 
Mule Esb Data Weave
Mule Esb Data WeaveMule Esb Data Weave
Mule Esb Data WeaveMohammed246
 
MuleSoft ESB Object Store
MuleSoft ESB Object StoreMuleSoft ESB Object Store
MuleSoft ESB Object Storeakashdprajapati
 
Groovy example in mule
Groovy example in muleGroovy example in mule
Groovy example in muleMohammed246
 
Caching and invalidating with managed store
Caching and invalidating with managed storeCaching and invalidating with managed store
Caching and invalidating with managed storeAnirban Sen Chowdhary
 
Mule with jdbc(my sql)
Mule with jdbc(my sql)Mule with jdbc(my sql)
Mule with jdbc(my sql)charan teja R
 
Mulesoft Using Groovy Component
Mulesoft Using Groovy ComponentMulesoft Using Groovy Component
Mulesoft Using Groovy Componentkumar gaurav
 
Mule ESB SMTP Connector Integration
Mule ESB SMTP Connector  IntegrationMule ESB SMTP Connector  Integration
Mule ESB SMTP Connector IntegrationAnilKumar Etagowni
 
MuleSoft ESB Shared Library
MuleSoft ESB Shared LibraryMuleSoft ESB Shared Library
MuleSoft ESB Shared Libraryakashdprajapati
 

What's hot (15)

MuleSoft ESB - CSV File to Database
MuleSoft ESB - CSV File to DatabaseMuleSoft ESB - CSV File to Database
MuleSoft ESB - CSV File to Database
 
Refreshing mule cache using oracle database change notification
Refreshing mule cache using oracle database change notificationRefreshing mule cache using oracle database change notification
Refreshing mule cache using oracle database change notification
 
Junit in mule demo
Junit in mule demo Junit in mule demo
Junit in mule demo
 
Spring introduction
Spring introductionSpring introduction
Spring introduction
 
Mule new jdbc component
Mule new jdbc componentMule new jdbc component
Mule new jdbc component
 
Passing java arrays in oracle stored procedure from mule esb flow
Passing java arrays in oracle stored procedure from mule esb flowPassing java arrays in oracle stored procedure from mule esb flow
Passing java arrays in oracle stored procedure from mule esb flow
 
Mule Esb Data Weave
Mule Esb Data WeaveMule Esb Data Weave
Mule Esb Data Weave
 
Mule esb :Data Weave
Mule esb :Data WeaveMule esb :Data Weave
Mule esb :Data Weave
 
MuleSoft ESB Object Store
MuleSoft ESB Object StoreMuleSoft ESB Object Store
MuleSoft ESB Object Store
 
Groovy example in mule
Groovy example in muleGroovy example in mule
Groovy example in mule
 
Caching and invalidating with managed store
Caching and invalidating with managed storeCaching and invalidating with managed store
Caching and invalidating with managed store
 
Mule with jdbc(my sql)
Mule with jdbc(my sql)Mule with jdbc(my sql)
Mule with jdbc(my sql)
 
Mulesoft Using Groovy Component
Mulesoft Using Groovy ComponentMulesoft Using Groovy Component
Mulesoft Using Groovy Component
 
Mule ESB SMTP Connector Integration
Mule ESB SMTP Connector  IntegrationMule ESB SMTP Connector  Integration
Mule ESB SMTP Connector Integration
 
MuleSoft ESB Shared Library
MuleSoft ESB Shared LibraryMuleSoft ESB Shared Library
MuleSoft ESB Shared Library
 

Similar to Integrate with database by groovy

Similar to Integrate with database by groovy (20)

Calling database with groovy in mule
Calling database with groovy in muleCalling database with groovy in mule
Calling database with groovy in mule
 
Jsp project module
Jsp project moduleJsp project module
Jsp project module
 
How to execute an oracle stored procedure with nested table as a parameter fr...
How to execute an oracle stored procedure with nested table as a parameter fr...How to execute an oracle stored procedure with nested table as a parameter fr...
How to execute an oracle stored procedure with nested table as a parameter fr...
 
Html web sql database
Html web sql databaseHtml web sql database
Html web sql database
 
Jdbc day-1
Jdbc day-1Jdbc day-1
Jdbc day-1
 
22jdbc
22jdbc22jdbc
22jdbc
 
phptut4
phptut4phptut4
phptut4
 
phptut4
phptut4phptut4
phptut4
 
Lecture 1. java database connectivity
Lecture 1. java database connectivityLecture 1. java database connectivity
Lecture 1. java database connectivity
 
Jdbc drivers
Jdbc driversJdbc drivers
Jdbc drivers
 
171_74_216_Module_5-Non_relational_database_-mongodb.pptx
171_74_216_Module_5-Non_relational_database_-mongodb.pptx171_74_216_Module_5-Non_relational_database_-mongodb.pptx
171_74_216_Module_5-Non_relational_database_-mongodb.pptx
 
Slide Latihan JDBC
Slide Latihan JDBCSlide Latihan JDBC
Slide Latihan JDBC
 
Spring database - part2
Spring database -  part2Spring database -  part2
Spring database - part2
 
Jdbc oracle
Jdbc oracleJdbc oracle
Jdbc oracle
 
Nuxt.JS Introdruction
Nuxt.JS IntrodructionNuxt.JS Introdruction
Nuxt.JS Introdruction
 
Developing node-mdb: a Node.js - based clone of SimpleDB
Developing node-mdb: a Node.js - based clone of SimpleDBDeveloping node-mdb: a Node.js - based clone of SimpleDB
Developing node-mdb: a Node.js - based clone of SimpleDB
 
Chapter vii(accessing databases with jdbc)
Chapter vii(accessing databases with jdbc)Chapter vii(accessing databases with jdbc)
Chapter vii(accessing databases with jdbc)
 
Java &amp; banco de dados
Java &amp; banco de dadosJava &amp; banco de dados
Java &amp; banco de dados
 
Toms introtospring mvc
Toms introtospring mvcToms introtospring mvc
Toms introtospring mvc
 
Class.bluemix.dbaas
Class.bluemix.dbaasClass.bluemix.dbaas
Class.bluemix.dbaas
 

More from Son Nguyen

Wsdl connector introduction
Wsdl connector introductionWsdl connector introduction
Wsdl connector introductionSon Nguyen
 
Android intergrate with mule
Android intergrate with muleAndroid intergrate with mule
Android intergrate with muleSon Nguyen
 
Mule flow overview
Mule flow overviewMule flow overview
Mule flow overviewSon Nguyen
 
Mule flow and filter
Mule flow and filterMule flow and filter
Mule flow and filterSon Nguyen
 
Handle exceptions in mule
Handle exceptions in muleHandle exceptions in mule
Handle exceptions in muleSon Nguyen
 
Spring security integrate with mule
Spring security integrate with muleSpring security integrate with mule
Spring security integrate with muleSon Nguyen
 
Message processor in mule
Message processor in muleMessage processor in mule
Message processor in muleSon Nguyen
 
Expression language in mule
Expression language in muleExpression language in mule
Expression language in muleSon Nguyen
 
Mule with data weave
Mule with data weaveMule with data weave
Mule with data weaveSon Nguyen
 
Using spring scheduler mule
Using spring scheduler muleUsing spring scheduler mule
Using spring scheduler muleSon Nguyen
 
Composite source in bound and out-bound
Composite source in bound and out-boundComposite source in bound and out-bound
Composite source in bound and out-boundSon Nguyen
 
Batch job processing
Batch job processingBatch job processing
Batch job processingSon Nguyen
 
Using message enricher
Using message enricherUsing message enricher
Using message enricherSon Nguyen
 
Finance connectors with mule
Finance connectors with muleFinance connectors with mule
Finance connectors with muleSon Nguyen
 
Google drive connection
Google drive connectionGoogle drive connection
Google drive connectionSon Nguyen
 
Using properties in mule
Using properties in muleUsing properties in mule
Using properties in muleSon Nguyen
 
Mule integrate with microsoft
Mule integrate with microsoftMule integrate with microsoft
Mule integrate with microsoftSon Nguyen
 
Anypoint connectors
Anypoint connectorsAnypoint connectors
Anypoint connectorsSon Nguyen
 
Mule esb basic introduction
Mule esb basic introductionMule esb basic introduction
Mule esb basic introductionSon Nguyen
 

More from Son Nguyen (20)

Wsdl connector introduction
Wsdl connector introductionWsdl connector introduction
Wsdl connector introduction
 
Android intergrate with mule
Android intergrate with muleAndroid intergrate with mule
Android intergrate with mule
 
Mule flow overview
Mule flow overviewMule flow overview
Mule flow overview
 
Mule flow and filter
Mule flow and filterMule flow and filter
Mule flow and filter
 
Handle exceptions in mule
Handle exceptions in muleHandle exceptions in mule
Handle exceptions in mule
 
Spring security integrate with mule
Spring security integrate with muleSpring security integrate with mule
Spring security integrate with mule
 
Message processor in mule
Message processor in muleMessage processor in mule
Message processor in mule
 
Expression language in mule
Expression language in muleExpression language in mule
Expression language in mule
 
Mule with data weave
Mule with data weaveMule with data weave
Mule with data weave
 
Using spring scheduler mule
Using spring scheduler muleUsing spring scheduler mule
Using spring scheduler mule
 
Composite source in bound and out-bound
Composite source in bound and out-boundComposite source in bound and out-bound
Composite source in bound and out-bound
 
Batch job processing
Batch job processingBatch job processing
Batch job processing
 
Using message enricher
Using message enricherUsing message enricher
Using message enricher
 
Finance connectors with mule
Finance connectors with muleFinance connectors with mule
Finance connectors with mule
 
Google drive connection
Google drive connectionGoogle drive connection
Google drive connection
 
Using properties in mule
Using properties in muleUsing properties in mule
Using properties in mule
 
Mule integrate with microsoft
Mule integrate with microsoftMule integrate with microsoft
Mule integrate with microsoft
 
Jms queue
Jms queueJms queue
Jms queue
 
Anypoint connectors
Anypoint connectorsAnypoint connectors
Anypoint connectors
 
Mule esb basic introduction
Mule esb basic introductionMule esb basic introduction
Mule esb basic introduction
 

Recently uploaded

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 

Recently uploaded (20)

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 

Integrate with database by groovy

  • 2. There are several ways of using this database component in Mule. But today I will be demonstrating a simple way of using Mule Database connector with Groovy. .
  • 3. Let us consider we have a database table with following Data :-
  • 4. 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;password=* ****" /> <spring:property name="driverName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> </spring:bean> </spring:beans>
  • 5. 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" />
  • 6. And finally we will create a simple Mule flow which will access this JDBC connectors using Groovy scripts as follows :-
  • 7. As you can see we are using the following in our Groovy script to call our Database connector: - muleContext.getRegistry().lookupConnector("Database_Gl l"); 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
  • 8. 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').toInteger (), 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 .
  • 9. So, after hitting the url we will get the following :-
  • 10. 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>
  • 11. 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 :-
  • 12. and let’s also check our table in our database. We will get the following:-
  • 13. 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..