gpt4 book ai didi

java - 接收消息调用存储过程时找不到对应参数

转载 作者:行者123 更新时间:2023-12-01 10:01:04 26 4
gpt4 key购买 nike

我已经完成了以下 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;
}
}

最佳答案

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

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

但应该非常相似。

关于java - 接收消息调用存储过程时找不到对应参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36804032/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com