简体   繁体   English

是否可以从Java代码获取嵌套表/数组的元数据?

[英]Is it possible to get metadata on a nested table/array from Java code?

Normally for a user defined type I can simply do the following (Simplified but functional example): 通常对于用户定义的类型,我可以简单地执行以下操作(简化但功能示例):

//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;

Except now I have a table that's defined with a primitive type instead of a struct, and I can't seem to access the metadata. 除了现在我有一个用原始类型而不是结构定义的表,我似乎无法访问元数据。 My current code is: 我目前的代码是:

//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?

I hope that I understand you right. 我希望我理解你。 If you can get column info, you can try following codes. 如果您可以获得列信息,可以尝试以下代码。

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

At times, in a real-world enterprise environment where you don't have admin perms on an Oracle DB, you might not be able to rely on Java's DatabaseMetaData class but you might be able to rely on DDL queries. 有时,在Oracle DB上没有管理员权限的实际企业环境中,您可能无法依赖Java的DatabaseMetaData类,但您可能依赖于DDL查询。 By that, I mean that you might be able to execute DDL queries as you would normally run a SQL query in your Java app. 通过这种方式,我的意思是您可以像在Java应用程序中运行SQL查询一样执行DDL查询。

For example: 例如:

  1. You might be able to get the datatype of the column by executing the following query as shown in this answer : select t.data_type from user_tab_columns t where t.TABLE_NAME = 'MYTABLE1' and t.COLUMN_NAME='COLUMN_VALUE' . 您可以通过执行以下查询来获取列的数据类型,如下面的答案所示: select t.data_type from user_tab_columns t where t.TABLE_NAME = 'MYTABLE1' and t.COLUMN_NAME='COLUMN_VALUE'
  2. You might be able to get the display size of the column by executing the following query as shown in this answer : SELECT data_length FROM all_tab_columns WHERE table_name = 'MYTABLE1' AND column_name = 'COLUMN_VALUE' . 您可以通过执行以下查询来获取列的显示大小,如下面的答案所示: SELECT data_length FROM all_tab_columns WHERE table_name = 'MYTABLE1' AND column_name = 'COLUMN_VALUE'
  3. You might be able to get the nullability of the column by executing the following query as shown in this answer : select nullable from user_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM