2012-10-26 69 views
1

我有一個主類,一個DTO和一個DAO。 我想要做的是讀取數據庫表CUSTOMER(兩個字段NAME,SURNAME),然後將其寫入一個txt文件。我似乎無法將它存儲在DTO中,這樣我的主類就可以得到它。使用數據庫值創建列表並存儲在DTO中

我的DTO存在兩個字段,NAME和SURNAME與getter和setter。 問題在於我的DAO與列表

*請記住我是一名學生。

這是我迄今所做的: 結果,寫入文件中像這樣:[名字姓,名的姓,名的姓,] 我需要它寫入deliminated文件管道「|」

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

您的方法返回一個DTO,而不是返回一個列表。您應該更改方法的簽名,在每次迭代中創建一個新的CustomerDTO,並將其添加到列表中,然後返回該列表。 –

+0

即時消息不知道這是什麼意思, – user1759247

+0

看到我的答案,這進入更多的細節。 –

回答

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列表,然後你可以使用它們寫入您的文件。

祝你好運。

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; 

然後,該方法的調用者將通過CustomerDTOs列表循環,並將其寫入文件。每種方法都有其責任:DAO處理數據庫檢索,但不處理文件IO。

0

這裏有一個完整的例子:我離開write方法給你的執行

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

。 另外。考慮使用泛型,如果你的Java允許的話。

相關問題