Sql Connection in Spring Servicemix camel

雨燕双飞 提交于 2019-12-12 16:24:20

问题


Sql Connection in Spring Servicemix camel

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
    <property name="url" value="jdbc:sqlserver://localhost:1433/orderdb"/>
    <property name="username" value="abc"/>
    <property name="password" value="pqr"/>
</bean>

When I try to make connection using dataSource.getConnection()

Not allowing please help

*****Connection Code **********

public class DatabaseBeanH2 {

    private DataSource dataSource;
    private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseBeanH2.class);

    public DatabaseBeanH2(){}

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public void create() throws SQLException{
        Statement sta = dataSource.getConnection().createStatement();
        try {
            sta.executeUpdate("CREATE TABLE orders ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, item VARCHAR(50), amount INT, description VARCHAR(300), processed BOOLEAN, consumed BOOLEAN);");
        } catch (SQLException e) {
            LOGGER.info("Table orders already exists");
        }
    }

    public void destroy() throws SQLException {
        dataSource.getConnection().close();
    }
}

回答1:


You have to setting up your database using following code

<!-- this is the JDBC data source which uses an in-memory only Apache Derby database -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="url" value="jdbc:derby:memory:orders;create=true"/>
<property name="username" value=""/>
<property name="password" value=""/>
</bean>

<!-- bean which creates/destroys the database table for this example -->
<bean id="initDatabase" class="org.apache.camel.example.sql.DatabaseBean"
  init-method="create" destroy-method="destroy">
<property name="dataSource" ref="dataSource"/>
</bean>

<!-- configure the Camel SQL component to use the JDBC data source -->
<bean id="sql" class="org.apache.camel.component.sql.SqlComponent">
<property name="dataSource" ref="dataSource"/>
</bean>

Please check this link http://camel.apache.org/sql-example.html




回答2:


You have to inject the dataSource bean in your DatabaseBeanH2 in the camel/spring context, something like this:

<bean id="databaseBean" class="my.package.DatabaseBeanH2">
    <property name="dataSource" ref="dataSource" />
</bean>


来源:https://stackoverflow.com/questions/28757848/sql-connection-in-spring-servicemix-camel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!