時,我已經通過下面的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[email protected]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());
}
\t */
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());
\t
}
}
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 {
\t
\t private DataSource dataSource;
\t private SimpleJdbcCall jdbcCall;
\t
\t public void setDataSource(DataSource dataSource){ \t
\t \t this.dataSource = dataSource;
\t \t this.jdbcCall = new SimpleJdbcCall(dataSource).withProcedureName("getRecord");
\t }
\t
\t public void create(String name, Integer age){
\t \t JdbcTemplate jdbcTemplateObject = new JdbcTemplate(dataSource);
\t \t String SQL = "INSERT INTO Student(name,age) VALUES (?,?)";
\t \t jdbcTemplateObject.update(SQL,name,age);
\t \t System.out.println("Created Record Name = " + name + " Age = " + age);
\t \t return;
\t }
\t
\t public Student getStudent(Integer id){
\t \t
\t \t SqlParameterSource in = new MapSqlParameterSource().addValue("inID", id);
\t \t Map<String, Object> out = jdbcCall.execute(in);
\t \t
\t \t Student student = new Student();
\t \t student.setId(id);
\t \t student.setName((String) out.get("outName"));
\t \t student.setAge((Integer) out.get("outAge"));
\t \t return student;
\t }
\t
\t public List<Student> listStudents(){
\t \t String SQL = "SELECT * FROM Student";
\t \t JdbcTemplate jdbcTemplateObject = new JdbcTemplate(dataSource);
\t \t List <Student> students = jdbcTemplateObject.query(SQL, \t new StudentMapper()); \t \t
\t \t return students;
\t }
}
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;
}
}
請包括您在問題中使用的代碼。 –
您還可以將存儲過程簽名添加到您的問題中嗎? – Igor