如何在postgres中将结果集值转换为整数格式?

时间:2014-07-01 08:45:53

标签: postgresql

此查询从表中选择随机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);

更新:我试图像这样投射

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+"')";

更新:我将值从servlet传递给postgresdb

1 个答案:

答案 0 :(得分: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注入:

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