Java with SQL:如何使用ResultSet

时间:2020-12-21 11:53:20

I have for example the following code line

我有以下代码行

ResultSet rs = stmt.executeQuery("SELECT * FROM item WHERE itemID='"+ X +"'");

if item table scheme is: ItemID,itemName,Count,location

如果项表方案是:ItemID,itemName,Count,location

how does the row from the table "sit" in the ResultSet? is there a way to take the entire row and push it to the first cell of ArrayList (that is the value i must return and i can not change it, for other queries with multiple line results each line will be in a different cell of the array)?

表中的行如何“坐”在ResultSet中?有没有办法把整行和它推到ArrayList的第一个单元格(这是我必须返回的值,我不能改变它,对于具有多行结果的其他查询,每行将在不同的单元格中阵列)?

1 个解决方案

#1


0  

you can iterate over your resultset using ResultSet.next() and get the row information on every iteration and add it into your array list.

您可以使用ResultSet.next()迭代结果集,并获取每次迭代的行信息并将其添加到数组列表中。

List<YourObj> ref = new ArrayList<>();
ResultSet rs = stmt.executeQuery("SELECT * FROM item WHERE itemID='"+ X +"'");
while(rs.next())
      ref.add(new YourObj(rs.getName(), rs.getWhatever()));
}

Here's the reference from Oracle Trails

以下是Oracle Trails的参考资料

#1


0  

you can iterate over your resultset using ResultSet.next() and get the row information on every iteration and add it into your array list.

您可以使用ResultSet.next()迭代结果集,并获取每次迭代的行信息并将其添加到数组列表中。

List<YourObj> ref = new ArrayList<>();
ResultSet rs = stmt.executeQuery("SELECT * FROM item WHERE itemID='"+ X +"'");
while(rs.next())
      ref.add(new YourObj(rs.getName(), rs.getWhatever()));
}

Here's the reference from Oracle Trails

以下是Oracle Trails的参考资料