MySqlJdbcConnection.java - Connector/J JDBC Connection

Q

How to create a JDBC connection to MySQL database using the Connector/J JDBC driver? I want to see a Java program example.

✍: FYIcenter.com

A

If you have a MySQL server running on your local computer and listening on the default port 3306, you can use the following example, to learn how to create a JDBC connection:

// Copyright (c) 2016 FYIcenter.com
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.DatabaseMetaData;

// Example of Connector/J JDBC connection to MySQL server
public class MySqlJdbcConnection {
   public static void main(String [] args) throws Exception {

      // Use the basic Connector/J JDBC connection URL
      String url = "jdbc:mysql://localhost?user=root&password=fyicenter";
      Connection con = DriverManager.getConnection(url);
      
      System.out.println("JDBC connection and database server information:"); 
      DatabaseMetaData meta = con.getMetaData();
      System.out.println("Database Product Name: "
        + meta.getDatabaseProductName());
      System.out.println("Database Product Version: "
        + meta.getDatabaseProductVersion());
      System.out.println("Driver Name: "
        + meta.getDriverName());
      System.out.println("Driver Version: "
        + meta.getDriverVersion());
      System.out.println("JDBC Major Version: "
        + meta.getJDBCMajorVersion());
      System.out.println("JDBC Minor Version: "
        + meta.getJDBCMinorVersion());

      // Close the connection
      con.close();
   }
}

You can compile and run the above example in a command window as shown below:

C:\fyicenter>\local\jdk-1.8.0\bin\javac 
   MySqlJdbcConnection.java

C:\fyicenter>\local\jdk-1.8.0\bin\java
   -cp .;C:\local\mysql-connector-java-5.1.40\mysql-connector-java-5.1.40-bin.jar 
   MySqlJdbcConnection

Sat Nov 26 19:22:17 EST 2016 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. 

JDBC connection and database server information:
Database Product Name: MySQL
Database Product Version: 5.7.10-log
Driver Name: MySQL Connector Java
Driver Version: mysql-connector-java-5.1.40 ( Revision: ... )
JDBC Major Version: 4
JDBC Minor Version: 0

The output shows that:

  • The database server is MySQL 5.7.10-log.
  • The JDBC driver is MySQL Connector Java (Connector/J) 5.1.40
  • The JDBC API supported is JDBC 4.0.

You can fix the warning message by adding "useSSL=false" into the connection URL string.

 

Examples for Connector/J - JDBC Driver for MySQL

⇒⇒FAQ for Connector/J - JDBC Driver for MySQL

2016-12-02, 1241👍, 0💬