Java DatabaseMetaData getTypeInfo() 方法及示例


DatabaseMetadata 介面的 getTypeInfo() 方法用於獲取底層資料庫支援的所有資料型別的描述。

此方法返回一個 ResultSet 物件,描述支援的資料型別。此物件包含以下詳細資訊的值(作為列名):

列名
資料型別
描述
TYPE_NAME
字串
資料型別的名稱。
DATA_TYPE
整數
表示此資料型別的整數值。
PRECISION
整數
此資料型別的最大精度。
LITERAL_PREFIX
字串
用於引用字串文字的字首。
LITERAL_SUFFIX
字串
用於引用字串文字的字尾。
CASE_SENSITIVE
布林值
確定此資料型別是否區分大小寫
UNSIGNED_ATTRIBUTE
布林值
確定此資料型別是否為無符號屬性。
FIXED_PREC_SCALE
布林值
確定當前資料型別是否可用作貨幣值。
AUTO_INCREMENT
布林值
確定當前資料型別是否可用於自動遞增。
LOCAL_TYPE_NAME
字串
此資料型別的本地化版本。

獲取 DatabaseMetaData 物件:

  • 確保您的資料庫正在執行。
  • 使用 DriverManager 類的 registerDriver() 方法註冊驅動程式。傳遞與底層資料庫對應的驅動程式類的物件。
  • 使用 DriverManager 類的 getConnection() 方法獲取連線物件。將資料庫的 URL、使用者名稱和資料庫使用者的密碼作為字串變數傳遞。
  • 使用 Connection 介面的 getMetaData() 方法獲取當前連線的 DatabaseMetaData 物件。

最後,透過呼叫 DatabaseMetaData 介面的 getTypeInfo() 方法,獲取包含支援的資料型別描述的 ResultSet 物件。

以下 JDBC 程式建立與 MySQL 資料庫的連線,並檢索所有資料型別的描述。

示例

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DatabaseMetadata_getTypeInfo {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String url = "jdbc:mysql:///mydatabase";
      Connection con = DriverManager.getConnection(url, "root", "password");
      System.out.println("Connection established......");
      //Retrieving the meta data object
      DatabaseMetaData metaData = con.getMetaData();
      //Retrieving the columns in the database
      ResultSet info = metaData.getTypeInfo();
      //Printing the column name and size
      while (info.next()) {
         System.out.println("Data type name: "+info.getString("TYPE_NAME"));
         System.out.println("Integer value representing this datatype: "+info.getInt("DATA_TYPE"));
         System.out.println("Maximum precision of this datatype: "+info.getInt("PRECISION"));
         if(info.getBoolean("CASE_SENSITIVE")) {
            System.out.println("Current datatype is case sensitive ");
         } else {
            System.out.println("Current datatype is not case sensitive ");
         }
         if(info.getBoolean("AUTO_INCREMENT")) {
            System.out.println("Current datatype can be used for auto increment");
         } else {
            System.out.println("Current datatype can not be used for auto increment");
         }
         System.out.println(" ");
      }
   }
}

輸出

Connection established......
Data type name: BIT
Integer value representing this datatype: -7
Maximum precision of this datatype: 1
Current datatype is case sensitive
Current datatype can not be used for auto increment
Data type name: BOOL
Integer value representing this datatype: -7
Maximum precision of this datatype: 1
Current datatype is case sensitive
Current datatype can not be used for auto increment
Data type name: TINYINT
Integer value representing this datatype: -6
Maximum precision of this datatype: 3
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: TINYINT UNSIGNED
Integer value representing this datatype: -6
Maximum precision of this datatype: 3
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: BIGINT
Integer value representing this datatype: -5
Maximum precision of this datatype: 19
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: BIGINT UNSIGNED
Integer value representing this datatype: -5
Maximum precision of this datatype: 20
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: LONG VARBINARY
Integer value representing this datatype: -4
Maximum precision of this datatype: 16777215
Current datatype is case sensitive
Current datatype can not be used for auto increment
Data type name: MEDIUMBLOB
Integer value representing this datatype: -4
Maximum precision of this datatype: 16777215
Current datatype is case sensitive
Current datatype can not be used for auto increment
Data type name: LONGBLOB
Integer value representing this datatype: -4
Maximum precision of this datatype: 2147483647
Current datatype is case sensitive
Current datatype can not be used for auto increment
Data type name: BLOB
Integer value representing this datatype: -4
Maximum precision of this datatype: 65535
Current datatype is case sensitive
Current datatype can not be used for auto increment
Data type name: TINYBLOB
Integer value representing this datatype: -4
Maximum precision of this datatype: 255
Current datatype is case sensitive
Current datatype can not be used for auto increment
Data type name: VARBINARY
Integer value representing this datatype: -3
Maximum precision of this datatype: 255
Current datatype is case sensitive
Current datatype can not be used for auto increment
Data type name: BINARY
Integer value representing this datatype: -2
Maximum precision of this datatype: 255
Current datatype is case sensitive
Current datatype can not be used for auto increment
Data type name: LONG VARCHAR
Integer value representing this datatype: -1
Maximum precision of this datatype: 16777215
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: MEDIUMTEXT
Integer value representing this datatype: -1
Maximum precision of this datatype: 16777215
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: LONGTEXT
Integer value representing this datatype: -1
Maximum precision of this datatype: 2147483647
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: TEXT
Integer value representing this datatype: -1
Maximum precision of this datatype: 65535
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: TINYTEXT
Integer value representing this datatype: -1
Maximum precision of this datatype: 255
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: CHAR
Integer value representing this datatype: 1
Maximum precision of this datatype: 255
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: NUMERIC
Integer value representing this datatype: 2
Maximum precision of this datatype: 65
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: DECIMAL
Integer value representing this datatype: 3
Maximum precision of this datatype: 65
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: INTEGER
Integer value representing this datatype: 4
Maximum precision of this datatype: 10
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: INTEGER UNSIGNED
Integer value representing this datatype: 4
Maximum precision of this datatype: 10
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: INT
Integer value representing this datatype: 4
Maximum precision of this datatype: 10
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: INT UNSIGNED
Integer value representing this datatype: 4
Maximum precision of this datatype: 10
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: MEDIUMINT
Integer value representing this datatype: 4
Maximum precision of this datatype: 7
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: MEDIUMINT UNSIGNED
Integer value representing this datatype: 4
Maximum precision of this datatype: 8
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: SMALLINT
Integer value representing this datatype: 5
Maximum precision of this datatype: 5
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: SMALLINT UNSIGNED
Integer value representing this datatype: 5
Maximum precision of this datatype: 5
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: FLOAT
Integer value representing this datatype: 7
Maximum precision of this datatype: 10
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: DOUBLE
Integer value representing this datatype: 8
Maximum precision of this datatype: 17
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: DOUBLE PRECISION
Integer value representing this datatype: 8
Maximum precision of this datatype: 17
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: REAL
Integer value representing this datatype: 8
Maximum precision of this datatype: 17
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: VARCHAR
Integer value representing this datatype: 12
Maximum precision of this datatype: 255
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: ENUM
Integer value representing this datatype: 12
Maximum precision of this datatype: 65535
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: SET
Integer value representing this datatype: 12
Maximum precision of this datatype: 64
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: DATE
Integer value representing this datatype: 91
Maximum precision of this datatype: 0
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: TIME
Integer value representing this datatype: 92
Maximum precision of this datatype: 0
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: DATETIME
Integer value representing this datatype: 93
Maximum precision of this datatype: 0
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: TIMESTAMP
Integer value representing this datatype: 93
Maximum precision of this datatype: 0
Current datatype is not case sensitive
Current datatype can not be used for auto increment

更新於:2019年7月30日

434 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.