"java.util.Scanner[delimiters=\p{javaWhitespace}+]" JDBC 程序出错
"java.util.Scanner[delimiters=\p{javaWhitespace}+]" Error in JDBC program
我编写了一个 jdbc 应用程序,它有一个名为 "UpdateStudent," 的函数,该函数将 studentId 作为参数,然后接收用户输入的关于学生变量要更改的内容。存储过程本身在 SQL 服务器内运行良好,但是 java 中的 运行 导致所有字符串变量都更改为值 "java.util.Scanner[delimiters=\p{javaWhitespace}+]"。
但是,我的整数值不受影响,并且已正确更新。
我认为这个问题与我在 Java 中实施扫描器有关,因为它在 SQL 服务器本身内运行良好。
下面是我的 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();
}
这也是我的学生 class 的函数 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()
将 returns 此扫描仪的字符串表示形式,而不是流中的字符串。 Scanner 的字符串表示包含可能对调试有用的信息。
如果要使用Scanner
获取输入,只需使用nextLine()
,例如:
String passedFirstName = insertedFirstName.nextLine();
此外,你只需要一个Scanner
对象。
扫描器class 将根据用于读取的API 从流中读取内容。如果使用 nextInt() 那么它将作为整数参数,对于字符串 nextLine() 就足够了。隐式转换可以由使用相应 API.
提供的扫描仪 class 进行
我编写了一个 jdbc 应用程序,它有一个名为 "UpdateStudent," 的函数,该函数将 studentId 作为参数,然后接收用户输入的关于学生变量要更改的内容。存储过程本身在 SQL 服务器内运行良好,但是 java 中的 运行 导致所有字符串变量都更改为值 "java.util.Scanner[delimiters=\p{javaWhitespace}+]"。 但是,我的整数值不受影响,并且已正确更新。 我认为这个问题与我在 Java 中实施扫描器有关,因为它在 SQL 服务器本身内运行良好。 下面是我的 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();
}
这也是我的学生 class 的函数 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()
将 returns 此扫描仪的字符串表示形式,而不是流中的字符串。 Scanner 的字符串表示包含可能对调试有用的信息。
如果要使用Scanner
获取输入,只需使用nextLine()
,例如:
String passedFirstName = insertedFirstName.nextLine();
此外,你只需要一个Scanner
对象。
扫描器class 将根据用于读取的API 从流中读取内容。如果使用 nextInt() 那么它将作为整数参数,对于字符串 nextLine() 就足够了。隐式转换可以由使用相应 API.
提供的扫描仪 class 进行