Saturday, 16 August 2014

JDBC : Connection URL

Enjoyed this tutorial?

Subscribe to Ram N Java for regular, beginner-friendly Java & database tutorials!

▶ Subscribe Now

What is a JDBC Connection URL?

A JDBC Connection URL is a formatted string used by Java applications to locate and connect to a specific database. Just like a web address directs your browser to a website, a JDBC URL informs Java's DriverManager which driver to pick, where the database server resides, what port it listens on, and which specific database instance to open.

General Syntax & Components

A typical JDBC connection URL consists of three primary parts:

jdbc:<subprotocol>:<subname>
  • jdbc (Main Protocol): The standard prefix identifying that the URL uses Java Database Connectivity.
  • Subprotocol: Specifies the database engine (for example, mysql, oracle, or sqlserver).
  • Subname: Details the host address, port number, database name, and optional parameters.

MySQL Connection URL Syntax & Example

When connecting to MySQL, the default port is 3306.

jdbc:mysql://<host>:<port>/<database_name>?property=value

Example:

jdbc:mysql://localhost:3306/employees_db?user=root&password=secret

Oracle Database Connection URL Syntax

For Oracle databases, you can connect using either the SID (System Identifier) or the Service Name:

// Syntax using SID:
jdbc:oracle:thin:@<host>:<port>:<SID>

// Syntax using Service Name:
jdbc:oracle:thin:@//<host>:<port>/<service_name>

In this format, thin indicates the use of Oracle's Type 4 Thin Driver, which is purely written in Java and requires no client-side installation.

How DriverManager Resolves Drivers

When your Java application passes a connection string via DriverManager.getConnection(url), the DriverManager scans its list of registered drivers. It asks each driver whether it recognizes the URL subprotocol (such as jdbc:mysql: or jdbc:oracle:). Once a compatible driver accepts the URL, it establishes the connection to the database engine.

Recommended Related Tutorials

Check out these related JDBC guides to level up your database programming:


Click here to watch in Youtube : https://www.youtube.com/watch?v=k1SW1GS9_zA

Click the below Image to Enlarge
JDBC : Connection URL
JDBC : Connection URL
JDBC : Connection URL
See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • No comments:

    Post a Comment

    Tutorials