Spring CallableStatementCreator
您好,我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要Apache Commons DBCP 和 Apache Commons 池 上你的类路径。
看起来你只有 DBCP。
You need both Apache Commons DBCP and Apache Commons Pool on your classpath.
It looks like you only have DBCP.
看起来您的类路径上需要 apache commons / dbcp
这是 Maven 依赖项(假设您使用 Maven):
Looks like you need apache commons / dbcp on your classpath
Here's the Maven dependency (assuming you use Maven):