Java – Apache camel example inserts a row into a table
I want to exchange Insert body into the database table to get one of my routing conditions
>Are there any sample / tutorial camel JDBC components inserted into the message body? > I can import the SQL statement itself and exchange Body pass it?
I saw it http://camel.apache.org/jdbc.html But I can't understand it
The spring example here is confusing to me I don't understand why it sets the body as an SQL query and imports some queries from the classpath again (the insert query example is not mentioned here.)
Solution
You may need to reorganize the payload before insertion, so there may be no problem using any method in camel to convert to set the principal to the appropriate insert statement
What matters is what payload structure your incoming message has In the basic case – it's a string – it should be fairly simple
// In a Java bean/processor before the JDBC endpoint. // Update: make sure to sanitize the payload from sql injections if it contains user inputs or external data not generated by trusted sources. exchange.getIn().setBody("INSERT INTO MYTABLE VALUES('" + exchange.getIn().getBody(String.class) + "','fixedValue',1.0,42)");
If your message contains complex data structures, this code is certainly more complex, but it generates SQL queries in almost the same way as regular applications
Sample classpath you want to reference
<jdbc:embedded-database id="testdb" type="DERBY"> <jdbc:script location="classpath:sql/init.sql"/> </jdbc:embedded-database>
Briefly explain how to test the JDBC component by starting the embedded database server (APACHE Derby) and populate it with some initial data (SQL / init.sql file) This part is not actually part of the core JDBC component, but just in the document. You can start and run the example without configuring the database server and setting the JDBC connection properties
That is, you may want to use SQL components for more complex scenarios