[CONF] Apache Camel: JDBC (page edited)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[CONF] Apache Camel: JDBC (page edited)

Dhiraj Bokde (Confluence)

JDBC has been edited by Claus Ibsen (Jun 01, 2008).

(View changes)

Content:

JDBC Component

The jdbc: component allows you to work with databases using JDBC queries and operations via SQL text as the message payload

URI format

jdbc:dataSourceName?options

Options

Name Default Value Description
readSize 20000 The default maximum number of rows that can be read by a polling query

Result

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.

Samples

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();
context.bind("testdb", ds);

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:

// lets add simple route
camelContext.addRoutes(new RouteBuilder() {
    public void configure() {
        from("direct:hello").to("jdbc:testdb?readSize=100");
    }
});

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);
assertNotNull(out);
assertNotNull(out.getOut());
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);
assertEquals(2, data.size());

See Also