创建包含数据库值的列表并存储在DTO中

时间:2012-10-26 05:56:09

标签: java database list dto

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

我的DTO存在两个字段,NAME和SURNAME以及getter和setter。 问题在于我带有List的DAO

*请记住我是学生。

这是我到目前为止所做的: 结果,写入这样的文件:[名字姓氏,名字姓氏,名字姓氏,] 我需要它写入文件管道已删除“|”

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;
}

3 个答案:

答案 0 :(得分:3)

我不确定你在编写这个文件的位置或方式,但是在跟踪代码时,我认为这可能是错误的。

在这个WHILE LOOP中:

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列表,然后您可以使用它们写入您的文件。

祝你好运。

答案 1 :(得分:1)

该方法应声明为

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。

答案 2 :(得分: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允许,请考虑使用泛型。