简体   繁体   English

我想在Java Eclipse中使用JDBC从上一个查询对ResultSet执行另一个SQL查询

[英]I want to execute another SQL query on the ResultSet from the previous query using JDBC in Java Eclipse

I have a JDBC project that I was just assigned in class and we are supposed to execute this query to our University database via JDBC Java Eclipse: 我有一个刚刚在班级分配的JDBC项目,我们应该通过JDBC Java Eclipse对我们的大学数据库执行此查询:

SELECT dept_name, COUNT(*) AS COUNT FROM student GROUP BY dept_name;

And with the returned resultSet query the result set with: 并使用返回的resultSet查询结果集:

HAVING COUNT = (SELECT MAX(ACOUNT) AS MAXCOUNT
                FROM (SELECT dept_name, COUNT(*) AS ACOUNT
                      FROM student GROUP BY dept_name) AS ATable);

I have looked everywhere in my book and via google and can't find tutorials for examples on how to do this. 我在我的书中和通过google浏览了所有内容,但找不到有关如何执行此操作的示例教程。 Any links/documentation you guys can point me to or a starting point? 你们可以指向我的任何链接/文档还是一个起点?

SOLUTION EDIT: I finally got clarification from my teacher, he meant to execute the SQL statement in my java program and loop through the results and using java and sift out the same information that the second SQL statement would acquire had it been attached to the original statement. 解决方案编辑:我终于从老师那里得到澄清,他的意思是在我的Java程序中执行SQL语句并遍历结果并使用Java筛选出第二条SQL语句附加到原始SQL语句时将获得的相同信息。声明。 Thank you to those that took the time to try and help. 感谢那些花时间尝试和帮助的人。

Your assignment only provides pseudo-code, and the word "execute" must be a mistake. 您的作业仅提供伪代码,并且“执行”一词必须是错误的。 As advised by others, "executing a query on a result set" makes no sense. 正如其他人所建议的那样,“对结果集执行查询”没有任何意义。

Assuming your tutor meant "filter out" instead, you might be expected to run two separate statements ( SELECT dept_name... and SELECT MAX(... ), then iterate over the first result set searching for the result that matches the second result set. 假设您的导师意味着“过滤掉”,则可能希望您运行两个单独的语句( SELECT dept_name...SELECT MAX(... ),然后在第一个结果集上进行迭代以搜索与第二个结果匹配的结果组。

Pseudo-code: 伪代码:

RSet1 = query("SELECT dept_name, COUNT(*) AS COUNT FROM student GROUP BY dept_name;");
RSet2 = query("SELECT MAX(ACOUNT) AS MAXCOUNT ...");
RowInR2 = RSet2[0]; // first and only row

foreach RowInR1 in RSet1
    if (
        RowInR1.count = RowInR2.maxcount
    ) {
        return RowInR1; // this is your result
    }
}

Please bear in mind this approach is absurd except for education purpose. 请记住,除了教育目的之外,这种方法是荒谬的。 It is possible to extract this data (one single row) in just one query, without the need to extract and parse the results from [Query 1] (this query is the exact concatenation of your two queries). 只需一个查询就可以提取该数据(单行),而无需提取并解析[Query 1]的结果(此查询是您的两个查询的精确串联)。

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

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