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 .
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..