MySQL Tutorials - Herong's Tutorial Examples - v4.46, by Herong Yang
Getting Driver and Server Information
Describes how to get JDBC driver and database information through the DatabaseMetaData object.
Once you have created a database connection object, you can obtain some version information about the JDBC driver and database server through the DatabaseMetaData object as shown in the following program:
/* MySqlDatabaseInfo.java * Copyright (c) 2005 HerongYang.com. All Rights Reserved. */ import java.sql.*; import javax.sql.*; public class MySqlDatabaseInfo { 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 driver and database info DatabaseMetaData meta = con.getMetaData(); System.out.println("Server name: " + meta.getDatabaseProductName()); System.out.println("Server 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()); // Closing the connection con.close(); } catch (Exception e) { System.err.println("Exception: "+e.getMessage()); } } }
The output confirms that JDBC driver mysql-connector-java-8.0.17.jar a JDBC 4.2 driver:
herong> java -cp .;\local\lib\mysql-connector-java-8.0.17.jar MySqlDatabaseInfo.java Server name: MySQL Server version: 8.0.16 Driver name: MySQL Connector/J Driver version: mysql-connector-java-8.0.17 (Revision: 16a712ddb3...) JDBC major version: 4 JDBC minor version: 2
Here is output when I ran the test with MySQL Connector/J 5.1.38 and MySQL Server 5.7. It shows that JDBC driver mysql-connector-java-5.1.38-bin.jar is a JDBC 4.0 driver:
herong> javac -cp .;\local\lib\mysql-connector-java-5.1.38-bin.jar MySqlDatabaseInfo.java herong> java -cp .;\local\lib\mysql-connector-java-5.1.38-bin.jar MySqlDatabaseInfo Server name: MySQL Server version: 5.7.10-log Driver name: MySQL Connector Java Driver version: mysql-connector-java-5.1.38 ( Revision: fe541c166cec739c74cc727c5da96c1028b4834a ) JDBC major version: 4 JDBC minor version: 0
Table of Contents
MySQL Introduction and Installation
Introduction of MySQL Programs
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
Connection URL Tests on Older MySQL Connector/J
Creating Connections with DataSource Class
►Getting Driver and Server Information
Creating Tables with AUTO_INCREMENT Columns
Character Strings and Bit Strings
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
Defining and Calling Stored Procedures
Variables, Loops and Cursors Used in Stored Procedures
System, User-Defined and Stored Procedure Variables
Storage Engines in MySQL Server
InnoDB Storage Engine - Primary and Secondary Indexes
Performance Tuning and Optimization
Installing MySQL Server on Linux