2016-08-23 104 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

感谢您的帮助基督徒!我知道了。] –