### Question:
BatchProcessing { public void usePreparedStatement(){ try { String insertEmployeeSQL = "INSERT INTO EMPLOYEE(ID, NAME, DESIGNATION) VALUES (?,?,?);"; String insertEmployeeAddrSQL = "INSERT INTO EMP_ADDRESS(ID, EMP_ID, ADDRESS) VALUES (?,?,?);"; PreparedStatement employeeStmt = connection.prepareStatement(insertEmployeeSQL); PreparedStatement empAddressStmt = connection.prepareStatement(insertEmployeeAddrSQL); for(int i = 0; i < EMPLOYEES.length; i++){ String employeeId = UUID.randomUUID().toString(); employeeStmt.setString(1,employeeId); employeeStmt.setString(2,EMPLOYEES[i]); employeeStmt.setString(3,DESIGNATIONS[i]); employeeStmt.addBatch(); empAddressStmt.setString(1,UUID.randomUUID().toString()); empAddressStmt.setString(2,employeeId); empAddressStmt.setString(3,ADDRESSES[i]); empAddressStmt.addBatch(); } employeeStmt.executeBatch(); empAddressStmt.executeBatch(); connection.commit(); } catch (Exception e) { try { connection.rollback(); } catch (SQLException ex) { System.out.println("Error during rollback"); System.out.println(ex.getMessage()); } e.printStackTrace(System.out); } }  void getConnection(); void createTables(); void useStatement(); void usePreparedStatement(); static void main(String[] args);  }### Answer:
@Test public void when_usePreparedStatement_thenInsertData_success() throws Exception { String insertEmployeeSQL = "INSERT INTO EMPLOYEE(ID, NAME, DESIGNATION) VALUES (?,?,?);"; String insertEmployeeAddrSQL = "INSERT INTO EMP_ADDRESS(ID, EMP_ID, ADDRESS) VALUES (?,?,?);"; Mockito.when(connection.prepareStatement(insertEmployeeSQL)).thenReturn(employeeStatement); Mockito.when(connection.prepareStatement(insertEmployeeAddrSQL)).thenReturn(employeeAddressStatement); target.usePreparedStatement(); }
@Test public void when_usePreparedStatement_ifThrowException_thenCatchException() throws Exception { String insertEmployeeSQL = "INSERT INTO EMPLOYEE(ID, NAME, DESIGNATION) VALUES (?,?,?);"; String insertEmployeeAddrSQL = "INSERT INTO EMP_ADDRESS(ID, EMP_ID, ADDRESS) VALUES (?,?,?);"; Mockito.when(connection.prepareStatement(insertEmployeeSQL)).thenReturn(employeeStatement); Mockito.when(connection.prepareStatement(insertEmployeeAddrSQL)).thenThrow(new RuntimeException()); target.usePreparedStatement(); }