Reputation: 6111
I am going through Apache Camel and want to insert data to DB. My database is SqlServer. Apache Camel Version is 2.14.1
This is what I tried till now,
import javax.sql.DataSource;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.SimpleRegistry;
import org.apache.commons.dbcp.BasicDataSource;
public class file2DB {
public static void main(String[] args) throws Exception {
final String url = "jdbc:sqlserver://localhost:1433;databaseName=TestDB";
DataSource datasource = setupDataSource(url);
SimpleRegistry simpleregistry = new SimpleRegistry();
simpleregistry.put("DataSource", datasource);
CamelContext context = new DefaultCamelContext(simpleregistry);
context.addRoutes(new file2DB().new MyRouteBuilder());
context.start();
Thread.sleep(20000);
context.stop();
}
class MyRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
try {
from("direct:myTable")
.setBody(constant("INSERT INTO employee1(empId,payrollId) VALUES('1234','4567')"))
.to("jdbc:DataSource");
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static DataSource setupDataSource(String url){
BasicDataSource basicDataSource = new BasicDataSource();
basicDataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
basicDataSource.setUsername("sa");
basicDataSource.setPassword("*****");
basicDataSource.setUrl(url);
return basicDataSource;
}
}
I checked the Datasource connection using below code and I am getting the result, So there is no issue with connection it seems.
Connection con=null;
try {
con = basicDataSource.getConnection();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from employee1");
while (rs.next()) {
System.out.println(rs.getString(2));
}
} catch (SQLException e) {
e.printStackTrace();
}
I am not getting any error also data is not getting inserted in table. I also tried "SELECT" query that is also not working.
I tried Logger and it give me many errors in logs No class defn found twitter etc, which I am not at all using and I think my error is not relevant to that.
Any help.
Upvotes: 1
Views: 2973
Reputation: 55555
You need to trigger the Camel route. You use from("direct:myTable")
and that does not do anything out of the box. You need to either send a message to this endpoint, or change it to a timer or something that triggers a new message.
To send a message use the ProducerTemplate
which you can find information about here
Upvotes: 4