I've found such a misunderstood:
CallableStatement test function works (TestCallableIn).
When I try to transform CallableStatement into Batch processing (TestCallableBatchProcessing), I cannot execute a while loop, cause
option = scanner.nextLine();
doesn't receive the input - moreover - the class doesn't get any input data just getting out of loop.
Do you know what may goes wrong?
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);
}
}
}