Java 从 resultSet 生成查询并执行新查询 [英] Java generating query from resultSet and executing the new query

查看:44
本文介绍了Java 从 resultSet 生成查询并执行新查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用某些查询结果来生成另一个查询并执行新查询,但这似乎不起作用.第二个查询没有被执行.有人可以告诉我为什么吗?这是代码的那部分.

I am trying to use some query result to generate another query and execute the new query but that does not seem to work. The second query is not being executed. Can someone please tell me why? This is that part of the code.

Statment stmt = connnection.createStatement();
Statment stmt2 = connnection.createStatement();
ResultSet r =  stmt.executeQuery("Select * from employees");
while (r.next()) {
            String Str = "Select name from employees where employeeId = " + (r.getInt(3) + 1);
            System.out.println(str);
            query = stmt2.executeQuery(str);
            System.out.println(query.getString(1));}

似乎生成了正确的查询,但不会执行.有什么原因吗?顺便说一句,查询"被声明为结果集.谢谢

The right query seems to be generated, but just won't execute. Is there a reason why this is so. BTW "query" is declared as resultset. Thanks

推荐答案

你只能在某一时刻针对一个数据库连接执行一个语句——因此你可以打开另一个数据库连接并执行第二个语句第二个连接,或遍历第一个语句的结果集并存储员工数据库 ID(例如在数组/集合中),然后关闭该语句并运行第二个,这次从您保存它们的数组/集合中检索 ID.

you can only have one statement executing at one moment in time against one database connection -- so you can either open another database connection and execute the second statement in the 2nd connection, or iterate through the resultset from first statement and store the employees database id's (e.g. in an array/collection) then close that statement and run the second one, this time retrieving the id's from the array/collection you saved them in.

这篇关于Java 从 resultSet 生成查询并执行新查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆