The jdbc: component allows you to work with databases using JDBC queries and operations via SQL text as the message payload
The default maximum number of rows that can be read by a polling query
The result is returned in the out body as a ArrayList<HashMap<String, Object>> list object with the result. The ArrayList contains the list of rows and the Map contains each row with the string key as the column name.
In the sample below we fetches the rows from the customer table.
First we register our datasource in the Camel registry as testdb:
JndiContext context = new JndiContext();
CamelContext camelContext = new DefaultCamelContext(context);
Then we configure a route that routes to the JDBC component so the SQL will be executed, notice that we refer to the testdb datasource that was bound in the previous step:
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.
Endpoint<Exchange> endpoint = camelContext.getEndpoint("direct:hello");
CamelTemplate<Exchange> template = new CamelTemplate<Exchange>(camelContext);
Exchange exchange = endpoint.createExchange();
exchange.getIn().setBody("select * from customer");
Exchange out = template.send(endpoint, exchange);
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);