Which method is used to establish a connection to a database in JDBC?

Which method is used to establish a connection to a database in JDBC?

a) DriverManager.getConnection()
b) DriverManager.connect()
c) Driver.connect()
d) Connection.open()

Answer:

a) DriverManager.getConnection()

Explanation:

The DriverManager.getConnection() method is the standard way to establish a connection to a database in JDBC. This method takes a database URL, along with optional username and password parameters, and returns a Connection object that represents the connection to the database. The DriverManager class uses the URL to determine which registered driver should be used to create the connection.

The getConnection() method is versatile, supporting various forms of the method signature to accommodate different connection needs. For example, it can be used with just a URL or with additional properties that provide more detailed connection information. This flexibility makes it easy to connect to different databases with varying requirements.

Once a connection is established, the Connection object provides methods for creating statements, executing queries, and managing transactions. The proper management of database connections is crucial for ensuring efficient database operations and resource management in Java applications.

Leave a Comment

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

Scroll to Top