Creating Connections with DataSource Class

Describes how to create connection objects with the DataSource class.

Newer version of JDBC recommends now that connection objects should be created by the DataSource implementation classes:

// MySQL Connector/J 8.0.17
com.mysql.cj.jdbc.MysqlDataSource

// MySQL Connector/J 5.1.38
com.mysql.jdbc.jdbc2.optional.MysqlDataSource

Here is a sample program that creates a connection object using the DataSource class:

/* MySqlDataSource.java
 * Copyright (c) 2005 HerongYang.com. All Rights Reserved.
 */
import java.sql.*;
import javax.sql.*;
public class MySqlDataSource {
  public static void main(String [] args) {
    Connection con = null;
    try {

// Setting up the DataSource object
      // For MySQL Connector/J 5.1.38
      // com.mysql.jdbc.jdbc2.optional.MysqlDataSource ds
      //   = new com.mysql.jdbc.jdbc2.optional.MysqlDataSource();

      // For MySQL Connector/J 8.0.17
      com.mysql.cj.jdbc.MysqlDataSource ds
        = new com.mysql.cj.jdbc.MysqlDataSource();

      ds.setServerName("localhost");
      ds.setPortNumber(3306);
      ds.setDatabaseName("test");
      ds.setUser("root");
      ds.setPassword("TopSecret");

// Getting a connection object
      con = ds.getConnection();

// Getting database info
      DatabaseMetaData meta = con.getMetaData();
      System.out.println("Server name: "
        + meta.getDatabaseProductName());
      System.out.println("Server version: "
        + meta.getDatabaseProductVersion());

// Closing the connection
      con.close();
    } catch (Exception e) {
      System.err.println("Exception: "+e.getMessage());
    }
  }
}

The output below confirms that I got a good connection. Remember to include mysql-connector-java-8.0.17.jar in the classpath for compilation and execution:

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

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

Server name: MySQL
Server version: 8.0.16

Here is output when I ran the test with the com.mysql.jdbc.jdbc2.optional.MysqlDataSource implementation class and MySQL Connector/J 5.1.38:

herong> javac -cp .;\local\lib\mysql-connector-java-5.1.38-bin.jar
  MySqlDataSource.java

herong> java -cp .;\local\lib\mysql-connector-java-5.1.38-bin.jar
  MySqlDataSource

Server name: MySQL
Server version: 5.7.10-log

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