2014-07-01 3 views
0

이 쿼리는 테이블에서 임의의 client_id 값을 선택합니다. 그리고 다른 쿼리에서 그 값이 필요합니다.결과 집합 값을 포스트그레스에서 정수 형식으로 변환하는 방법은 무엇입니까?

String s= "SELECT client_id FROM clients OFFSET random()*(select count(*) from clients) LIMIT 1"; 
d=conn.createStatement(); 
ResultSet rs1 = d.executeQuery(s); 

UPDATE :이

int val = ((Number) rs1).intValue(); 

SQL 오류와 같은 캐스트하려고 내 다른 쿼리의 결과 집합의 값을 사용하려면이

rg.postgresql.jdbc3g.Jdbc3gResultSet cannot be cast to java.lang.Number 

처럼 보여줍니다

insert into invheader(invdate,client_id,amount,tax,total,closed,ship_via,note) 
values(
to_date('"+indate+"', 'DD-mon-YYYY'), 
'"+rs1+"', //<<---- i need to pass the value of result set in integer format 
'"+amont+"', 
'"+tx+"', 
'"+tot+"', 
'"+closd+"', 
'"+shipvia+"', 
'"+not+"')"; 

업데이트 : 지나가고 있습니다. 서블릿에서 값이

+1

동안 (rs1.next()) {int clientID = rs1.getInt ("client_id");}는 ID – SparkOn

+0

Eep를 제공합니다. http://bobby-tables.com/ –

+0

을 참조하십시오. 대신에'PreparedStatement'를 사용하십시오. 응용 프로그램을 SQL 인젝션으로부터 보호하는 것 외에도 다양한 데이터 유형 * 다 * * (날짜, " '', 숫자 등을 포함하는 문자열)을 다루기도합니다. –

답변

0

이 왕복을 피하고 할 모든 것을 SQL

with client_id as (
    select client_id 
    from clients 
    offset random() * (select count(*) from clients) 
    limit 1 
) 
insert into invheader (
    invdate, client_id, amount, tax, total, closed, ship_via, note 
) values (
to_date('"+indate+"', 'DD-mon-YYYY'), 
(select client_id from client_id), //<<---- i need to pass the value of result set in integer format 
'"+amont+"', 
'"+tx+"', 
'"+tot+"', 
'"+closd+"', 
'"+shipvia+"', 
'"+not+"')"; 

을 그리고 SQL 주입의 매우주의 postgresdb합니다 :

https://www.google.com/search?q=sql+injection&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a&channel=sb

관련 문제