2016-08-23 56 views
-2

我有一個postgreSQL函數根據它的參數返回0或1,我有一個方法可以訪問這個函數,但是當我運行時出現錯誤,我在設置當前日期在時間戳。我試圖用simpleDataFormat添加模式,還有很多其他的東西,但我無法做到。提前致謝!在時間戳中獲取今天的日期

ERROR: function inserir_posicao(numeric, bigint, double precision, double precision, numeric) does not exist 
    Dica: No function matches the given name and argument types. You might need to add explicit type casts. 

Funtion在DB:

CREATE OR REPLACE FUNCTION public.inserir_posicao(
    _tag bigint, 
    _data_hora timestamp without time zone, 
    _lat double precision, 
    _long double precision, 
    _gado_id bigint) 
    RETURNS integer AS 
$BODY$declare 
    tagPesq BigInt; 
begin 

    select tag into tagPesq from coordenadas where tag = $1; 
    if tagPesq is not null and tagPesq > 0 then 
    update coordenadas set pos_data = $2, 
    pos_latitude = $3, 
    pos_longitude = $4, 
    gado_id = $5 
    where tag_id = $1; 
    else 
    insert into coordenadas(pos_data,pos_latitude,pos_longitude, 
    tag_id, gado_id) values ($2,$3,$4,$1,$5); 
    end if; 

    return 1; 

    EXCEPTION WHEN RAISE_EXCEPTION THEN 
    BEGIN 
    return 0; 
    END; 
end;$BODY$ 
    LANGUAGE plpgsql VOLATILE 
    COST 100; 
ALTER FUNCTION public.inserir_posicao(bigint, timestamp without time zone, double precision, double precision, bigint) 
    OWNER TO postgres; 

方法:

public int inserirPosicao(BigInteger tagId, BigInteger gadoId, double lat, double lon) { 
     Timestamp timestamp = new Timestamp(System.currentTimeMillis()); 


     Query qry = manager.createNativeQuery("select inserir_posicao(:tag,:data,:lat,:lng,:gado)"); 
     qry.setParameter("tag", tagId); 
     qry.setParameter("data", timestamp.getTime()); 
     qry.setParameter("lat", lat); 
     qry.setParameter("lng", lon); 
     qry.setParameter("gado", gadoId); 
     return (int) qry.getSingleResult(); 
    } 
+1

的[獲取時間沒有在Java中的時區]可能的複製(http://stackoverflow.com/questions/39106041/get-time-without-time -zone-in-java) –

+0

你的問題在這一行:qry.setParameter(「data」,timestamp.getTime());請檢查我的答案。 – Christian

回答

1

您的問題是在這條線:

qry.setParameter("data", timestamp.getTime()); 

getTime()返回的日期/時間的方法毫秒ince 01/01/1970,這是一個很大的數字。但是你的函數需要一個時間戳值,所以你會得到這個錯誤。

解決方案:

你要通過一個 「日期/時間」 的值作爲像 「YYYY-MM-DD HH24:MI:SS」 一個格式化串,然後設置參數作爲字符串.. 。

...或者,如果你想傳遞仍然以毫秒爲單位的日期,你要「轉變」這個BIGINT數日期/時間,通過改變線路:

Query qry = manager.createNativeQuery("select inserir_posicao(:tag,:data,:lat,:lng,:gado)"); 

Query qry = manager.createNativeQuery("select inserir_posicao(:tag, to_timestamp(:data/1000) ,:lat,:lng,:gado)"); 

我們必須通過1000:data的原因是因爲to_timestamp預計自01/01/1970的秒數,和要傳遞的毫秒數。

單參數to_timestamp函數也可用;它接受 雙精度參數,並從Unix紀元(自1970-01-01 00:00:00:00 00:00 00秒之間的 秒)轉換爲帶時區的時間戳。 (整數 Unix的時代被隱式轉換爲雙精度)。

+0

感謝您的幫助基督徒!我知道了。] –

相關問題