Java DatabaseMetaData getTypeInfo()方法与示例
getTypeInfo()
的DatabaseMetaData接口的方法用于获得由底层数据库支持的所有数据类型的说明。
此方法返回一个ResultSet对象,该对象描述了受支持的数据类型。该对象保存以下详细信息的值(作为列名)-
获取DatabaseMetaData对象-
确保您的数据库已启动并正在运行。
使用registerDriver()
DriverManager类的方法注册驱动程序。传递与基础数据库相对应的驱动程序类的对象。
使用getConnection()
DriverManager类的方法获取连接对象。将URL和数据库中的用户密码作为字符串变量传递给数据库。
使用getMetaData()
Connection接口的方法获取有关当前连接的DatabaseMetaData对象。
最后,通过调用getTypeInfo()
DatabaseMetaData接口的方法来获取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("Connection established......"); //检索元数据对象 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(" "); } } }
输出结果
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