Based on the following code before the trycatchfinally block
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
{
}
Solution
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();
}
