Sample Code:
OpportunityLineItem OLI = new OpportunityLineItem();
OLI.OpportunityId = oppty.Id; //Opportunity Id has to be assigned
OLI.PricebookEntryId = PBE.Id; //PriceBookEntry Id for that Product should be assigned.
OLI.Quantity = 1;
OLI.UnitPrice = PBE.UnitPrice;
insert OLI;
We cannot assign Product2Id to OpportunityLineItem.
Cheers!!!