Java DatabaseMetaData getTypeInfo() 方法示例
jdbcjava 8object oriented programmingprogramming
DatabaseMetadata 接口的 getTypeInfo() 方法用于获取底层数据库支持的所有数据类型的描述。
此方法返回一个 ResultSet 对象,该对象描述了所支持的数据类型。此对象包含以下详细信息的值(作为列名) −
列名 | 数据类型 | 描述 |
---|---|---|
TYPE_NAME | String | 数据类型的名称。 |
DATA_TYPE | int | 表示此数据类型的整数值。 |
PRECISION | int | 此数据类型的最大精度。 |
LITERAL_PREFIX | String | 用于引用字符串文字的前缀。 |
LITERAL_SUFFIX | String | 用于引用字符串的后缀文字。 |
CASE_SENSITIVE | 布尔值 | 确定此数据类型是否区分大小写 |
UNSIGNED_ATTRIBUTE | 布尔值 | 确定此数据类型是否为无符号属性。 |
FIXED_PREC_SCALE | 布尔值 | 确定当前数据类型可以用作货币值。 |
AUTO_INCREMENT | 布尔值 | 确定当前数据类型是否可用于自动增量。 |
LOCAL_TYPE_NAME | String | 此数据类型的本地化版本。 |
获取 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 { //注册驱动程序 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //获取连接 String url = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(url, "root", "password"); System.out.println("连接已建立......"); //检索元数据对象 DatabaseMetaData metaData = con.getMetaData(); //检索数据库中的列 ResultSet info = metaData.getTypeInfo(); //打印列名和大小 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(" "); } } }
输出
连接已建立...... 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