转换结果从SQL阵列设置为字符串数组 [英] Convert a Result Set from SQL Array to Array of Strings
问题描述
我查询INFORMATION_SCHEMA.COLUMNS表在我的PostgreSQL数据库。使用一个表名,结果集查找所有的列名,类型,以及它是否是可空(除了主键,ID)。这是正在使用的查询:
SELECT列名,IS_NULLABLE,DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS
其中较低(TABLE_NAME)=低('TABLE1')AND COLUMN_NAME!='ID'
ORDER BY ORDINAL_POSITION;
我对这些结果的字符串数组,我试图用的ResultSet方法的getArray(字符串columnLabel)
来避免通过结果循环。我想存储在字符串数组返回数组,却得到了一个类型不匹配错误(类型不匹配:不能从数组转换为String [] 的)。
有没有办法到SQL Array对象转换或强制转换为String []?
相关code:
的String [] COLUMNNAME,类型,可为空;//获取字段名称,类型和放大器;空性
查询字符串=SELECT列名,IS_NULLABLE,DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS
+WHERE较低(TABLE_NAME)=低('+ tableName值+')AND COLUMN_NAME!='ID'
+ORDER BY ORDINAL_POSITION尝试{
结果集RS = Query.executeQueryWithRS(C,查询);
COLUMNNAME = rs.getArray(rs.getArray(栏);
类型= rs.getArray(DATA_TYPE);
可空= rs.getArray(IS_NULLABLE);
}赶上(例外五){
e.printStackTrace();
}
使用:
阵列A = rs.getArray(IS_NULLABLE);
的String [] =可空(字符串[])a.getArray();
作为解释这里
阵列
是SQL类型,的getArray()
返回一个对象转换为Java数组。
I am querying the information_schema.columns table in my PostgreSQL database. Using a table name, the result set finds all the column names, type, and whether it is nullable (except for the primary key, 'id'). This is the query being used:
SELECT column_name, is_nullable,data_type FROM information_schema.columns
WHERE lower(table_name) = lower('TABLE1') AND column_name != 'id'
ORDER BY ordinal_position;
I have a string array for each of these results and I am trying to use the ResultSet method getArray(String columnLabel)
to avoid looping through the results. I want to store the returned Arrays in the string arrays, but get a type mismatch error ("Type mismatch: cannot convert from Array to String[]").
Is there a way to convert or typecast the SQL Array object to a String[]?
Relevant Code:
String[] columnName, type, nullable;
//Get Field Names, Type, & Nullability
String query = "SELECT column_name, is_nullable,data_type FROM information_schema.columns "
+ "WHERE lower(table_name) = lower('"+tableName+"') AND column_name != 'id' "
+ "ORDER BY ordinal_position";
try{
ResultSet rs = Query.executeQueryWithRS(c, query);
columnName = rs.getArray(rs.getArray("column_name");
type = rs.getArray("data_type");
nullable = rs.getArray("is_nullable");
}catch (Exception e) {
e.printStackTrace();
}
Use:
Array a = rs.getArray("is_nullable");
String[] nullable = (String[])a.getArray();
As explained here
Array
is SQL type, getArray()
returns an object to cast to java array.
这篇关于转换结果从SQL阵列设置为字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!