Java中如何获取数据库产品信息?
下面的代码可帮助您获取有关在创建程序时使用的数据库的一些产品信息。您可以检索数据库信息,例如产品的主要和次要版本,数据库产品名称及其发行版本。
package org.nhooo.example.jdbc;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
public class DatabaseProductInfo {
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 connection =
DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
DatabaseMetaData metadata = connection.getMetaData();
String productName = metadata.getDatabaseProductName();
String productVersion = metadata.getDatabaseProductVersion();
int majorVersion = metadata.getDatabaseMajorVersion();
int minorVersion = metadata.getDatabaseMinorVersion();
System.out.println("Product Name = " + productName);
System.out.println("Product Version = " + productVersion);
System.out.println("Major Version = " + majorVersion);
System.out.println("Minor Version = " + minorVersion);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
结果如下:
Product Name = MySQLProduct Version = 5.7.21
Major Version = 5
Minor Version = 7
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>
以上是 Java中如何获取数据库产品信息? 的全部内容, 来源链接: utcz.com/z/326334.html