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