Postgres plpgsql函数:
CREATE OR REPLACE FUNCTION usersList()
RETURNS TABLE(at varchar,name varchar,surname varchar) AS $$
BEGIN
RETURN QUERY SELECT * FROM users;
END;
$$ LANGUAGE plpgsql;
和java代码
result = Pstatement.executeQuery("Select usersList() ");
while(result.next()) {
System.out.println(result.getString(("at")));
System.out.println(result.getString(("name")));
System.out.println(result.getString(("surname")));
}
Java error sql exception message:
Message: The column name at was not found in this ResultSet.
SQLState: 42703
ErrorCode: 0
如何从函数返回所有表列,然后在java中打印它们?
答案 0 :(得分:1)
Postgres plpgsql函数:
CREATE OR REPLACE FUNCTION usersList()
RETURNS TABLE(at varchar,name varchar,surname varchar) AS $$
BEGIN
RETURN QUERY SELECT * FROM users;
END;
$$ LANGUAGE plpgsql;
和java代码
result = Pstatement.executeQuery("SELECT * FROM usersList() ");
while(result.next()) {
System.out.println(result.getString(("at")));
System.out.println(result.getString(("name")));
System.out.println(result.getString(("surname")));
}
致RealSkeptic&&尼克巴恩斯!!!
答案 1 :(得分:0)
您可以使用result.getMetaData().getColumnCount()
查看检索的列数,然后调用result.getMetaData().getColumnName(x)
来检查列的名称(将x
替换为列数)。< / p>
从技术上讲,您应该能够像这样编写代码块:
result = Pstatement.executeQuery("Select usersList() ");
String mesites[];
while(result.next()) {
int columnCount = result.getMetaData().getColumnCount();
System.out.println("Found:"+columnCount+" columns.");
for(int i=1; i<=columnCount; i++){
System.out.println(result.getString(result.getMetaData().getColumnName(i)));
}
}
然后应该打印出在该结果集中检索的任何列,而不管名称(如果有的话)。