在Spring ServiceMix的骆驼SQL连接 [英] Sql Connection in Spring Servicemix camel
本文介绍了在Spring ServiceMix的骆驼SQL连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在春季ServiceMix的骆驼SQL连接
<豆ID =数据源级=org.apache.commons.dbcp.BasicDataSource消灭法=亲密>
<属性名=driverClassNameVALUE =com.microsoft.sqlserver.jdbc.SQLServerDriver/>
<属性名=URL值=的jdbc:SQLSERVER://本地主机:1433 / orderdb/>
<属性名=用户名值=ABC/>
<属性名=密码值=焊接工艺评定/>
< /豆>
当我尝试使用的DataSource.getConnection进行连接()
不容许请帮助
*****连接code **********
公共类DatabaseBeanH2 { 私人数据源数据源;
私有静态最后记录器记录仪= LoggerFactory.getLogger(DatabaseBeanH2.class); 公共DatabaseBeanH2(){} 公共无效的setDataSource(DataSource的数据源){
this.dataSource =数据源;
} 公共无效创建()抛出的SQLException {
声明STA =的DataSource.getConnection()的createStatement()。
尝试{
sta.executeUpdate(CREATE TABLE命令(ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,项目VARCHAR(50),金额INT,说明VARCHAR(300),处理BOOLEAN,消耗布尔值););
}赶上(的SQLException E){
LOGGER.info(表的订单已经存在);
}
} 公共无效的destroy()抛出的SQLException {
的DataSource.getConnection()close()方法。
}
}
解决方案
您在骆驼/ Spring上下文注入数据源bean在DatabaseBeanH2,是这样的:
<豆的id =databaseBean级=my.package.DatabaseBeanH2>
<属性名=数据源REF =数据源/>
< /豆>
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();
}
}
解决方案
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>
这篇关于在Spring ServiceMix的骆驼SQL连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文