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.
![]() | Warning |
|---|---|
So far endpoints from this component could be used only as producers. It means
that you cannot use them in |
SQL component uses following endpoint URI notation
sql:select * from table where id=# order by name?[options]
Notice that standard ? symbol that denotes parameters to SQL query
is substituted with # symbol as ? symbol is used
to specify options for the endpoint.
| Option | Type | Default | Description |
|---|---|---|---|
| dataSourceRef | String | null | FUSE Mediation Router 1.5.1/2.0: Reference to a DataSource to lookup in the registry. |
| template.<xxx> | null | Sets additional options on the Spring JdbcTemplate that is
used under the covers to execute the queries. For instance setting option:
template.maxRows=10. For detailed documentation see JdbcTemplate javadoc documentation. |
This component tries to convert the message body to Iterator and
then, using this iterator it fills parameters of query.
It means that if the body is not an array, collection of iterator, then this conversion will result in iterator that iterates over only one object that is the body itself.
Result of the invocation is effectively List<Map<String,
Object>> as returned from JdbcTemplate.queryForList() method. But for update operations the result is
the number of updated rows returned as an Integer.
When doing update operations the SQL Component stores the update count in the body header
| Header | Description |
|---|---|
| SqlProducer.UPDATE_COUNT | FUSE Mediation Router 1.x: The number of rows updated for update operations, returned as an Integer object. |
CamelSqlUpdateCount
|
FUSE Mediation Router 2.0: The number of rows updated for update operations, returned as an Integer object. |
CamelSqlRowCount
|
FUSE Mediation Router 2.0: The number of rows returned for select operations, returned as an Integer object. |
SQL component has to be configured before it can be used. In Spring it can be done this way:
<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>
You can now set a reference to a dataSoruce in the URI directly
sql:select * from table where id=# order by name?dataSourceRef=myDS
In the sample below we execute a query and retrieve the result as a
List of rows where each rows is a Map<String,
Object where the key is the column name.
First we setup a table to use for our sample. As this is based on an unit test we do it java code:
// this is the database we create with some initial data for our unit test
jdbcTemplate.execute("create table projects (id integer primary key,"
+ "project varchar(10), license varchar(5))");
jdbcTemplate.execute("insert into projects values (1, 'Camel', 'ASF')");
jdbcTemplate.execute("insert into projects values (2, 'AMQ', 'ASF')");
jdbcTemplate.execute("insert into projects values (3, 'Linux', 'GPL')");
Then we configure our route and our sql component. Notice that we use a direct
endpoint in front of the sql. This allows us to send an exchange to the direct endpoint
with the uri direct:simple that is much easier for client to use than
the long URI that the sql component uses. Notice that the dataSource is lookup up in the
registry. So we can use standard Spring XML to configure our DataSource.
from("direct:simple")
.to("sql:select * from projects where license = # order by id?dataSourceRef=jdbc/myDataSource")
.to("mock:result");
And then we fire the message into the direct endpoint that will route it to our sql component that queries the database.
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
// send the query to direct that will route it to the sql where we will execute the query
// and bind the parameters with the data from the body. The body only contains one value
// in this case (GPL) but if we should use multi values then the body will be iterated
// so we could supply a List<String> instead containing each binding value.
template.sendBody("direct:simple", "GPL");
mock.assertIsSatisfied();
// the result is a List
List received = assertIsInstanceOf(List.class, mock.getReceivedExchanges().get(0).getIn().getBody());
// and each row in the list is a Map
Map row = assertIsInstanceOf(Map.class, received.get(0));
// and we should be able the get the project from the map that should be Linux
assertEquals("Linux", row.get("PROJECT"));
And we could configure the dataSource in Spring XML such as:
<jee:jndi-lookup id="myDS" jndi-name="jdbc/myDataSource"/>