&引用;Scanner[delimiters=\p{javaWhitespace}+;];JDBC程序中的错误

&引用;Scanner[delimiters=\p{javaWhitespace}+;];JDBC程序中的错误,java,sql-server,jdbc,Java,Sql Server,Jdbc,我已经编写了一个jdbc应用程序,它有一个名为“UpdateStudent”的函数,该函数将studentId作为参数,然后接收用户输入,以确定要将student的变量更改为什么。存储过程本身在SQL server中运行良好,但从java运行会导致所有字符串变量都更改为值“java.util.Scanner[delimiters=\p{javaWhitespace}+]”。 但是,我的整数值不受影响,并且已正确更新。 我相信这个问题与我用Java实现的扫描器有关,因为它在SQL Server本身

我已经编写了一个jdbc应用程序,它有一个名为“UpdateStudent”的函数,该函数将studentId作为参数,然后接收用户输入,以确定要将student的变量更改为什么。存储过程本身在SQL server中运行良好,但从java运行会导致所有字符串变量都更改为值“java.util.Scanner[delimiters=\p{javaWhitespace}+]”。 但是,我的整数值不受影响,并且已正确更新。 我相信这个问题与我用Java实现的扫描器有关,因为它在SQL Server本身中运行良好。 以下是my Main中UpdateStudent的函数:

public static void updateStudent()
{
    System.out.print("\n Please enter the Id of a current student that you wish to update");
    System.out.print("\n==>");

    Student student = new Student();

    @SuppressWarnings("resource")

    Scanner insertstudentID = new Scanner(System.in);
    int passedStudentID = insertstudentID.nextInt(); //program starts counting at zero, but starts displaying at one

    System.out.print("\n Enter the new value for FirstName \n ==> ");
    @SuppressWarnings("resource")
    Scanner insertedFirstName = new Scanner(System.in);
    insertedFirstName.nextLine();        
    String passedFirstName = insertedFirstName.toString();
    student.setmFirstName(passedFirstName);

    System.out.print("\n Enter the new value for LastName \n ==> ");
    @SuppressWarnings("resource")
    Scanner insertedLastName = new Scanner(System.in);
    insertedLastName.nextLine();
    String passedLastName = insertedLastName.toString();
    student.setmLastName(passedLastName);

    System.out.print("\n Enter the new value for Num \n ==> ");
    @SuppressWarnings("resource")
    Scanner insertedNum = new Scanner(System.in);
    int passedNum = insertedNum.nextInt();
    student.setmNum(passedNum);

    student.updateStudent(passedStudentID, passedFirstName, passedLastName, passedNum);

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

}
以下是我的学生课堂上的函数updateStudent:

public void updateStudent(int studentId, String lastName, String firstName, int num) 
{

    Connection con = dbConnect();
    CallableStatement cs = null;
    ResultSet rs = null;
    //int studentId1=0;
    int returnVal=0;
    try {
        cs = con.prepareCall("{? = call updateStudent (?,?,?,?)}");
        cs.registerOutParameter(1, returnVal);
        cs.setInt(2, studentId);    //changed setint to registerOutParameter   
                                    //When changed setint to registerOutParameter   
                                    //error: The formal parameter "@studentId" was not declared 
                                    //as an OUTPUT parameter, but the actual parameter passed in requested output.
        cs.setString(3, firstName);            
        cs.setString(4, lastName);          
        cs.setInt(5,num);

        rs = cs.executeQuery();
       // cs.executeQuery();

    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if (rs != null) try { rs.close(); } catch(Exception e) {}
        if (cs != null) try { cs.close(); } catch(Exception e) {}
        if (con != null) try { con.close(); } catch(Exception e) {}
    }


}
如果有人能告诉我为什么会出现这个错误,我将不胜感激。一切似乎都在编译和运行,所以我想我可能只是以某种方式错误地显示了结果。

Scanner.toString()
将返回此扫描仪的字符串表示形式,而不是流中的字符串。扫描仪的字符串表示形式包含可能对调试有用的信息

如果要使用
Scanner
获取输入,只需使用
nextLine()
,例如:

String passedFirstName = insertedFirstName.nextLine();

此外,您只需要一个
Scanner
对象。

Scanner类将根据用于读取的API从流中读取内容。如果使用nextInt(),则它将作为整型参数,对于字符串nextLine()就足够了。隐式转换可以由使用各自API提供的scanner类执行。

谢谢,您的回答完全正确。在继续JDBC开发时,我将确保牢记“toString”和“nextLine”之间的区别。