简体   繁体   English

java jdbc访问多个结果集

[英]java jdbc accessing multiple resultsets

I have the following structure:我有以下结构:

List -->List_Participant -->Participant列表 -->List_Participant -->参与者

so a list may contain several participants.I try to read this in java:所以一个列表可能包含几个参与者。我尝试在 java 中阅读:

        stat = con.createStatement();
        ResultSet rs = stat.executeQuery("select * from list;");
        // get the informations about the bracket sheet
        while (rs.next()) {
          string name = rs.getString("Name");
          ResultSet rs2 = stat.executeQuery("select * from List_Participant where name= '"+name+"';"); 

            while (rs2.next()) {
               // get the participants

            }
            rs2.close();
        }
        rs.close();

But this does not work.但这不起作用。 I don't receive an exception nor any other output.我没有收到异常或任何其他输出。 I suggest opening a second resultset will close the first one because since I do the first resultset, store the data in an arraylist and close it and afterwards the second it would work, but that leads to a poor performance because I have to search always in the arraylist.我建议打开第二个结果集将关闭第一个结果集,因为因为我做了第一个结果集,所以将数据存储在一个数组列表中并关闭它,然后第二个它会起作用,但这会导致性能不佳,因为我必须始终在数组列表。

What might be a better solution?什么可能是更好的解决方案?

Edit: Solution is to make a Join, my current try:编辑:解决方案是加入,我目前的尝试:

 select * from List_participant 
INNER JOIN List ON List.name = List_participant.List 
INNER JOIN participant ON List_participant.participant =participant.ROWID;

How do I adress the columns now, since they might have the same name?我现在如何处理这些列,因为它们可能具有相同的名称?

You can try using two different Statement instances for each query.您可以尝试为每个查询使用两个不同的Statement实例。 See the JavaDoc for java.sql.Statement .请参阅java.sql.Statement的 JavaDoc。 The following example shows the principle.下面的例子展示了这个原理。

Statement statement1 = connection.createStatement();
Statement statement2 = connection.createStatement();

ResultSet resultSet1 = statement1.executeQuery("select * from list");
while (resultSet1.next()) {
    String name = resultSet1.getString("Name");
        
    ResultSet resultSet2 = statement2.executeQuery(
        "select * from List_Participant where name= '" + name + "'");
    while (resultSet2.next()) {
        // get the participants
    }
}

BUT: This is not standard usage of JDBC or SQL for good reasons.但是:这不是 JDBC 或 SQL 的标准用法,这是有充分理由的。 It deprives the database of any optimization possibility and moves to much data between the DB and your app for no good reason (See the comments of JohnSkeet and BalusC).它剥夺了数据库的任何优化可能性,并无缘无故地转移到数据库和您的应用程序之间的大量数据(参见 JohnSkeet 和 BalusC 的评论)。

Better use appropriate JOIN s in your one and only statement.最好在您的唯一声明中使用适当的JOIN This can be optimized by the DB:可以通过数据库进行优化:

SELECT lp.* FROM list l JOIN List_Participant lp ON l.name = lp.name

Add any filters/conditions you like to minimize the data retrieved.添加您喜欢的任何过滤器/条件以最小化检索到的数据。

Here is the reason why you can't have two ResultSet opened from the same Statement这就是为什么你不能从同一个Statement打开两个ResultSet的原因

ResultSet javadoc : ResultSet javadoc

A ResultSet object is automatically closed when the Statement object that generated it is closed, re-executed , or used to retrieve the next result from a sequence of multiple results.当生成它的 Statement 对象关闭、重新执行或用于从多个结果序列中检索下一个结果时,ResultSet 对象将自动关闭。

So basicly, a Statement can only give you one ResultSet at a time, so you loose the first result when you execute the second query.所以基本上,一个Statement一次只能给你一个ResultSet ,所以当你执行第二个查询时你会丢失第一个结果。

Solution :解决方案 :

  • Using one instance of Statement per ResultSet needed.每个ResultSet需要使用一个Statement实例。
  • merging the queries to only have one将查询合并为只有一个

Nested query ?嵌套查询? Something like Select * from List_Participant where name in (select name from List);类似于Select * from List_Participant where name in (select name from List); This can be made to work for your third tabel as well.这也适用于您的第三个表。

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

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