samedi 25 avril 2015

"java.util.Scanner[delimiters=\p{javaWhitespace}+]" Error in JDBC program

I have written a jdbc application that has a function called "UpdateStudent," which takes the studentId as a parameter and then receives user input as to what the student's variables are to be changed to. The stored procedure works fine by itself within SQL server, but running things from java results in all string varaibles being changed to the value "java.util.Scanner[delimiters=\p{javaWhitespace}+]". However, my integer values are not affected, and are updated correctly. I believe the issue has to do with my implementation of the scanner in Java, since it works fine within SQL Server itself. Below is the function for UpdateStudent from my Main:

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

}

Here is the function updateStudent from my Student class as well:

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) {}
    }


}

I would appreciate any pointers about why I am getting this error. Everything seems to be compiling and running fine, so I think I might be just displaying my results incorrectly somehow.

Aucun commentaire:

Enregistrer un commentaire