java 将数据库结果集解析为对象列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4255721/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:33:03  来源:igfitidea点击:

Parse a Database Result Set into an Object List

javalistjdbcobjectresultset

提问by chrissygormley

I am trying to get all data from a table contained in a database, and parse it into a List. I have tried a few different techniques and can't get it working. I am trying to serialize the ResultSet so I can send it over a socket. The code I have got so far is:

我试图从包含在数据库中的表中获取所有数据,并将其解析为一个列表。我尝试了几种不同的技术,但无法正常工作。我正在尝试序列化 ResultSet 以便我可以通过套接字发送它。到目前为止我得到的代码是:

public Object readJavaObject(String query, Connection con) throws Exception
{        
    PreparedStatement stmt = con.prepareStatement(query);
    //stmt.setString(1, id);
    query_results = stmt.executeQuery();
    while (query_results.next())
    {
        dataObject = query_results.getObject(1);
    }
    query_results.close();
    stmt.close();

    return dataObject;
}

public void run()
{
    try
    {
        Class.forName(dbClass);
        con = DriverManager.getConnection (dbUrl, user, pass);
        query = "SELECT * FROM Bench_table";
        dataList = (List) this.readJavaObject(query, con);
        con.close();
    }
}

I'm not sure what code to write around readJavaObject. Can anyone help to point out where the issue is?

我不确定要写什么代码readJavaObject。谁能帮忙指出问题出在哪里?

Thanks for any help.

谢谢你的帮助。

回答by Bozho

Take a look at DbUtils(from apache-commons).

看看DbUtils(来自 apache-commons)。

If you need a more complex solution for mapping sql results to java objects, take a look at object-relational mapping. In Java the standard is JPA (Java Persistence API), with Hibernate, EclipseLink and OpenJPA as the most famous implementors.

如果您需要更复杂的解决方案来将 sql 结果映射到 java 对象,请查看object-relational mapping。在 Java 中,标准是 JPA(Java Persistence API),其中最著名的实现者是 Hibernate、EclipseLink 和 OpenJPA。

回答by jzd

There are several issues with your code.

您的代码有几个问题。

  1. You loop through the results but only return data from the last row. Instead create a List in the readJavaObject method and add an element each pass through the loop.
  2. Instead of getting the item in the first column, I assume you want all the data. You are going to have to be more specific with which "get" methods you use to pull data off the ResultSet. Possibly create a POJO to represent a row of data, then inside the loop populate it and store it in the list.
  3. Assuming you use a POJO, you might want to use generics with your list to make it easier to get items out of it without having to cast.
  1. 您遍历结果,但只返回最后一行的数据。而是在 readJavaObject 方法中创建一个 List 并在每次循环时添加一个元素。
  2. 我假设您需要所有数据,而不是获取第一列中的项目。您将不得不更具体地使用哪些“get”方法从 ResultSet 中提取数据。可能创建一个 POJO 来表示一行数据,然后在循环内填充它并将其存储在列表中。
  3. 假设您使用 POJO,您可能希望在您的列表中使用泛型,以便更轻松地从列表中获取项目而无需强制转换。

回答by Buhake Sindi

I don't know exactly what you want to achieve, but can I introduce you to the DAO (Data Access Object) Pattern?

我不确切知道您想要实现什么,但我可以向您介绍DAO(数据访问对象)模式吗?

回答by FBB

I recently discovered the CachedRowSet, which does exactly what OP needs. It notablyallows to serialize a ResultSet, and to restore the original when needed.

我最近发现了CachedRowSet,它正是 OP 所需要的。它特别允许序列化 a ResultSet,并在需要时恢复原始文件。