2013-03-05 43 views
1
public class DimensionStoneDaoHibernate extends HibernateDaoSupport implements DimensionStoneDao 
     { 

    public Connection con = null; 
    int i=0; 

    public void setCon(Connection con) { 
     this.con = getSession().connection(); 
    } 

    public Connection getCon() {   

     setCon(con);   
     return con; 
    } 
     public List getMineralByApplicationId(String appId) { 
      List<String> mineral = new ArrayList<String>(); 

     PreparedStatement query =getCon().prepareStatement("select * from multiple_mineral_report(?) as types(val1 varchar)",ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);  

       query.setInt(1, Integer.parseInt(appId));    
        ResultSet rs=query.executeQuery(); 

        String value = new String(); 
        i = 0; 
        while(rs.next()){    
         value = rs.getString(1); 
         mineral.add(value); 
        } 

     } 

} 

上面的SQL函數multiple_mineral_report(?)返回字符串。當執行上面的代碼下面的異常被拋出:Postgres錯誤:列定義列表只允許返回「記錄」的函數

org.postgresql.util.PSQLException: ERROR: a column definition list is only allowed for functions returning "record"

CREATE OR REPLACE FUNCTION multiple_mineral_report(b integer) 
    RETURNS character varying AS 
$BODY$ 
declare 
k varchar := '';  
sql_res record; 
begin 
for sql_res in execute 'select mineral_name from application_mineral as am inner join mineral as m on(am.mineral=m.mineral_id) where am.application_id = '||$1||'' loop 
    k = k || sql_res.mineral_name || ', '; 
end loop; 
return k; 
end; 
$BODY$ 
    LANGUAGE plpgsql VOLATILE 
    COST 100; 
ALTER FUNCTION multiple_mineral_report(integer) OWNER TO postgres; 
+0

SShould添加更多的信息:與您使用的語言開始。 – 2013-03-05 09:42:46

+0

好的,我已根據您的要求編輯 – 2013-03-05 11:34:44

+1

請向我們展示'multiple_mineral_report'的'create function'聲明 – 2013-03-05 11:36:42

回答

1

你的錯誤是在這裏:

PreparedStatement query =getCon().prepareStatement(
"select * from multiple_mineral_report(?) as types(val1 varchar)" 
,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

你不必(實際上不可能)將返回類型定義與一個列定義列表,如果它已被很好地定義。你的功能被定義爲

RETURNS character varying 

放棄粗體部分,這個特殊的錯誤應該消失。
不過說真的,因爲你的函數返回一個值,你想打電話給你的功能就像這樣:

SELECT multiple_mineral_report(?) 
+0

謝謝您的建議,這對我很有幫助 – 2013-03-07 04:25:12

相關問題