0

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

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;
   }
}
user3183411
  • 315
  • 2
  • 7
  • 19

3 Answers3

2

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

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

but it should be fairly similar.

0

Try declaring the output parameters using SqlOutParameter like in this example, maybe the warning will go away:

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)
);
Igor Deruga
  • 1,504
  • 1
  • 10
  • 18
0
this.jdbcCall = new SimpleJdbcCall(dataSource)
.declareParameters (new SqlOutParameter("out_id", Types.INTEGER), new SqlOutParameter("out_name", Types.NVARCHAR), new SqlOutParameter("out_age", Types.INTEGER));
cigien
  • 57,834
  • 11
  • 73
  • 112