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

Compare with Current View Page History

« Previous Version 12 Next »

JDBC Component

The jdbc component enables you to access databases through JDBC, where SQL queries and operations are sent in the message body. This component uses the standard JDBC API, unlike the SQL Component component, which uses spring-jdbc.

This component can only be used to define producer endpoints, which means that you cannot use the JDBC component in a from() statement.

URI format

jdbc:dataSourceName[?options]

This component only supports producer endpoints.

You can append query options to the URI in the following format, ?option=value&option=value&...

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 an ArrayList<HashMap<String, Object>>. The List object contains the list of rows and the Map objects contain 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.

Message Headers

Header

Description

CamelJdbcRowCount

If the query is a SELECT, query the row count is returned in this OUT header.

CamelJdbcUpdateCount

If the query is an UPDATE, query the update count is returned in this OUT header.

Samples

In the following example, 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. Note how 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

We create an endpoint, add the SQL query to the body of the IN message, and then send the exchange. The result of the query 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 the JDBC component, we need to combine it with a polling scheduler such as the Timer or Quartz etc. In the following example, we retrieve data from the database every 60 seconds:

from("timer://foo?period=60000").setBody(constant("select * from customer")).to("jdbc:testdb").to("activemq:queue:customers");
  • No labels