JDBC 实现:参数错误

JDBC implementation: error with regard to parameters

我正在创建一个 JDBC 程序,该程序将显示所选学生在各自 类 中的成绩列表。但是,我不断收到错误 "Procedure or function 'getStudentHistory' expects parameter '@studentId', which was not supplied."

这是我在 SQL 服务器中的存储过程:

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

这是我的主函数 Class:

@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();

这是我的 Student 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 服务器中运行良好,但我对如何正确实现具有内部联接的存储过程感到困惑。我用于创建、更新、删除和显示数据的所有其他存储过程工作正常,但它们只使用一个学生 table。我将不胜感激社区可以给我的任何指示,如果有人认为这与找到我的问题的解决方案相关,我很乐意 post 更多我的代码。

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

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

另请参阅 CallableStatement 的文档。

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