Java URL openConnection() Method

The openConnection() method of URL class opens the connection to specified URL and URLConnection instance that represents a connection to the remote object referred by the URL.

It should be noted that a URLConnection instance does not establish the actual network connection on the creation. This will happen only when calling URLConnection.connect().

Signature

The openConnection method is overloaded as given in the following table.

No.MethodDescription
1.URLConnection openConnection()Returns a URLConnection instance that represents a connection to the remote object referred to by the URL.
2.openConnection(Proxy proxy) throws IOExceptionSame as openConnection(), except that the connection will be made through the specified proxy; Protocol handlers that do not support proxying will ignore the proxy parameter and make a normal connection.

Returns

The URLConnection linking to the URL.

Throw

IOException - if an I/O exception occurs.

Example 1

Test it Now

Output:

The mime type is : text/html; charset=iso-8859-1
 The time out time of connection is : 0

Example 2

Test it Now

Output:

-----------------------------------
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>JDBC Tutorial | What is Java Database Connectivity(JDBC) - javatpoint</title><link rel="SHORTCUT ICON" href="/images/logo/icon.png" />
<link rel="stylesheet" type="text/css" href="/link.css"/>
<meta name="keywords" content="java, jdbc, tutorial, api, drivers, odbc"/><meta name="description" content="Java JDBC Tutorial or What is Java Database Connectivity(JDBC) with examples on Driver, DriverManager, Connection, Statement, ResultSet, PreparedStatement, CallableStatement, ResultSetMetaData, DatabaseMetaData, RowSet, Store Image, Fetch Image, Store file, Fetch file etc."/><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><link rel="canonical" href="https://www.javatpoint.com/java-jdbc" />
Next TopicJava URL Class




Latest Courses