调用存储过程时接收消息找不到对应的参数
Posted
技术标签:
【中文标题】调用存储过程时接收消息找不到对应的参数【英文标题】:Receiving Message Unable to locate the Corresponding Parameter when calling Stored Procedure 【发布时间】:2016-04-22 22:06:37 【问题描述】:我一直在阅读以下 Spring 教程
http://www.tutorialspoint.com/spring/calling_stored_procedure.htm
一切正常,唯一的问题是我收到以下消息。
Apr 22, 2016 3:50:51 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5910e440: startup date [Fri Apr 22 15:50:51 MDT 2016]; root of context hierarchy
Apr 22, 2016 3:50:51 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
Apr 22, 2016 3:50:51 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: net.sourceforge.jtds.jdbc.Driver
----Listing Record with ID = 2 -----
Apr 22, 2016 3:50:52 PM org.springframework.jdbc.core.metadata.CallMetaDataContext matchInParameterValuesWithCallParameters
WARNING: Unable to locate the corresponding parameter value for 'outName' within the parameter values provided: [inID]
Apr 22, 2016 3:50:52 PM org.springframework.jdbc.core.metadata.CallMetaDataContext matchInParameterValuesWithCallParameters
WARNING: Unable to locate the corresponding parameter value for 'outAge' within the parameter values provided: [inID]
ID : 3, Name : Ayan, Age : 15
唯一的区别是我使用的是 MSSQL。如您所见,结果返回。我什至传入了一个不同的参数并且值发生了变化,所以我知道它正在工作。我只是不确定为什么我会收到这些消息。我已验证参数名称与类中的名称匹配。
package com.tutorialspoint;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tutorialspoint.StudentJDBCTemplate;
public class MainApp
public static void main(String[] args)
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
StudentJDBCTemplate studentJDBCTemplate =
(StudentJDBCTemplate)context.getBean("studentJDBCTemplate");
/*
System.out.println("------Records Creation--------" );
studentJDBCTemplate.create("Zara", 11);
studentJDBCTemplate.create("Nuha", 2);
studentJDBCTemplate.create("Ayan", 15);
System.out.println("------Listing Multiple Records--------" );
List<Student> students = studentJDBCTemplate.listStudents();
for (Student record : students)
System.out.print("ID : " + record.getId() );
System.out.print(", Name : " + record.getName() );
System.out.println(", Age : " + record.getAge());
*/
System.out.println("----Listing Record with ID = 2 -----" );
Student student = studentJDBCTemplate.getStudent(3);
System.out.print("ID : " + student.getId() );
System.out.print(", Name : " + student.getName() );
System.out.println(", Age : " + student.getAge());
package com.tutorialspoint;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
public class StudentMapper implements RowMapper<Student>
public Student mapRow(ResultSet rs, int rowNum) throws SQLException
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
return student;
package com.tutorialspoint;
import java.util.Map;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
public class StudentJDBCTemplate implements StudentDAO
private DataSource dataSource;
private SimpleJdbcCall jdbcCall;
public void setDataSource(DataSource dataSource)
this.dataSource = dataSource;
this.jdbcCall = new SimpleJdbcCall(dataSource).withProcedureName("getRecord");
public void create(String name, Integer age)
JdbcTemplate jdbcTemplateObject = new JdbcTemplate(dataSource);
String SQL = "INSERT INTO Student(name,age) VALUES (?,?)";
jdbcTemplateObject.update(SQL,name,age);
System.out.println("Created Record Name = " + name + " Age = " + age);
return;
public Student getStudent(Integer id)
SqlParameterSource in = new MapSqlParameterSource().addValue("inID", id);
Map<String, Object> out = jdbcCall.execute(in);
Student student = new Student();
student.setId(id);
student.setName((String) out.get("outName"));
student.setAge((Integer) out.get("outAge"));
return student;
public List<Student> listStudents()
String SQL = "SELECT * FROM Student";
JdbcTemplate jdbcTemplateObject = new JdbcTemplate(dataSource);
List <Student> students = jdbcTemplateObject.query(SQL, new StudentMapper());
return students;
package com.tutorialspoint;
public class Student
private Integer age;
private String name;
private Integer id;
public void setAge(Integer age)
this.age = age;
public Integer getAge()
return age;
public void setName(String name)
this.name = name;
public String getName()
return name;
public void setId(Integer id)
this.id = id;
public Integer getId()
return id;
【问题讨论】:
请在问题中包含您使用的代码。 您能否也将存储过程签名添加到您的问题中? 【参考方案1】:添加参数声明也对我有用,但略有不同:
public Student getStudent(Integer id)
SqlParameterSource in = new MapSqlParameterSource().addValue("id", id, Types.INTEGER)
.addValue("name",Types.VARCHAR)
.addValue("age", Types.INTEGER);
Map<String, Object> out = jdbcCall.execute(in);
Student student = new Student();
student.setId(id);
student.setName((String) out.get("name"));
student.setAge((Integer) out.get("age"));
return student;
请注意我使用的是 MS SQL Server 而不是 mysql;并且创建的程序略有不同:
CREATE PROCEDURE getRecord
@id INT,
@name VARCHAR(20) OUTPUT ,
@age INT OUTPUT
AS
SELECT @name = name, @age = AGE
FROM Student where id = @id
但应该非常相似。
【讨论】:
【参考方案2】:尝试像本例那样使用 SqlOutParameter 声明输出参数,也许警告会消失:
SimpleJdbcCall caller = new SimpleJdbcCall(getDataSource())
.withCatalogName("PKG_REG_REVIEW")
.withFunctionName("fn_get_task_complete_date")
.withoutProcedureColumnMetaDataAccess()
.declareParameters(
new SqlOutParameter("v_maxdate", Types.DATE),
new SqlParameter("p_case_seq", Types.INTEGER),
new SqlParameter("p_task_seq", Types.INTEGER)
);
【讨论】:
以上是关于调用存储过程时接收消息找不到对应的参数的主要内容,如果未能解决你的问题,请参考以下文章