The jdbc: component allows you to work with databases using JDBC queries and operations via SQL text as the message payload.
This component uses standard Java JDBC to work with the database, unlike the SQL Component component that uses spring-jdbc.
So far endpoints from this component could be used only as producers. It means that you cannot use them in from() statement.
This component only supports producer, meaning that you can not use routes with this component in the from type.
0 / 2000
The default maximum number of rows that can be read by a polling query. The default value is 2000 for Camel 1.5.0 or older. In newer releases the default value is 0.
The result is returned in the OUT body as a ArrayList<HashMap<String, Object>> list object with the result. The List contains the list of rows and the Map contains each row with the string key as the column name.
Note: This component fetches ResultSetMetaData to be able to return the column name as the key in the Map.
If the query is an update query the update count is returned in the OUT header jdbc.updateCount
In the sample below we fetch the rows from the customer table.
First we register our datasource in the Camel registry as testdb:
Or you can create a datasource in Spring like this:
<camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring">
<constant>select * from customer</constant>
<!-- Just add a demo to show how to bind a date source for camel in Spring-->
<bean id="testdb" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:mem:camel_jdbc" />
<property name="username" value="sa" />
<property name="password" value="" />
And then we create the endpoint and sends the exchange containing the SQL query to execute in the in body. The result is returned in the out body.
// first we create our exchange using the endpoint
Endpoint endpoint = context.getEndpoint("direct:hello");
Exchange exchange = endpoint.createExchange();
// then we set the SQL on the in body
exchange.getIn().setBody("select * from customer order by ID");
// now we send the exchange to the endpoint, and receives the response from Camel
Exchange out = template.send(endpoint, exchange);
// assertions of the response
ArrayList<HashMap<String, Object>> data = out.getOut().getBody(ArrayList.class);
assertNotNull("out body could not be converted to an ArrayList - was: "
+ out.getOut().getBody(), data);
HashMap<String, Object> row = data.get(0);
row = data.get(1);
Sample - Polling the database every minute
If we want to poll a database using this component we need to combine this with a polling scheduler such as the Timer or Quartz etc.
In this sample we retrieve data from the database every 60th seconds.
from("timer://foo?period=60000").setBody(constant("select * from customer")).to("jdbc:testdb").to("activemq:queue:customers");