首頁  >  文章  >  資料庫  >  JDBC中的DatabaseMetaData是什麼?其意義何在?

JDBC中的DatabaseMetaData是什麼?其意義何在?

王林
王林轉載
2023-08-31 16:53:02678瀏覽

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());
   }
}

輸出

Connection established......
Underlying database supports batch updates
MySQL-AB JDBC Driver
mysql-connector-java-5.1.12 ( Revision: ${bzr.revision-id} )
root@localhost
jdbc:mysql://localhost/sampleDB
Numeric functions:
ABS,ACOS,ASIN,ATAN,ATAN2,BIT_COUNT,CEILING,COS,COT,DEGREES,EXP,FLOOR,LOG,LOG10,MAX
,MIN,MOD,PI,POW,POWER,RADIANS,RAND,ROUND,SIN,SQRT,TAN,TRUNCATE
String functions:
ASCII,BIN,BIT_LENGTH,CHAR,CHARACTER_LENGTH,CHAR_LENGTH,CONCAT,CONCAT_WS,CONV,ELT,E
XPORT_SET,FIELD,FIND_IN_SET,HEX,INSERT,INSTR,LCASE,LEFT,LENGTH,LOAD_FILE,LOCATE,LO
CATE,LOWER,LPAD,LTRIM,MAKE_SET,MATCH,MID,OCT,OCTET_LENGTH,ORD,POSITION,QUOTE,REPEA
T,REPLACE,REVERSE,RIGHT,RPAD,RTRIM,SOUNDEX,SPACE,STRCMP,SUBSTRING,SUBSTRING,SUBSTR
ING,SUBSTRING,SUBSTRING_INDEX,TRIM,UCASE,UPPER
System functions:
DATABASE,USER,SYSTEM_USER,SESSION_USER,PASSWORD,ENCRYPT,LAST_INSERT_ID,VERSION
Time and Date funtions:
DAYOFWEEK,WEEKDAY,DAYOFMONTH,DAYOFYEAR,MONTH,DAYNAME,MONTHNAME,QUARTER,WEEK,YEAR,H
OUR,MINUTE,SECOND,PERIOD_ADD,PERIOD_DIFF,TO_DAYS,FROM_DAYS,DATE_FORMAT,TIME_FORMAT
,CURDATE,CURRENT_DATE,CURTIME,CURRENT_TIME,NOW,SYSDATE,CURRENT_TIMESTAMP,UNIX_TIME
STAMP,FROM_UNIXTIME,SEC_TO_TIME,TIME_TO_SEC

以上是JDBC中的DatabaseMetaData是什麼?其意義何在?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除