Java 如何循环内部联接

Java 如何循环内部联接,java,mysql,sql,jsp,servlets,Java,Mysql,Sql,Jsp,Servlets,这个论坛上的一些用户帮助我创建了这个内部连接 SELECT a.*, p.* FROM artist a INNER JOIN pics p on p.id_artist = a.id 要查询这些表 artist pics ------------------- --------------------------------------- | id | name | age | | id_artist |

这个论坛上的一些用户帮助我创建了这个内部连接

SELECT a.*, p.* FROM artist a INNER JOIN pics p on p.id_artist = a.id 
要查询这些表

      artist                          pics
-------------------       ---------------------------------------
| id | name | age |       | id_artist | picUrl | picDescription |
-------------------       ---------------------------------------
   |                          /\
   ----------------------------
现在我在循环结果集时得到的结果是:

1    michael jackson    50    img/435432.jpg    concert
1    michael jackson    50    img/213234.jpg    family
1    michael jackson    50    img/123163.jpg    concert
我想“得到”的是:

但我不明白它是怎么循环的

我必须将查询结果放入许多名为Artist的对象中。这些是它的领域

int id;
String name;
int age;
Pic pics [];
这是图片中的对象

int idArtist;
String Url;
String Description
所以我应该做的是:

while(resulset.next()){
   Artist artist = new Artist();
   artist.setId(resulset.getInt("a.id"));
   ........
   Pic pic = new Pic();
   pic.setIdArtist(resulset.getInt("a.id"));
   pic.setUrl(resulset.getInt("p.picUrl"));
   .....
   artist.setPic(pic );
}

但这是不正确的,因为这样我可以为数据库中的每个图片获得一个艺术家对象。我想要的只是一个艺术家对象和艺术家对象内的许多Pic对象

如果您想使用
JSP
html
(表)打印它,您可以循环使用
resultset
html
属性,如
colspan
rowsspan
将帮助您在
html
中生成所需的输出,如果您只想使用sql,我认为只需编写一个pl/sql过程就可以做到这一点。首先,它将把您的查询结果保存在一个数组中,然后您可以使用它以您想要的格式自由打印它们

DECLARE
cursor cursor1 
SELECT a.*, p.* FROM artist a INNER JOIN pics p on p.id_artist = a.id;
artist_rec  cursor1%rowtype;  --structure of one row
Type artist_array IS VARRAY(25) OF cursor1%rowtype -- array structure for holding returned select query
artist_arr artist_array; -- our array variable
i   NUMBER:=1;
BEGIN
    artist_arr  := artist_array; -- initialize array
    Open cursor1;
LOOP
    FETCH cursor1 into artist_rec  
    artist_arr .EXTEND(1); -- add another element to array with extend it
    artist_arr  (i) := artist_rec ; -- assign fetched row to array
    i := i + 1;  -- increment element count
END LOOP;
CLOSE cursor1;
FOR j IN artist_arr.FIRST.. artist_arr.LAST LOOP
    DBMS_OUTPUT_PUTLINE('') -- You can loop in result now, left is about output formats
                            -- and also you must hold a list of printed artist to avoid
                            -- not to write again


END LOOP;
END;

希望您能获得进一步执行以下代码的想法/方向

String myCurrentID = "";
String strPreviousID = "";

while (moreRecordsAvailable)
{
    myCurrentID = resulset.getString("ID");
    if (!myCurrentID.equals(strPreviousID ))
    {
        // If the previous and current are different, then add both artist and picture objects
         strPreviousID =        myCurrentID ;
         Artist artist = new Artist();
         artist.setId(resulset.getString("a.id"));
         ........
         Pic pic = new Pic();
         pic.setIdArtist(resulset.getString("a.id"));
         pic.setUrl(resulset.getInt("p.picUrl"));
         .....
        artist.setPic(pic );
    }
    else
    {
        // If previous and current is same, then add only pictures object. Need not add artist object here.
        Pic pic = new Pic();
        pic.setIdArtist(resulset.getString("a.id"));
        pic.setUrl(resulset.getInt("p.picUrl"));
    }

}

维护一个
列表
并覆盖
Artist#equals
Artist#hashCode
以仅授权给艺人姓名。然后使用
List#contains
查看是否需要添加新的或使用现有的。最后,还要制作
pics
a
List
,这样您就可以动态地将
Pic
s添加到您的
Artist
中。这是关于代码设计的选择,而不是SQL输出。

不清楚您想要什么???什么循环?您可能应该跟踪最后一行打印的内容,如果与当前内容相同,则跳过它。到目前为止,您的代码是什么?这与如何循环代码无关,而是与如何打印代码有关。你现在怎么打印呢?正如@ScottHunter所说,这完全取决于你如何打印东西。如果你用java打印,那么显示一些代码。我编辑了这篇文章。谢谢你的帮助
String myCurrentID = "";
String strPreviousID = "";

while (moreRecordsAvailable)
{
    myCurrentID = resulset.getString("ID");
    if (!myCurrentID.equals(strPreviousID ))
    {
        // If the previous and current are different, then add both artist and picture objects
         strPreviousID =        myCurrentID ;
         Artist artist = new Artist();
         artist.setId(resulset.getString("a.id"));
         ........
         Pic pic = new Pic();
         pic.setIdArtist(resulset.getString("a.id"));
         pic.setUrl(resulset.getInt("p.picUrl"));
         .....
        artist.setPic(pic );
    }
    else
    {
        // If previous and current is same, then add only pictures object. Need not add artist object here.
        Pic pic = new Pic();
        pic.setIdArtist(resulset.getString("a.id"));
        pic.setUrl(resulset.getInt("p.picUrl"));
    }

}