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

Compare with Current View Page History

« Previous Version 6 Next »

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.

URI format

So far endpoints from this component could be used only as producers. It means that you cannot use them in from() statement.

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.

Options

Option

Type

Default

Description

dataSourceRef

String

null

Camel 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.

Rules of treating message body

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.

Results of invocation

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.

Header values

When doing update operations the SQL Component stores the update count in the body header

Header

Description

SqlProducer.UPDATE_COUNT

The number of rows updated for update operations, returned as an Integer object.

Configuration in Camel 1.5.0 or lower

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>

Configuration in Camel 1.5.1 or higher

You can now set a reference to a dataSoruce 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 rows is a Map<String, Object where the key is the column name.

TODO: sample

  • No labels