Spring CallableStatementCreator

发布于 2024-11-04 05:19:22 字数 3761 浏览 4 评论 0原文

您好,我是 spring 新手。我尝试使用 CallableStatementCreator 访问存储过程,但出现以下错误。

原因: org.springframework.beans.factory.BeanCreationException: 使用名称创建 bean 时出错 类中定义的“SampleDataSource” 路径资源 [StoredProcCallApplication-config.xml]: bean实例化失败;嵌套的 例外是 java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool

StoredProcCall.java

package Utility;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import oracle.jdbc.driver.OracleTypes;
import org.springframework.jdbc.core.CallableStatementCreator;
public class StoredProcCall implements CallableStatementCreator {
    @Override
    public CallableStatement createCallableStatement(Connection arg0)
            throws SQLException {
        // TODO Auto-generated method stub
        CallableStatement cs=arg0.prepareCall("call test_proc(?,?)");
        cs.setString(1,"Vivek");
        cs.registerOutParameter(2, OracleTypes.CURSOR);
        return cs;}}

TestCallImpl.java

package com;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import Utility.StoredProcCall;
import oracle.jdbc.driver.OracleTypes;
import Utility.StoredProcCall;
public class TestCallImpl {
    StoredProcCall storedproccall=null;
    JdbcTemplate jdbcTemplate;

    public void setDataSource(DataSource dataSource)
    {this.jdbcTemplate=new JdbcTemplate(dataSource);
    }
    public StoredProcCall getstoredproc()
    {
    return storedproccall;
    }
    public void setstoredproc(StoredProcCall storedproccall)
        {
        this.storedproccall=storedproccall;
        }
    public void callProc()
    {
    List l=new ArrayList();
    l.add(new SqlParameter("ename",OracleTypes.VARCHAR));
    l.add(new SqlOutParameter("cur",OracleTypes.CURSOR,new RowMapper()
    {
    @Override
        public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
            // TODO Auto-generated method stub
            Map m=new HashMap();
            m.put("empno",arg0.getInt("empno"));
            m.put("ename",arg0.getString("ename"));

            return m;   }}));
Map results=this.jdbcTemplate.call(new StoredProcCall(),l);
results.put("ename","Vivek");
System.out.println(results.get("ename"));}}

StoredProcCallApplication-config.xml

<?xml version="1.0" encoding="UTF-8"?> <beans ---> 
 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" > 
 <property name="dataSource" ref="SampleDataSource">
</property> </bean> 

<bean id="SampleDataSource" class="org.apache.commons.dbcp.BasicDataSource" scope="singleton">
 <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
 <property name="url" value="jdbc:oracle:thin:@servername"/> 
  <property name="username" value="admin" />
   <property name="password" value="admin" /> 
    </bean>
         <bean id="storedproccall" class="Utility.StoredProcCall"> 
           </bean> 
          <bean id="TestClass" class="com.TestCallImpl"> 
      <property name="jdbcTemplate" ref="jdbcTemplate" />
       </bean> </beans>

Hi I m new to spring.I ve tried accessing a stored proc using CallableStatementCreator ,but i get foll error.

Caused by:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name
'SampleDataSource' defined in class
path resource
[StoredProcCallApplication-config.xml]:
Instantiation of bean failed; nested
exception is
java.lang.NoClassDefFoundError:
org/apache/commons/pool/impl/GenericObjectPool

StoredProcCall.java

package Utility;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import oracle.jdbc.driver.OracleTypes;
import org.springframework.jdbc.core.CallableStatementCreator;
public class StoredProcCall implements CallableStatementCreator {
    @Override
    public CallableStatement createCallableStatement(Connection arg0)
            throws SQLException {
        // TODO Auto-generated method stub
        CallableStatement cs=arg0.prepareCall("call test_proc(?,?)");
        cs.setString(1,"Vivek");
        cs.registerOutParameter(2, OracleTypes.CURSOR);
        return cs;}}

TestCallImpl.java

package com;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import Utility.StoredProcCall;
import oracle.jdbc.driver.OracleTypes;
import Utility.StoredProcCall;
public class TestCallImpl {
    StoredProcCall storedproccall=null;
    JdbcTemplate jdbcTemplate;

    public void setDataSource(DataSource dataSource)
    {this.jdbcTemplate=new JdbcTemplate(dataSource);
    }
    public StoredProcCall getstoredproc()
    {
    return storedproccall;
    }
    public void setstoredproc(StoredProcCall storedproccall)
        {
        this.storedproccall=storedproccall;
        }
    public void callProc()
    {
    List l=new ArrayList();
    l.add(new SqlParameter("ename",OracleTypes.VARCHAR));
    l.add(new SqlOutParameter("cur",OracleTypes.CURSOR,new RowMapper()
    {
    @Override
        public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
            // TODO Auto-generated method stub
            Map m=new HashMap();
            m.put("empno",arg0.getInt("empno"));
            m.put("ename",arg0.getString("ename"));

            return m;   }}));
Map results=this.jdbcTemplate.call(new StoredProcCall(),l);
results.put("ename","Vivek");
System.out.println(results.get("ename"));}}

StoredProcCallApplication-config.xml

<?xml version="1.0" encoding="UTF-8"?> <beans ---> 
 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" > 
 <property name="dataSource" ref="SampleDataSource">
</property> </bean> 

<bean id="SampleDataSource" class="org.apache.commons.dbcp.BasicDataSource" scope="singleton">
 <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
 <property name="url" value="jdbc:oracle:thin:@servername"/> 
  <property name="username" value="admin" />
   <property name="password" value="admin" /> 
    </bean>
         <bean id="storedproccall" class="Utility.StoredProcCall"> 
           </bean> 
          <bean id="TestClass" class="com.TestCallImpl"> 
      <property name="jdbcTemplate" ref="jdbcTemplate" />
       </bean> </beans>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流绪微梦 2024-11-11 05:19:22

java.lang.NoClassDefFoundErrororg/apache/commons/pool/impl/GenericObjectPool

您需要Apache Commons DBCP Apache Commons 池 上你的类路径。

看起来你只有 DBCP。

java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool

You need both Apache Commons DBCP and Apache Commons Pool on your classpath.

It looks like you only have DBCP.

稚气少女 2024-11-11 05:19:22

看起来您的类路径上需要 apache commons / dbcp

<bean id="SampleDataSource" class="org.apache.commons.dbcp.BasicDataSource" ...
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

这是 Maven 依赖项(假设您使用 Maven):

<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>

Looks like you need apache commons / dbcp on your classpath

<bean id="SampleDataSource" class="org.apache.commons.dbcp.BasicDataSource" ...
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Here's the Maven dependency (assuming you use Maven):

<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文