简体   繁体   中英

How to execute multiple postgres queries in java?

I know how to run a simple "select * from table" in java postgres jdbc connector.

But how do we run two or more statements, where statement2 depends on statement1 using java/JDBC-Postgres?

Example #1:
SET search_path TO a,b,c;
Select * from table1;

Example #2:
Create temporary table tempabc ... definition and value;
Select * from tableDEF inner join tempabc USING (tableDEF.refid = tableDEF.id) where table a=1;

This question may already be answered but I cannot find duplicates for it. If nothing in staightforward JDBC-Postgres way let me know other ways. Thanks!

This may be what you are looking for: Batch statements. The caveat is that your statements must be insert/update/delete (actions).

http://www.mkyong.com/jdbc/jdbc-preparedstatement-example-batch-update/

JavaDoc for addBatch

Alternatively, you could execute a stored proc and return cursors. More on that here: https://stackoverflow.com/a/10804730/559095

对于第一个问题,您可以使用setSchema更改搜索路径。至于第二个问题,只要您使用相同的连接,临时表就会在那里。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM