SQL Component
The sql: component allows you to work with databases using JDBC queries. The difference between this component and JDBC component is that in case of SQL the query is a property of the endpoint and it uses message payload as parameters passed to the query.
This component uses spring-jdbc
behind the scenes for the actual SQL handling.
URI format
The SQL component can only be used to define producer endpoints. In other words, you cannot define an SQL endpoint in a from()
statement.
The SQL component uses the following endpoint URI notation:
sql:select * from table where id=# order by name[?options]
Notice that the standard ?
symbol that denotes the parameters to an SQL query is substituted with the #
symbol, because the ?
symbol is used to specify options for the endpoint.
You can append query options to the URI in the following format, ?option=value&option=value&...
Options
Option |
Type |
Default |
Description |
---|---|---|---|
|
|
|
Camel 1.5.1/2.0: Reference to a |
|
|
|
Sets additional options on the Spring |
Treatment of the message body
The SQL component tries to convert the message body to an object of java.util.Iterator
type and then uses this iterator to fill the query parameters (where each query parameter is represented by a #
symbol in the endpoint URI). If the message body is not an array or collection, the conversion results in an iterator that iterates over only one object, which is the body itself.
For example, if the message body is an instance of java.util.List
, the first item in the list is substituted into the first occurrence of #
in the SQL query, the second item in the list is substituted into the second occurrence of #
, and so on.
Result of the query
For select
operations, the result is an instance of List<Map<String, Object>>
type, as returned by the JdbcTemplate.queryForList() method. For update
operations, the result is the number of updated rows, returned as an Integer
.
Header values
When performing update
operations, the SQL Component stores the update count in the following message headers:
Header |
Description |
---|---|
|
Camel 1.x: The number of rows updated for |
|
Camel 2.0: The number of rows updated for |
|
Camel 2.0: The number of rows returned for |
Configuration in Camel 1.5.0 or lower
The SQL component must be configured before it can be used. In Spring, you can configure it as follows:
<bean id="sql" class="org.apache.camel.component.sql.SqlComponent"> <property name="dataSource" ref="myDS"/> </bean> <bean id="myDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/ds" /> <property name="username" value="username" /> <property name="password" value="password" /> </bean>
Configuration in Camel 1.5.1 or higher
You can now set a reference to a DataSource
in the URI directly:
sql:select * from table where id=# order by name?dataSourceRef=myDS
Sample
In the sample below we execute a query and retrieve the result as a List
of rows, where each row is a Map<String, Object
and the key is the column name.
First, we set up a table to use for our sample. As this is based on an unit test, we do it java code:
Then we configure our route and our sql
component. Notice that we use a direct
endpoint in front of the sql
endpoint. This allows us to send an exchange to the direct
endpoint with the URI, direct:simple
, which is much easier for the client to use than the long sql:
URI. Note that the DataSource
is looked up up in the registry, so we can use standard Spring XML to configure our DataSource
.
And then we fire the message into the direct
endpoint that will route it to our sql
component that queries the database.
We could configure the DataSource
in Spring XML as follows:
<jee:jndi-lookup id="myDS" jndi-name="jdbc/myDataSource"/>