JDBC Component
The jdbc: component allows enables you to work with access databases using through JDBC, where SQL queries and operations via SQL text as are sent in the message payloadbody.
This component uses the standard Java JDBC to work with the databaseAPI, unlike the SQL Component component that , which uses spring-jdbc.
Warning |
---|
So far endpoints from this component could be used only as producers. It This component can only be used to define producer endpoints, which means that you cannot use them the JDBC component in a |
URI format
Code Block |
---|
jdbc:dataSourceName[?options] |
This component only supports producer , meaning that you can not use routes with this component in the from
typeendpoints.
You can append query options to the URI in the following format, ?option=value&option=value&...
Options
Name | Default Value | Description |
---|---|---|
| | 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. |
...
The result is returned in the OUT body as a an ArrayList<HashMap<String, Object>>
list object with the result. The List
object contains the list of rows and the Map
contains objects contain each row with the string 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 |
---|---|
| If the query is a select |
| If the query is an update |
Samples
In the sample below following example, we fetch the rows from the customer table.
First we register our datasource in the Camel registry as testdb
:
Wiki Markup |
---|
{snippet:id=register|lang=java|url=camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java} |
Then we configure a route that routes to the JDBC component, so the SQL will be executed, notice that . Note how we refer to the testdb
datasource that was bound in the previous step:
Wiki Markup |
---|
{snippet:id=route|lang=java|url=camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java} |
Or you can create a datasource DataSource
in Spring like this:
Wiki Markup |
---|
{snippet:id=example|lang=java|url=camel/trunk/components/camel-jdbc/src/test/resources/org/apache/camel/component/jdbc/camelContext.xml} |
And then we We create the endpoint and sends the exchange containing an endpoint, add the SQL query to execute in the in bodybody of the IN message, and then send the exchange. The result of the query is returned in the out OUT body.:
Wiki Markup |
---|
{snippet:id=invoke|lang=java|url=camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java} |
...
If we want to poll a database using this the JDBC component, we need to combine this it with a polling scheduler such as the Timer or Quartz etc.
In this sample In the following example, we retrieve data from the database every 60th 60 seconds.:
Code Block | ||||
---|---|---|---|---|
| ||||
from("timer://foo?period=60000").setBody(constant("select * from customer")).to("jdbc:testdb").to("activemq:queue:customers"); |
...