Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Based on the following code before the try/catch/finally block, fill out what sh

ID: 3928018 • Letter: B

Question

Based on the following code before the try/catch/finally block, fill out what should go into the try/catch/finally blocks in order to handle a transaction that executes the below DELETE Statement. Also, specify the code that should go into the finally block to close the EntityManager Object entityManager.  

Assume that there are accounts whose statuses are old.

EntityTransaction transaction = entityManager.getTransaction();
String qStr = "DELETE FROM Account a WHERE a.Status = :Status";
Query qObj = entityManager.createQuery(qStr);
qObj.setParameter(Status, 'Old');
int numRowsAffected = 0;

try
{
}

catch(Exception e)
{
}

finally
{
}

Explanation / Answer

try

{

EntityTransaction transaction = entityManager.getTransaction();

String qStr = "DELETE FROM Account a WHERE a.Status = :Status";

Query qObj = entityManager.createQuery(qStr);

qObj.setParameter(Status, 'Old');

int numRowsAffected = 0;

}

catch(Exception e)

{

System.out.println(e.printStackTrace());

}

finally

{

if(qObj!=null)

qObj.close();

if(transaction!=null)

transaction.close();

}try
{
EntityTransaction transaction = entityManager.getTransaction();
String qStr = "DELETE FROM Account a WHERE a.Status = :Status";
Query qObj = entityManager.createQuery(qStr);
qObj.setParameter(Status, 'Old');
int numRowsAffected = 0;
}

catch(Exception e)
{
System.out.println(e.printStackTrace());
}

finally
{
if(qObj!=null)
qObj.close();
if(transaction!=null)
transaction.close();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote