MySQL Tutorials - Herong's Tutorial Examples - Version 4.20, by Dr. Herong Yang
Getting Driver and Server Information
This section 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) 2015, 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 com.mysql.jdbc.jdbc2.optional.MysqlDataSource ds = new com.mysql.jdbc.jdbc2.optional.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-5.1.38 a JDBC 4.0 driver:
C:\herong>javac -cp .;\local\lib\mysql-connector-java-5.1.38-bin.jar MySqlDatabaseInfo.java C:\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
Last update: 2015.
Table of Contents
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
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
Transaction Management and Isolation Levels
Defining and Calling Stored Procedures