JDBC中的DatabaseMetaData是什么?其意义何在?
通常,有关数据的数据称为元数据。 DatabaseMetaData 接口提供了一些方法来获取有关您所连接的数据库的信息,例如数据库名称、数据库驱动程序版本、最大列长度等...
以下是一些方法DatabaseMetaData 类。
方法 | 说明|
---|---|
getDriverName() | 检索当前 JDBC 驱动程序的名称 |
getDriverVersion() | 检索当前 JDBC 驱动程序的版本 td> |
getUserName() | 检索用户名。 |
getDatabaseProductName() | 检索当前数据库的名称。 |
getDatabaseProductVersion() | 检索当前数据库的版本。 |
getNumericFunctions() | 检索数字函数列表此数据库可用。 |
getStringFunctions() | 检索此数据库可用的数值函数列表。 td> |
getSystemFunctions() | 检索此数据库可用的系统函数列表。 | getTimeDateFunctions() | 检索此数据库可用的时间和日期函数列表。 |
getURL() | 检索当前数据库的 URL。 |
supportsSavepoints() | 验证当前数据库是否支持保存点 |
supportsStoredProcedures() | 验证当前数据库的天气支持存储过程。 |
supportsTransactions() | 验证当前数据库是否支持事务。 |
示例
以下示例演示 DatabaseMetaData 类的用法。
import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; public class DatabaseMetadataExample { public static void main(String args[])throws Exception { //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/sampleDB"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Creating the DatabaseMetaData object DatabaseMetaData dbMetadata = con.getMetaData(); //invoke the supportsBatchUpdates() method. boolean bool = dbMetadata.supportsBatchUpdates(); if(bool) { System.out.println("Underlying database supports batch updates"); } else { System.out.println("Underlying database doesnt supports batch updates"); } //Retrieving the driver name System.out.println(dbMetadata.getDriverName()); //Retrieving the driver version System.out.println(dbMetadata.getDriverVersion()); //Retrieving the user name System.out.println(dbMetadata.getUserName()); //Retrieving the URL System.out.println(dbMetadata.getURL()); //Retrieving the list of numeric functions System.out.println("Numeric functions: "+dbMetadata.getNumericFunctions()); System.out.println(""); //Retrieving the list of String functions System.out.println("String functions: "+dbMetadata.getStringFunctions()); System.out.println(""); //Retrieving the list of system functions System.out.println("System functions: "+dbMetadata.getSystemFunctions()); System.out.println(""); //Retrieving the list of time and date functions System.out.println("Time and Date funtions: "+dbMetadata.getTimeDateFunctions()); } }登录后复制