2

我有一个主类、一个 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;
}
4

3 回答 3

3

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

在这个 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然后您可以使用它们写入您的文件。

祝你好运。

于 2012-10-26T06:10:00.143 回答
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。

于 2012-10-26T06:11:54.573 回答
0

这是一个完整的例子:

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 允许,请考虑使用泛型。

于 2012-10-26T06:21:35.217 回答