Which method is used to close a JDBC connection?

Java MCQ: Which method is used to close a JDBC connection?

a) terminate()
b) close()
c) shutdown()
d) disconnect()

Answer:

b) close()

Explanation:

The close() method is used to close a JDBC connection. This method is called on the Connection object to release the database resources associated with the connection. It is important to close connections when they are no longer needed to prevent resource leaks and ensure that the database can handle new connections efficiently.

Here’s an example of closing a connection:

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user", "password");
try {
    // Perform database operations
} finally {
    conn.close();  // Close the connection
}

In this example, the close() method is called in a finally block to ensure that the connection is closed even if an exception occurs during database operations.

Properly closing JDBC connections is a best practice to maintain database performance and stability.

Reference links:

https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top