如何将MySQL查询结果转换为ArrayList

时间:2023-01-04 07:55:16

I have SQL query which results in multiple columns. I want to execute this query and get the results into my ArrayList<> instead of the ResultSet. My class for the column definitions is

我有SQL查询,结果是多个列。我想执行这个查询,并将结果放入我的ArrayList<>而不是ResultSet中。我的列定义类是

public class Record{
    private String FileName;
    private String FileID;
    private String Loan;
    private String Page;
}

Query is :

查询的方法是:

String query = "SELECT FileName, FileID, loanNumnber, PageNumber FROM table";
ResultSet rs = stmt.executeQuery(query);

I want the results of the query in recordData object.

我想要记录数据对象中的查询结果。

ArrayList<Record> recordData = new ArrayList<Record>;

Please suggest how the arraylist can be populated directly with correct mapping.

请建议如何使用正确的映射直接填充arraylist。

4 个解决方案

#1


3  

Use following code snippet if you want to implement by yourself. It will convert the result set to Record objects and add it to the ArrayList.

如果您想自己实现,请使用以下代码片段。它将把结果集转换为记录对象并将其添加到ArrayList中。

Record record;
while(rs.next()){

     record = new record();

     String fileName  = rs.getString("FileName");
     String fileID = rs.getString("FileID");
     String loanNumnber = rs.getString("loanNumnber");
     String pageNumber = rs.getString("PageNumber");

    record.setFileName(fileName);
    record.setFileID(fileID);
    record.setLoan(loanNumnber);
    record.setPage(pageNumber);

    recordData.add(record)
}

rs.close();

Otherwise, if you want to use any third party frameworks then there are lot of options such as Hibernate, iBatis etc.

否则,如果您想使用任何第三方框架,那么就有许多选项,如Hibernate、iBatis等。

#2


0  

You can use the mybatis. Please use the ORM.

你可以用蜡染。请使用ORM。

https://en.wikipedia.org/wiki/Object-relational_mapping

https://en.wikipedia.org/wiki/Object-relational_mapping

#3


0  

You should use OR mapper like Hibernate. Thanks POJO classes you can do whan you want. Some readings:

您应该使用Hibernate或mapper。感谢POJO课程,你可以做任何你想做的。一些数据:

http://www.tutorialspoint.com/hibernate/hibernate_examples.htm

http://www.tutorialspoint.com/hibernate/hibernate_examples.htm

http://www.mkyong.com/tutorials/hibernate-tutorials/

http://www.mkyong.com/tutorials/hibernate-tutorials/

#4


0  

Add parenthesis "()" to this line in your code so that it looks like this:

在代码中添加括号“()”,使其看起来如下:

ArrayList<Record> recordData = new ArrayList<Record>();

ArrayList recordData = new ArrayList ();

#1


3  

Use following code snippet if you want to implement by yourself. It will convert the result set to Record objects and add it to the ArrayList.

如果您想自己实现,请使用以下代码片段。它将把结果集转换为记录对象并将其添加到ArrayList中。

Record record;
while(rs.next()){

     record = new record();

     String fileName  = rs.getString("FileName");
     String fileID = rs.getString("FileID");
     String loanNumnber = rs.getString("loanNumnber");
     String pageNumber = rs.getString("PageNumber");

    record.setFileName(fileName);
    record.setFileID(fileID);
    record.setLoan(loanNumnber);
    record.setPage(pageNumber);

    recordData.add(record)
}

rs.close();

Otherwise, if you want to use any third party frameworks then there are lot of options such as Hibernate, iBatis etc.

否则,如果您想使用任何第三方框架,那么就有许多选项,如Hibernate、iBatis等。

#2


0  

You can use the mybatis. Please use the ORM.

你可以用蜡染。请使用ORM。

https://en.wikipedia.org/wiki/Object-relational_mapping

https://en.wikipedia.org/wiki/Object-relational_mapping

#3


0  

You should use OR mapper like Hibernate. Thanks POJO classes you can do whan you want. Some readings:

您应该使用Hibernate或mapper。感谢POJO课程,你可以做任何你想做的。一些数据:

http://www.tutorialspoint.com/hibernate/hibernate_examples.htm

http://www.tutorialspoint.com/hibernate/hibernate_examples.htm

http://www.mkyong.com/tutorials/hibernate-tutorials/

http://www.mkyong.com/tutorials/hibernate-tutorials/

#4


0  

Add parenthesis "()" to this line in your code so that it looks like this:

在代码中添加括号“()”,使其看起来如下:

ArrayList<Record> recordData = new ArrayList<Record>();

ArrayList recordData = new ArrayList ();