gpt4 book ai didi

java - JDBC 实现 : error with regard to parameters

转载 作者:行者123 更新时间:2023-11-30 08:53:09 26 4
gpt4 key购买 nike

我正在创建一个 JDBC 程序,该程序将显示所选学生在各自类(class)中的成绩列表。但是,我不断收到错误消息“过程或函数‘getStudentHistory’需要参数‘@studentId’,但未提供。”

这是我在 SQL Server 中的存储过程:

CREATE PROC [dbo].[getStudentHistory]

@studentId INT
AS

SELECT LastName, FirstName, Year, Term, C.Prefix + ' ' + CAST(C.Num AS CHAR(3)) + ' - ' + SEC.Letter AS [Course Number],
Units, isNull(G.Letter, ' ') AS Grade
FROM Student S

INNER JOIN StudentInSection SIS
ON S.StudentId = SIS.StudentId
INNER JOIN Section SEC
ON SEC.SectionId = SIS.SectionId
INNER JOIN Course C
ON C.CourseId = SEC.CourseId
LEFT OUTER JOIN Grade G
ON G.GradeId = SIS.GradeId
INNER JOIN Semester SEM
ON SEM.SemesterId = SEC.SemesterId

WHERE S.StudentId = @studentId
Order BY Units DESC

这是我在主类中的函数:

@SuppressWarnings("resource")
public static void showStudentHistory()
{

System.out.print("\n Please enter the Id of a current student you wanna see grades for");
System.out.print("\n==>");

Scanner insertstudentID = new Scanner(System.in);
int passedStudentID = insertstudentID.nextInt() ;

Student student = new Student(passedStudentID, null, null);
List<Student> students = student.getStudentHistory(passedStudentID);
String tabs = "\t\t";
System.out.println("LastName"+ tabs + "FirstName"+ tabs + "Year"+ tabs + "Term"+ tabs + "Course Number"+ tabs + "Units"+ tabs + "Grade");
System.out.println("---------"+ tabs + "---------"+ tabs + "--------"+ tabs + "-----------");


// Student tempStu = students.get(passedStudentID);
// System.out.println(tempStu.getmStudentId() + "\t\t\t" +
// padRight(tempStu.getmFirstName(), 15) + "\t\t" +
// padRight(tempStu.getmLastName(), 15) + "\t\t" +
// tempStu.getmNum());

Scanner kb = new Scanner(System.in);
System.out.print("\nHit Enter to continue...");
String discard = kb.nextLine();

这是我的学生类(class)的附带功能:

public List<Student> getStudentHistory(int StudentID) {

List<Student> students = new ArrayList<>();
Connection con = dbConnect();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = con.prepareCall("getStudentHistory");
rs = ps.executeQuery();
// Iterate through the data in the result set and load the List
// while (rs.next()) {
// students.add(new Student(rs.getInt("StudentId")
// , rs.getString("LastName")
// , rs.getString("FirstName")
// , rs.getInt("Year")
// , rs.getString("Course Number")
// , rs.getInt("Units")
// , rs.getString("Grade")
// )
// );
// }
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) try { rs.close(); } catch(Exception e) {}
if (ps != null) try { ps.close(); } catch(Exception e) {}
if (con != null) try { con.close(); } catch(Exception e) {}
}
return students;

}

我知道我的存储过程本身在 SQL Server 中运行良好,但我对如何正确实现具有内部联接的存储过程感到困惑。我用于创建、更新、删除和显示数据的所有其他存储过程工作正常,但它们只使用一个学生表。我将不胜感激社区可以给我的任何指示,如果有人认为这与找到我的问题的解决方案相关,我很乐意发布更多我的代码。

最佳答案

问题与您使用内部联接无关。存储过程需要一个参数,您还没有提供它。你应该这样调用它:

ps = con.prepareCall("{call getStudentHistory(?)}");
ps.setInt(1, studentId);
ResultSet rs = ps.executeQuery();

另请参阅 CallableStatement 的文档.

作为旁注,我建议您使用 java 命名约定:变量和参数以小写字母开头,而不是大写字母(例如使用 studentID,而不是 StudentID ).

关于java - JDBC 实现 : error with regard to parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29875985/

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