JDBC Driver Connection URL

This section 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:

/**
 * MySqlConnectionUrl.java
 * Copyright (c) 2015, HerongYang.com, All Rights Reserved.
 */
import java.sql.*;
public class MySqlConnectionUrl {
  public static void main(String [] args) {
    Connection con = null;
    try {
      Class.forName("com.mysql.jdbc.Driver") ;
      System.out.println("MySQL JDBC driver loaded ok.");
 
      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 5.7.10 server with default settings. The warning message tells me that the connection was made with SSL, but the server's certificate was not verified.

C:\herong>java -cp .;\local\lib\mysql-connector-java-5.1.38-bin.jar 
   MySqlConnectionUrl

MySQL JDBC driver loaded ok.

WARN: Establishing SSL connection without server's identity 
verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ 
and 5.7.6+ requirements SSL connection must be established by default
if explicit option isn't set. For compliance with existing 
applications not using SSL the verifyServerCertificate property is set
to 'false'. You need either to explicitly disable SSL by setting 
useSSL=false, or set useSSL=true and provide truststore for server 
certificate verification. 
Connected with host:port/database.

WARN...
Connected with default host.

WARN...
Connected with default port.

WARN...
Connected with no database.

WARN...
Connected with properties only.

As a comparison, here is the output on MySQL 5.0 server:

C:\herong>java -cp .;\local\lib\mysql-connector-java-5.1.38-bin.jar
  MySqlConnectionUrl

MySQL JDBC driver loaded ok.
Connected with host:port/database.
Connected with default host.
Connected with default port.
Connected with no database.
Connected with properties only.

Last update: 2015.

Table of Contents

 About This Book

 Introduction of SQL

 MySQL Introduction and Installation

 Introduction of MySQL Programs

 Perl Programs and MySQL Servers

 PHP Programs and MySQL Servers

Java Programs and MySQL Servers

 MySQL Connector/J - Download and Installation

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

JDBC Driver Connection URL

 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

 Transaction Management and Isolation Levels

 Locks Used in MySQL

 Defining and Calling Stored Procedures

 Variables, Loops and Cursors Used in Stored Procedures

 Outdated Tutorials

 References

 PDF Printing Version