【问题标题】:Sql Connection in Spring Servicemix camelSpring Servicemix骆驼中的Sql连接
【发布时间】:2015-04-29 17:43:53
【问题描述】:

Spring Servicemix骆驼中的Sql连接

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

当我尝试使用 dataSource.getConnection() 建立连接时

不允许请帮忙

*****连接代码**********

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();
    }
}

【问题讨论】:

    标签: sql apache-camel apache-servicemix


    【解决方案1】:

    您必须使用以下代码设置数据库

    <!-- 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>
    

    请查看此链接http://camel.apache.org/sql-example.html

    【讨论】:

      【解决方案2】:

      您必须在骆驼/弹簧上下文中的 DatabaseBeanH2 中注入 dataSource bean,如下所示:

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

      【讨论】:

      • 你好 Michael,我检查了日志然后我发现这是 servicemix 中的错误: 原因:java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLSer verDriver 请给出解决方案
      • ServiceMix中安装sqlserver jdbc驱动了吗?您是否已在捆绑包中导入了 jdbc 驱动程序的包?
      猜你喜欢
      • 1970-01-01
      • 2015-04-27
      • 2018-08-25
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多