2016-08-23 3 views
-2

나는 매개 변수에 따라 0 또는 1을 반환하는 postgreSQL의 함수가 있는데,이 함수에 액세스하는 메소드가 있지만 실행하면 오류가 발생하며 현재 설정에 문제가 있습니다. 날짜를 타임 스탬프로 표시합니다. simpleDataFormat 및 많은 다른 것들을 사용하여 패턴을 추가하려고했지만 couldnt 할 수 있습니다. 미리 감사드립니다! DB에서타임 스탬프에서 오늘 날짜 가져 오기

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. 

연료 소모량 :

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

가능한 중복 (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, biginteger 번호입니다. 하지만 함수는 타임 스탬프 값을 예상하므로이 오류가 발생합니다.

솔루션 :

당신은 "YYYY-MM-DD 형식 HH24 : MI : SS"와 같은 형식의 문자열로 "날짜/시간"값을 통과해야 다음 문자열로 매개 변수를 설정 .. .

... 또는 당신은 여전히 ​​밀리 초에 날짜를 통과 할 경우, 당신은 라인을 변경하여, 날짜/시간이 BIGINT 수를 "전환"해야 :

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

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

to_timestamp이 1970년 1월 1일 이후 (초)을 기대하기 때문에 우리가 1000에 의해 :data를 분할해야하는 이유는, 당신은 (밀리 초)을 통과한다.

단일 인수 to_timestamp 기능도 사용할 수 있습니다. double precision 인수를 받아 을 받아들이고 Unix epoch (1970-01-01 00 : 00 : 00 + 00 이후 초 )에서 시간대가있는 타임 스탬프로 변환합니다. [자바 시간대없이 시간을 얻을]의 (정수 유닉스 시대가 암시 적으로 배정 밀도로 캐스팅되어 있습니다.)

+0

도움을 주셔서 감사합니다. 알았어.] –

관련 문제