是否可以从JDBC元数据结构中获取数组列的类型?
re = openedConnection.getMetaData().getColumns(...)
while (rs.next())
{
final String columnName = rs.getString("COLUMN_NAME");
final int columnType = rs.getInt("DATA_TYPE");
...
type返回ARRAY但是一个数组(int,text,numeric ...)?
理想情况下,数据库无关(Oracle和Postgres支持这不确定MySQL或SQL Server)
答案 0 :(得分:0)
使用java.sql.ResultSetMetaData.getColumnTypeName()
:
Oracle安装程序:
CREATE OR REPLACE TYPE random_array IS TABLE OF VARCHAR2(42);
/
<强>爪哇强>:
Class.forName("oracle.jdbc.OracleDriver");
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl",
"username",
"password"
);
PreparedStatement st = con.prepareStatement(
"SELECT random_array( 'a', 'b', 'c' ) AS array FROM DUAL"
);
ResultSet rs = st.executeQuery();
while( rs.next() )
{
String type = rs.getMetaData().getColumnTypeName(1);
String[] data = (String[]) rs.getArray(1).getArray();
System.out.println( type + ": " + Arrays.toString( data ) );
}
st.close();
con.close();
<强>输出强>:
SCHEMA_NAME.RANDOM_ARRAY: [a, b, c]
如果您想返回Oracle并找到可以使用的集合元素的类型:
SELECT ELEM_TYPE_NAME,
LENGTH,
PRECISION,
SCALE
FROM ALL_COLL_TYPES
WHERE TYPE_NAME = 'RANDOM_ARRAY'
AND OWNER = 'SCHEMA_NAME';
哪个输出:
ELEM_TYPE_NAME LENGTH PRECISION SCALE
-------------- ------ --------- -----
VARCHAR2 42