Junior
What is a JDBC URL and what role does it play in working with databases in Java?
sobes.tech AI
Answer from AI
JDBC URL is a string used to specify the location of the database and connection parameters. It serves as a standardized way to specify the database for the JDBC driver.
Role:
- Driver Identification: Part of the URL (protocol
subprotocol) indicates which JDBC driver to use. - Database Location: The URL contains information about the host, port, and database name.
- Parameter Passing: It can include additional connection parameters (e.g.,
user,password, configuration options).
Format:
jdbc:<subprotocol>:<data source>[;<option>=<value>]
Examples:
// Oracle
jdbc:oracle:thin:@localhost:1521:orcl
// MySQL
jdbc:mysql://localhost:3306/mydatabase
// PostgreSQL
jdbc:postgresql://localhost:5432/mydatabase
// H2 (In-memory)
jdbc:h2:mem:testdb