SQL Insert
Use Case
In this example, we demonstrate how to create a route that inserts data into a database.
Steps:
- Create a datasource
- Use the SQL Component to execute insert operations
Create Datasource
To interact with a database, a datasource must first be defined. Below is a visual representation and example configuration for creating a datasource.
Example: database.camel.yaml
- beans:
- name: PostgresDatabase
type: "#class:org.apache.commons.dbcp2.BasicDataSource"
properties:
username: username
password: password
url: jdbc:postgresql://postgres:5432/databaseName
driverClassName: org.postgresql.Driver
Use the SQL Component
After configuring the datasource, you can use the SQL Component to insert data into your database.
Configure the datasource and define the SQL query. Below is an example of a query using data mapped from the exchange:
INSERT INTO table_name (id, field1, field2)
VALUES (:#${body[id]}, :#${variable.var1}, :#${variable.var2})
- The :#${...} syntax is used for parameter substitution with values from the Camel exchange.
- Ensure that the datasource name matches the name used in your route definition.
Additional Resources Apache Camel SQL Component Documentation