我发现这样一个被误解了:
CallableStatement测试函数有效( TestCallableIn )。
当我尝试将CallableStatement转换为批处理( TestCallableBatchProcessing )时,我无法执行while循环,导致
option = scanner.nextLine();
没有收到输入 - 此外 - 该类没有得到任何输入数据只是走出循环。
你知道可能出现什么问题吗?
TestCallableBatchProcessing
package com.pluralsight.testingJDBC;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.Scanner;
public class TestCallableBatchProcessing {
public static void main(String[] args) {
try (
Connection conn = DBUtil.getConnection(DBType.ORADB);
CallableStatement callableStatement = conn.prepareCall("{call AddNewEmployee(?,?,?,?,?)}");
)
{
String option;
do {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Employee # : ");
int empno = Integer.parseInt(scanner.nextLine());
System.out.print("Enter Employee Name : ");
String ename = scanner.nextLine();
System.out.print("Enter Email :");
String email = scanner.nextLine();
System.out.print("Enter Hiredate : ");
Date doj = java.sql.Date.valueOf(scanner.nextLine());
System.out.print("Enter Salary : ");
double salary = scanner.nextDouble();
callableStatement.setInt(1, empno);
callableStatement.setString(2, ename);
callableStatement.setString(3, email);
callableStatement.setDate(4, (java.sql.Date) doj);
callableStatement.setDouble(5, salary);
callableStatement.addBatch();
System.out.println("Do you want to add another record (yes/no) :");
option = scanner.nextLine();
} while ( option.equalsIgnoreCase("yes"));
int []updateCounts = callableStatement.executeBatch();
System.out.println("You have inserted : " + updateCounts.length + " records.");
} catch (SQLException ex) {
DBUtil.showErrorMessage(ex);
}
}
}
TestCallableIn:
package com.pluralsight.testingJDBC;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.Scanner;
public class TestCallableIn {
public static void main(String[] args) {
try(
Connection conn = DBUtil.getConnection(DBType.ORADB);
CallableStatement callableStatement = conn.prepareCall("{call AddNewEmployee(?,?,?,?,?)}");
)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Employee # : ");
int empno = Integer.parseInt(scanner.nextLine());
System.out.print("Enter Employee Name : ");
String ename = scanner.nextLine();
System.out.print("Enter Email ID :");
String email = scanner.nextLine();
System.out.print("Enter Hiredate : ");
Date doj = java.sql.Date.valueOf(scanner.nextLine());
System.out.print("Enter Salary : ");
double salary = scanner.nextDouble();
callableStatement.setInt(1, empno);
callableStatement.setString(2, ename);
callableStatement.setString(3, email);
callableStatement.setDate(4, (java.sql.Date) doj);
callableStatement.setDouble(5, salary);
callableStatement.execute();
System.out.println("Employee Record Added Successfully.");
} catch (SQLException ex) {
DBUtil.showErrorMessage(ex);
}
}
}