使用 java 中蓝图中定义的数据源访问 oracle 数据库
Using datasource defined in blueprint in java to access oracle db
下面是我的数据源
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
<property name="URL" value="jdbc:oracle:thin:@${host}:${port}:${dbname}"/>
<property name="user" value="${user}"/>
<property name="password" value="${pass}"/>
</bean>
<service ref="dataSource" interface="javax.sql.DataSource">
<service-properties>
<entry key="osgi.jndi.service.name" value="jdbc/oracleds"/>
<entry key="datasource.name" value="OracleDS"/>
</service-properties>
</service>
<bean id="LogToOracleDB" class="com.beans.LogToOracleDB">
<property name="dataSource" ref="dataSource" />
</bean>
我想在一些名为 LogToOracleDB
的 bean 文件中使用它
public class LogToOracleDB {
protected static DataSource dataSource;
public void test(Exchange exchange) throws SQLException {
Connection con = dataSource.getConnection();
Statement stmt = con.createStatement();
...............
}
我在从 datasource
获取连接时遇到空指针异常,因为它是空的。有人可以帮我查找数据源吗??
dataSource = (DataSource)context.lookup(JNDIname);
你必须这样使用,否则创建一个 comboPool 并尝试从那里看
下面是我的数据源
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
<property name="URL" value="jdbc:oracle:thin:@${host}:${port}:${dbname}"/>
<property name="user" value="${user}"/>
<property name="password" value="${pass}"/>
</bean>
<service ref="dataSource" interface="javax.sql.DataSource">
<service-properties>
<entry key="osgi.jndi.service.name" value="jdbc/oracleds"/>
<entry key="datasource.name" value="OracleDS"/>
</service-properties>
</service>
<bean id="LogToOracleDB" class="com.beans.LogToOracleDB">
<property name="dataSource" ref="dataSource" />
</bean>
我想在一些名为 LogToOracleDB
public class LogToOracleDB {
protected static DataSource dataSource;
public void test(Exchange exchange) throws SQLException {
Connection con = dataSource.getConnection();
Statement stmt = con.createStatement();
...............
}
我在从 datasource
获取连接时遇到空指针异常,因为它是空的。有人可以帮我查找数据源吗??
dataSource = (DataSource)context.lookup(JNDIname);
你必须这样使用,否则创建一个 comboPool 并尝试从那里看