0

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?

console dialog

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);
    }
  }
}
ehr
  • 103
  • 1
  • 1
  • 11
  • There is no whiteline consuming statement after `double salary = scanner.nextDouble();` after the salary line; please insert a `scanner.nextLine();` to consume the newline, I believe it should work then. – Am_I_Helpful Sep 05 '17 at 17:55
  • 1
    Thanks a lot, it works. :) – ehr Sep 05 '17 at 18:00

0 Answers0