JDBC Driver Connection URL

Describes the connection URL format and how to create connection objects with the DriverManager class.

If you want to use the DriverManager class to create connection objects, you need to know how to make a connection URL that provides access information to the MySQL server. The MySQL connection URL has the following format:

jdbc:mysql://[host][:port]/[database][?property1][=value1]...

  host - The host name where MySQL server is running.
         Default is 127.0.0.1 - the IP address of localhost.

  port - The port number where MySQL is listening for connection.
         Default is 3306.

  Database - The name of an existing database on MySQL server.
         If not specified, the connection starts no current database.

  Property - The name of a supported connection properties.
         "user" and "password" are 2 most important properties.

  Value - The value for the specified connection property.

Here are some example connection URLs:

jdbc:mysql://localhost:3306/test?user=root&password=TopSecret
jdbc:mysql://:3306/test?user=root&password=TopSecret
jdbc:mysql://localhost/test?user=root&password=TopSecret
jdbc:mysql://localhost:3306/?user=root&password=TopSecret
jdbc:mysql://localhost/?user=root&password=TopSecret
jdbc:mysql://:3306/?user=root&password=TopSecret
jdbc:mysql:///test?user=root&password=TopSecret
jdbc:mysql:///?user=root&password=TopSecret

I wrote the following program to validate some of the connection URLs listed above:

/* MySqlConnectionUrl2.java
 * Copyright (c) 2005 HerongYang.com. All Rights Reserved.
 */
import java.sql.*;
public class MySqlConnectionUrl2 {
  public static void main(String [] args) {
    Connection con = null;
    try {
      con = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/test?"
        + "user=root&password=TopSecret");
      System.out.println("Connected with host:port/database.");
      con.close();

      con = DriverManager.getConnection(
        "jdbc:mysql://:3306/test?"
        + "user=root&password=TopSecret");
      System.out.println("Connected with default host.");
      con.close();

      con = DriverManager.getConnection(
        "jdbc:mysql://localhost/test?"
        + "user=root&password=TopSecret");
      System.out.println("Connected with default port.");
      con.close();

      con = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/?"
        + "user=root&password=TopSecret");
      System.out.println("Connected with no database.");
      con.close();

      con = DriverManager.getConnection(
        "jdbc:mysql:///?"
        + "user=root&password=TopSecret");
      System.out.println("Connected with properties only.");
      con.close();

    } catch (Exception e) {
      System.err.println("Exception: "+e.getMessage());
    }
  }
}

Here is the output of running the above program on my MySQL Server 8 with default settings and JDK 12.

herong> javac MySqlConnectionUrl2.java

herong> java -cp .;\local\lib\mysql-connector-java-8.0.17.jar
   MySqlConnectionUrl2

Connected with host:port/database.
Connected with default host.
Connected with default port.
Connected with no database.
Connected with properties only.

Note that:

Table of Contents

 About This Book

 Introduction of SQL

 MySQL Introduction and Installation

 Introduction of MySQL Programs

 PHP Programs and MySQL Server

 Perl Programs and MySQL Servers

Java Programs and MySQL Servers

 MySQL Connector/J - Download and Installation

 Loading JDBC Driver Class - com.mysql.cj.jdbc.Driver

JDBC Driver Connection URL

 Connection URL Tests on Older MySQL Connector/J

 Creating Connections with DataSource Class

 Getting Driver and Server Information

 Creating Tables with AUTO_INCREMENT Columns

 "INSERT INTO" Statements

 Datatypes and Data Literals

 Operations and Expressions

 Character Strings and Bit Strings

 Commonly Used Functions

 Table Column Types for Different Types of Values

 Using DDL to Create Tables and Indexes

 Using DML to Insert, Update and Delete Records

 Using SELECT to Query Database

 Window Functions for Statistical Analysis

 Use Index for Better Performance

 Transaction Management and Isolation Levels

 Locks Used in MySQL

 Defining and Calling Stored Procedures

 Variables, Loops and Cursors Used in Stored Procedures

 System, User-Defined and Stored Procedure Variables

 MySQL Server Administration

 Storage Engines in MySQL Server

 InnoDB Storage Engine - Primary and Secondary Indexes

 Performance Tuning and Optimization

 Bulk Changes on Large Tables

 MySQL Server on macOS

 Installing MySQL Server on Linux

 Connection, Performance and Second Instance on Linux

 Archived Tutorials

 References

 Full Version in PDF/EPUB