Which method is used to retrieve detailed information about an SQL exception?

  • getErrorCode()
  • getMessage()
  • getSQLException()
  • getSQLState()
The getErrorCode() method is used to retrieve the specific error code associated with an SQL exception, providing detailed information about the exception.

How can you handle a situation where a database connection is lost during a transaction?

  • Commit the transaction
  • Ignore the issue and proceed
  • Retry the transaction
  • Rollback the transaction
If a database connection is lost during a transaction, you can handle it by retrying the transaction, allowing for the re-establishment of the connection and completion of the operation.

What is the significance of the finally block in JDBC operations?

  • To handle exceptions
  • To improve performance
  • To optimize code execution
  • To release resources
The finally block in JDBC operations is significant for releasing resources, ensuring that resources like database connections are properly closed, regardless of whether an exception occurs.

How do you differentiate between checked and unchecked SQL exceptions?

  • Checked exceptions are SQLExceptions
  • Checked exceptions are caught at compile-time
  • Unchecked exceptions are SQLExceptions
  • Unchecked exceptions are caught at compile-time
Checked SQL exceptions are those derived from SQLException, and they must be either caught or declared to be thrown. Unchecked SQL exceptions are those that extend RuntimeException.

What is the best practice for handling SQL exceptions in a multi-tier application?

  • Catch and log the exception at the point of occurrence
  • Convert the SQL exception to a custom exception
  • Handle the exception and continue processing
  • Propagate the exception to the upper tiers
The best practice is to propagate the SQL exception to the upper tiers to allow centralized handling and logging, providing a clear separation of concerns in a multi-tier application.

In JDBC, how can you ensure that all resources are freed, even if an SQL exception occurs?

  • Close resources in the catch block
  • Implement a custom resource management mechanism
  • Resources are automatically freed in JDBC
  • Use the finally block to close resources
Using the finally block ensures that resources (like connections, statements, result sets) are closed, even if an SQL exception occurs, promoting proper resource management and avoiding leaks.

When refactoring a servlet to make it more configurable, what changes are made regarding initialization parameters?

  • Introduce new initialization parameters
  • Keep the existing parameters unchanged
  • Remove all initialization parameters
  • Replace parameters with hardcoding
In the process of making a servlet more configurable, one typically introduces new initialization parameters rather than removing or replacing existing ones. This allows for greater flexibility without disrupting existing functionality.

If an SQL exception occurs in a method that performs multiple database updates, how should the exception be handled to maintain data integrity?

  • Commit all changes made before the exception
  • Ignore the exception and continue with the remaining updates
  • Log the exception and terminate the transaction
  • Rollback all changes made before the exception
When an SQL exception occurs during multiple updates, rolling back all changes made before the exception helps maintain data integrity by ensuring either all updates succeed or none do.

When designing an application that connects to a database, how should SQL exceptions be managed to provide meaningful feedback to the user?

  • Display the raw SQL exception message
  • Log the exception and display a user-friendly error message
  • Show a generic error message to the user
  • Terminate the application on exception
To provide meaningful feedback, log the SQL exception for debugging purposes and display a user-friendly error message to the user, avoiding the display of raw SQL exception messages that may expose sensitive information.

The translation of JSP to servlet occurs in the ______ phase.

  • Compiling
  • Execution
  • Initialization
  • Parsing
The translation of JSP to servlet occurs in the compiling phase. JSP pages are translated to servlets before they are compiled into bytecode and executed on the server.