You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 10 Next »

JDBC Component

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.

URI format

jdbc:dataSourceName?options

This component only supports producer, meaning that you can not use routes with this component in the from type.

Options

Name

Default Value

Description

readSize

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.

Result

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

Samples

In the sample below we fetch the rows from the customer table.

First we register our datasource in the Camel registry as testdb:

Error formatting macro: snippet: java.lang.NullPointerException

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:

Error formatting macro: snippet: java.lang.NullPointerException

Or you can create a datasource in Spring like this:

Error formatting macro: snippet: java.lang.NullPointerException

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.

Error formatting macro: snippet: java.lang.NullPointerException

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");
  • No labels