|
JDBC has been edited by Claus Ibsen (Jun 01, 2008). Content:JDBC ComponentThe jdbc: component allows you to work with databases using JDBC queries and operations via SQL text as the message payload URI formatjdbc:dataSourceName?options Options
ResultThe 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. SamplesIn 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 = "" assertNotNull("out body could not be converted to an ArrayList - was: " + out.getOut().getBody(), data); assertEquals(2, data.size()); See Also |
Unsubscribe or edit your notifications preferences
