【问题标题】:Creating a list with database values and store in DTO创建一个包含数据库值的列表并存储在 DTO 中
【发布时间】:2012-10-26 05:56:09
【问题描述】:

我有一个主类、一个 DTO 和一个 DAO。 我想要做的是读取数据库表 CUSTOMER(两个字段 NAME,SURNAME),然后将其写入 txt 文件。我似乎无法让它将值存储在 DTO 中,以便我的主类可以获取它。

我的 DTO 存在两个字段,NAME 和 SURNAME 以及 getter 和 setter。 问题在于我的 DAO 与列表

*请记住我是学生。

这是我到目前为止所做的: 结果,像这样写入文件:[name surname, name surname, name surname,] 我需要它写入以“|”分隔的文件管道

public CustomerDTO getDetails(Connection conn) throws SQLException {

    CustomerDTO customerDTO = new CustomerDTO();
    ResultSet rs = null;
    PreparedStatement pstmnt = null;

    try {
        // SELECT NAME, SURNAME FROM CUSTOMER
        StringBuffer stringBuffer = new StringBuffer("SELECT " + DBConstants.CUST_NAME + ", ");
        stringBuffer.append(DBConstants.CUST_SURNAME);
        stringBuffer.append(" FROM " + "CUSTOMER");
        //build string
        String query = stringBuffer.toString();
        //prepared statement
        pstmnt = conn.prepareStatement(query);
        //execute preparestatement
        rs = pstmnt.executeQuery();
        //keep reading next line in table - .next() method means next line
        List myList = new ArrayList();

        while (rs.next()) {
            String ss = rs.getString("NAME");
            String sss = rs.getString("SURNAME");
            myList.add(ss + " " + sss);
            customerDTO.setName(myList.toString());
            customerDTO.setSurname(myList.toString());
        }
    } catch (Exception e) {
        e.getMessage();
    }
    rs.close();
    //return DTO with details.
    return customerDTO;
}

【问题讨论】:

  • 您的方法返回单个 DTO,而不是返回列表。您应该更改方法的签名,在每次迭代时创建一个新的 CustomerDTO,并将其添加到列表中,然后返回该列表。
  • 查看我的回答,其中包含更多详细信息。

标签: java database list dto


【解决方案1】:

我不确定您在哪里或如何编写此文件,但在跟踪代码时,我认为这可能是错误的。

在这个 WHILE 循环中:

while (rs.next()) {
    String ss = rs.getString("NAME");
    String sss = rs.getString("SURNAME");

    // You'r adding an element here that would have a value like this: "John Doe"
    myList.add(ss + " " + sss);


    // Single instance of a DTO whose value. State values will always be replaced with the
    // string represenation of the myList variable.
    //
    // Both properties will always contain the same value no matter what.
    customerDTO.setName(myList.toString());
    customerDTO.setSurname(myList.toString());
}

我认为你真正应该做的是这样的:

// Notice the use of Generics (Look it up). It is more compile time safe and better practice.
ArrayList<CustomerDTO> customerDTOS = new ArrayList<CustomerDTO>();
while (rs.next()) {
    String ss = rs.getString("NAME");
    String sss = rs.getString("SURNAME");

    CustomerDTO customerDTO = new CustomerDTO();
    customerDTO.setName(ss);
    customerDTO.setSurName(sss);

    customerDTOS.Add(customerDTO);
}

return customerDTOS;

简而言之,您的方法应该返回一个CustomerDTOs 列表,然后您可以使用它们写入您的文件。

祝你好运。

【讨论】:

    【解决方案2】:

    方法应该声明为

    public List<CustomerDTO> getDetails(Connection conn) throws SQLException {
        ...
    }
    

    因为它的目标是返回一个客户列表(表格中每行一个)。

    在方法内部,你应该有以下内容:

    // create the result list, initially empty
    List<CustomerDTO> result = new ArrayList<CustomerDTO>();
    
    // iterate through the rows
    while (rs.next()) {
    
        // TODO: create a new CustomerDTO, and populate it with the row's data
        // TODO: add this DTO to the result list
    }
    
    return result;
    

    然后,此方法的调用者将遍历 CustomerDTO 列表,并将它们写入文件。每个方法都有其职责:DAO 处理数据库检索,但不处理文件 IO。

    【讨论】:

      【解决方案3】:

      这是一个完整的例子:

      public void caller() {
          Connection conn = null;
          // TODO: setup connection
          List list = getDetails(conn);
          for (int i=0; i<list.size(); i++) {
            CustomerDTO dto = list.get(i);
            write(dto.getName(), dto.getSurname());
          }
      }
      
      public List getDetails(Connection conn) throws SQLException {
      
          List myList = new ArrayList();
          ResultSet rs = null;
          PreparedStatement pstmnt = null;
      
          try {
              // SELECT NAME, SURNAME FROM CUSTOMER
              StringBuffer stringBuffer = new StringBuffer("SELECT " + DBConstants.CUST_NAME     + ", ");
              stringBuffer.append(DBConstants.CUST_SURNAME);
              stringBuffer.append(" FROM " + "CUSTOMER");
              //build string
              String query = stringBuffer.toString();
              //prepared statement
              pstmnt = conn.prepareStatement(query);
              //execute preparestatement
              rs = pstmnt.executeQuery();
              //keep reading next line in table - .next() method means next line
      
              while (rs.next()) {
                  String ss = rs.getString("NAME");
                  String sss = rs.getString("SURNAME");
                  CustomerDTO customerDTO = new CustomerDTO();
                  customerDTO.setName(ss);
                  customerDTO.setSurname(sss);
                  myList.add(customerDTO);
              }
          } catch (Exception e) {
              e.getMessage();
          }
          rs.close();
          //return list of DTOs.
          return myList;
      }
      

      我把 write 方法的实现留给你。 还。如果您的 java 允许,请考虑使用泛型。

      【讨论】:

        猜你喜欢
        • 2019-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-05
        • 1970-01-01
        • 2014-07-29
        • 1970-01-01
        相关资源
        最近更新 更多