[英]Execute multiple sql SELECT queries using JDBC from a Java Program
I am trying to execute multiple SQL Queries that SELECT different columns from different tables of the same Database. 我试图执行多个SQL查询,这些查询从同一数据库的不同表中选择不同的列。 The results will be pasted to an XML.
结果将粘贴到XML。 Is there a way were I can merge the queries and produce a single execute statement?
有没有办法合并查询并生成一个execute语句?
Below you can find the code I use to execute a single query. 在下面,您可以找到我用于执行单个查询的代码。
String SQLquery1="SELECT * FROM TABLE1";
String SQLquery2="SELECT P.TITLE, P.LASTNAME, P.FIRSTNAME, P.ADDRESS, PO.POSTCODE, P.SEX FROM TABLE2 P LEFT OUTER JOIN TABLE3 PO ON P.VALUE2= PO.VALUE2";
//Code that exececutes a single query
Statement stmt=null;
Connection db = null;
ResultSet rs=null;
db = DriverManager.getConnection(url);
stmt=db.createStatement();
rs=stmt.executeQuery(SQLquery1);
I was wondering if I can use the executeQuery to execute both of these queries as a single statement or if there is another way to execute them separately. 我想知道是否可以使用executeQuery将这两个查询作为一条语句执行,或者是否有另一种方法可以分别执行它们。 I mean do I have to execute different statements for each query?
我的意思是我必须为每个查询执行不同的语句吗?
I mean do I have to execute different statements for each query? 我的意思是我必须为每个查询执行不同的语句吗?
You do. 你做。 If you were selecting the same columns in both queries (or at least the same # and type of columns), you could combine the queries with a
UNION ALL
. 如果您在两个查询中选择了相同的列(或者至少选择了相同的#和类型的列),则可以将查询与
UNION ALL
结合使用。 Since the columns differ, you must use different statements (and connections - if you want to run both queries simultaneously). 由于各列不同,因此必须使用不同的语句(和连接-如果要同时运行两个查询)。 If you run the queries sequentially, one connection is sufficient.
如果按顺序运行查询,则一个连接就足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.