简体   繁体   English

如何在Java中执行多个postgres查询?

[英]How to execute multiple postgres queries in java?

I know how to run a simple "select * from table" in java postgres jdbc connector. 我知道如何在java postgres jdbc连接器中运行简单的“从表中选择*”。

But how do we run two or more statements, where statement2 depends on statement1 using java/JDBC-Postgres? 但是,如何使用Java / JDBC-Postgres运行两个或多个语句,其中statement2取决于statement1?

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. 如果没有用JDBC-Postgres简化的方法,请让我知道其他方法。 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/ http://www.mkyong.com/jdbc/jdbc-preparedstatement-example-batch-update/

JavaDoc for addBatch 用于addBatch的JavaDoc

Alternatively, you could execute a stored proc and return cursors. 或者,您可以执行存储的proc并返回游标。 More on that here: https://stackoverflow.com/a/10804730/559095 此处的更多信息: https : //stackoverflow.com/a/10804730/559095

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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