package org.nhooo.example.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.DatabaseMetaData; import java.sql.SQLException; public class JDBCDriverInfo { private static final String URL = "jdbc:mysql://localhost/nhooo"; private static final String USERNAME = "root"; private static final String PASSWORD = ""; public static void main(String[] args) { try (Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD)) { DatabaseMetaData metadata = conn.getMetaData(); String driverName = metadata.getDriverName(); String driverVersion = metadata.getDriverVersion(); int majorVersion = metadata.getDriverMajorVersion(); int minorVersion = metadata.getDriverMinorVersion(); System.out.println("Driver Name = " + driverName); System.out.println("Driver Version = " + driverVersion); System.out.println("Major Version = " + majorVersion); System.out.println("Minor Version = " + minorVersion); } catch (SQLException e) { e.printStackTrace(); } } }
我们程序的结果:
Driver Name = MySQL Connector/J Driver Version = mysql-connector-java-8.0.17 (Revision: 16a712ddb3f826a1933ab42b0039f7fb9eebc6ec) Major Version = 8 Minor Version = 0
Maven依赖
<!-- https://search.maven.org/remotecontent?filepath=mysql/mysql-connector-java/8.0.17/mysql-connector-java-8.0.17.jar --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.17</version> </dependency>