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