接收消息,调用存储过程时无法找到对应的参数 [英] Receiving Message Unable to locate the Corresponding Parameter when calling Stored Procedure

查看:81
本文介绍了接收消息,调用存储过程时无法找到对应的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读下面的Spring教程

I have been going through the following Spring Tutorial

http://www.tutorialspoint.com/spring/calling_stored_procedure.htm

一切正常,我唯一的问题是我收到以下消息.

Everything is working the only problem I have is that I am receiving the following message.

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.如您所见,结果返回.我什至传入了一个不同的参数,并且值发生了变化,所以我知道它正在工作.我只是不确定为什么我会收到这些消息.我已经验证了参数名称是否与该类中的参数匹配.

The only difference is I am using a MSSQL. As you can see the results return. I've even passed in a different parameter and the value changes so I know it's working. I'm just not sure why I am receiving these messages. I've verified that the parameters name match with what is in the class.

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;
   }
}

推荐答案

它对我来说也添加了参数声明,但略有不同:

It worked for me adding the parameter declaration too, but slightly different:

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;并且创建的过程略有不同:

please note i was using MS SQL Server instead of MySQL; and have created the procedure slightly different:

CREATE PROCEDURE getRecord
  @id INT,
  @name VARCHAR(20) OUTPUT ,
  @age INT OUTPUT
AS
  SELECT @name = name, @age = AGE
  FROM Student where id = @id

但它应该非常相似.

这篇关于接收消息,调用存储过程时无法找到对应的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆