[英]Is it possible to get metadata on a nested table/array from Java code?
通常對於用戶定義的類型,我可以簡單地執行以下操作(簡化但功能示例):
//Create table
create type myType1 as object( a char(2), b char(2) );
create type myTable1 as table of myType1;
//Java code
ArrayDescriptor des = ArrayDescriptor.createDescriptor( "MYTABLE1", con);
StructDescriptor sDes = StructDescriptor.createDescriptor( des.getBaseName(), con);
//Populte the metadata
String columnName = sDes.getColumnName(0);
int oracleType = sDes.getColumnType(0);
int maxSize = sDes.getColumnDisplaySize(0);
boolean isNullable = sDes.isNullable(0)>0;
除了現在我有一個用原始類型而不是結構定義的表,我似乎無法訪問元數據。 我目前的代碼是:
//Create table
create or replace type myTable2 as table of char(2);
//Java code
ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor( "MYTABLE2", con);
//This next line would throw an exception, as CHAR type is not a structure
//StructDescriptor sDes = StructDescriptor.createDescriptor( des.getBaseName(), con);
//Populte the metadata
int oracleType = descriptor.getBaseType(); // This Works
String columnName = "COLUMN_VALUE"; // This Works (I think)
int maxSize = ???? // How do I access the '2'?
boolean isNullable = ???? // How do I access isNullable?
我希望我理解你。 如果您可以獲得列信息,可以嘗試以下代碼。
DatabaseMetaData databaseMetaData = connection.getMetaData();
ResultSet columns = databaseMetaData.getColumns(null,null, tableName, null);
while(columns.next())
{
String columnName = columns.getString("COLUMN_NAME");
String datatype = columns.getString("DATA_TYPE");
String columnsize = columns.getString("COLUMN_SIZE");
String decimaldigits = columns.getString("DECIMAL_DIGITS");
String isNullable = columns.getString("IS_NULLABLE");
String is_autoIncrment = columns.getString("IS_AUTOINCREMENT");
}
有時,在Oracle DB上沒有管理員權限的實際企業環境中,您可能無法依賴Java的DatabaseMetaData
類,但您可能依賴於DDL查詢。 通過這種方式,我的意思是您可以像在Java應用程序中運行SQL查詢一樣執行DDL查詢。
例如:
select t.data_type from user_tab_columns t where t.TABLE_NAME = 'MYTABLE1' and t.COLUMN_NAME='COLUMN_VALUE'
。 SELECT data_length FROM all_tab_columns WHERE table_name = 'MYTABLE1' AND column_name = 'COLUMN_VALUE'
。 select nullable from user_tab_columns where table_name = 'MYTABLE1' and column_name = 'COLUMN_VALUE';
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.