Java ResultSet 如何把它放入一个ArrayList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26127305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ResultSet how to put it into a ArrayList
提问by Adem ?kmen
Is "ResultSet" considered to be an ArrayList? I'm talking about jdbc. If no, then how do I put the information i get from my DB using the
“ResultSet”是否被认为是一个 ArrayList?我说的是 jdbc。如果不是,那么我如何使用从我的数据库中获得的信息
while (result.next()) {
....
}
syntax into an ArrayList called something like hotelResult?
语法转换成一个 ArrayList,称为类似 hotelResult 的东西?
I hope it was understandable.
我希望这是可以理解的。
采纳答案by Bart1612
No, ResultSet is not considered an ArrayList but rather a table. If hotelResult for example has the type of String you can fill the list with this piece of code(if the column from the ResultSet is a String).
不,ResultSet 不被认为是一个 ArrayList,而是一个表。例如,如果hotelResult 具有String 类型,您可以使用这段代码填充列表(如果ResultSet 中的列是String)。
while(result.next()) {
hotelResult.add(result.getString("Enter the columnname here");
}
For each datatype there is a method to get the value from the ResultSet. Look in the Java API for the different kinds of methods.
对于每种数据类型,都有一种从 ResultSet 中获取值的方法。在 Java API 中查找不同类型的方法。
回答by Michael M
A ResultSet is not an ArrayList. Rather, it is a special object (Interface) to hold data retrieved by queries via JDBC connections.
ResultSet 不是 ArrayList。相反,它是一个特殊的对象(接口),用于保存通过 JDBC 连接查询检索到的数据。
A ResultSet object cannot be updated, and can only be traversed forward... not back. By default, you can only iterate through it once, from the first row to the last (though with a bit of coding, you can generate a ResultSet object that can be edited and traversed bi-directionally).
ResultSet 对象无法更新,只能向前遍历……不能向后遍历。默认情况下,您只能遍历它一次,从第一行到最后一行(尽管通过一些编码,您可以生成一个可以双向编辑和遍历的 ResultSet 对象)。
The records stored within a ResultSet object can easily be placed within an ArrayList. Here is an example on how you can do this:
存储在 ResultSet 对象中的记录可以轻松地放置在 ArrayList 中。以下是有关如何执行此操作的示例:
Connection con = ... ;
Statement stmt = ... ;
ResultSet results = stmt.executeQuery("...");
//Stores properties of a ResultSet object, including column count
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
ArrayList<String> hotelResultList = new ArrayList<>(columnCount);
while (results.next()) {
int i = 1;
while(i <= columnCount) {
hotelResultList.add(results.getString(i++));
}
}
NOTE: This example assumes a single String being returned in the query, such as a Hotel name. You will likely want to hold multiple pieces of data about each hotel, in which case you would create a "Hotel" object, and then create the ArrayList as a List of Hotel objects. Using a rowmapper, each hotel object can be populated with the associated data.
注意:此示例假定查询中返回单个字符串,例如酒店名称。您可能希望保存有关每家酒店的多条数据,在这种情况下,您将创建一个“Hotel”对象,然后将 ArrayList 创建为一个 Hotel 对象列表。使用行映射器,每个酒店对象都可以填充相关数据。
In addition, using one of the popular JDBC frameworks to handle JDBC connections, queries, and result sets can simplify the process further.
此外,使用流行的 JDBC 框架之一来处理 JDBC 连接、查询和结果集可以进一步简化流程。
回答by Bart1612
I will help u out :)! Create the needed variables in the class see my example :)
我会帮你的:)!在类中创建所需的变量,请参阅我的示例 :)
public class HotelData {
private String hotelName = null;
private int hotelTelephone = 0;
public HotelData(String hotelName, int hotelTelephone) {
this.hotelName = hotelName;
this.hotelTelephone = hotelTelephone;
}
}
Now create the ArrayList:
现在创建 ArrayList:
public ArrayList<HotelData> hotelResult = new ArrayList<HotelData>();
With the while method now:
现在使用 while 方法:
while(result.next()) {
hotelResult.add(new HotelData(result.getString("Enter columnname"), result.getInt("Enter colummname")));
}
Hope this will help u buddy :)! If u need to get the data from the ArrayList u can simply write ur own get methods in the HotelData class!
希望这会帮助你朋友:)!如果您需要从 ArrayList 获取数据,您可以简单地在 HotelData 类中编写您自己的获取方法!
回答by Bart1612
To answer your first question, u don't really need the HotelData class. The only thing this class does is holding the data nice and clean in one Object (for each specific hotel).
要回答您的第一个问题,您实际上并不需要 HotelData 类。这个类所做的唯一一件事就是将数据保存在一个对象中(对于每个特定的酒店)。
If you implement it your way, you must cast all the values to Strings first(if they do not contain String values) before u can store your items in the hotelInfo list. This is because the hotelInfo list has the String type, in my implementation this is also not needed(the casts) because i have created a constructor with one String value and one int value.
如果您按照自己的方式实现它,则必须先将所有值转换为字符串(如果它们不包含字符串值),然后才能将您的项目存储在 hotelInfo 列表中。这是因为hotelInfo 列表具有String 类型,在我的实现中也不需要(强制转换),因为我创建了一个具有一个String 值和一个int 值的构造函数。
If u want your example to work implement it like this:
如果您希望您的示例能够像这样实现它:
not this: hotelInfo.add(result.getString("hotelNo"));
不是这个:hotelInfo.add(result.getString("hotelNo"));
but like this: hotelInfo.add("" + result.getInt("hotelNo")); //Notice the cast here!
但就像这样:hotelInfo.add("" + result.getInt("hotelNo")); //注意这里的演员表!
回答by Vikram B
If you're asking how I can get the database values which is added in my list .Here is the solution below
如果您问我如何获取添加到我的列表中的数据库值。这是下面的解决方案
pom.xml file add below snippet of dependency
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>18.0</version> </dependency>
call the method from the main program .
List output = readRows(rs);
here
rs
is theResultSet
object,readRows()
is the methodbelow is the code snippet for the
readRows()
methodprivate static List<List<Object>> readRows(ResultSet rs) throws SQLException { ImmutableList.Builder<List<Object>> rows = ImmutableList.builder(); int columnCount = rs.getMetaData().getColumnCount(); while (rs.next()) { List<Object> row = new ArrayList<>(); for (int i = 1; i <= columnCount; i++) { row.add(rs.getObject(i)); } rows.add(row); } return rows.build(); }
pom.xml 文件添加下面的依赖片段
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>18.0</version> </dependency>
从主程序调用该方法。
List output = readRows(rs);
这
rs
是ResultSet
对象,readRows()
是方法下面是该
readRows()
方法的代码片段private static List<List<Object>> readRows(ResultSet rs) throws SQLException { ImmutableList.Builder<List<Object>> rows = ImmutableList.builder(); int columnCount = rs.getMetaData().getColumnCount(); while (rs.next()) { List<Object> row = new ArrayList<>(); for (int i = 1; i <= columnCount; i++) { row.add(rs.getObject(i)); } rows.add(row); } return rows.build(); }
回答by Ahmed Emad
i believe that will clear- ArrayList hotels holds objects of HotelDtos
我相信会清除 - ArrayList 酒店持有 HotelDtos 的对象
public class HotelDto {
private String hotelName;
private String hotelAddress;
private int hotelRank;
public String getHotelName() {
return hotelName;
}
public void setHotelName(String hotelName) {
this.hotelName = hotelName;
}
public String getHotelAddress() {
return hotelAddress;
}
public void setHotelAddress(String hotelAddress) {
this.hotelAddress = hotelAddress;
}
public int getHotelRank() {
return hotelRank;
}
public void setHotelRank(int hotelRank) {
this.hotelRank = hotelRank;
}
}
}
public class HotelDao {
public List<HotelDto> getHotlInfo(String hotelName) {
List<HotelDto> hotels = new ArrayList<HotelDto>();
try {
String query = "SELECT hotelName, hotelAddress,hotelrank " + "FROM HOTELS_TABLE "
+ "WHERE hotelName = " + "'" + hotelName + "'" + " ";
ResultSet resultSet = DBConnection.getDBConnection().createStatement().executeQuery(query);
int i = 0;
while (resultSet.next()) {
HotelDto hDto = new HotelDto();
hDto.setHotelName(resultSet.getString(1));
hDto.setHotelAddress(resultSet.getString(2));
hDto.setHotelrank(resultSet.getInt(3));
hotels.add(i, hDto);
i++;
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
return hotels;
}
}
}
}