Javatpoint Logo
Javatpoint Logo

Java DriverManager getConnection() Method

The getConnection(String url) method of Java DriverManager class attempts to establish a connection to the database by using the given database URL. The appropriate driver from the set of registered JDBC drivers is selected.

Syntax

Parameters

url - a database url of the form jdbc:subprotocol:subname

Returns

This method returns connections to the URL.

Exception

SQLException will be thrown, if database access occurs or url is null.

SQLTimeoutException will be thrown, when the timeout value specified by the setLoginTimeout method has been exceeded and tried to cancel the current database connection attempt.

Example

Test it Now-->

Output:

java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)

getConnection(String url, Properties info)

The getConnection(String url, Properties info) method of Java DriverManager class attempts to establish a connection to the database by using the given database url. The appropriate driver from the set of registered JDBC drivers is selected. Properties are implementation-defined as to which value will take precedence. For maximum portability, an application should only specify a property once.

Syntax

Parameters

url - a database url of the form jdbc:subprotocol:subname

info - a list of arbitrary string tag/value pairs as connection arguments.

Returns

This method returns a Connection to the URL.

Exception

SQLException will be thrown, if database access occurs or url is null.

SQLTimeoutException will be thrown, when the timeout value specified by the setLoginTimeout method has been exceeded and tried to cancel the current database connection attempt.

Example

Output:

Connection created
Connection closed





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA