如何打印列表<>数据库的结果

时间:2016-04-18 05:17:14

标签: java list

我有代码

        Customer customerWithId10 = customerDao.getCustomerById(1);

        List<Customer> customerFirst10Rows = customerDao.getCustomers(0, 10);
       for(int i=0;i<customerFirst10Rows.size();i++){
           System.out.println(customerFirst10Rows.get(i));
       }

但我得到这样的结果

  

co.id.ipb.ilkom.training.model.Customer@41140b9   co.id.ipb.ilkom.training.model.Customer@41140b9   co.id.ipb.ilkom.training.model.Customer@41140b9

如何打印和获取数据?

这里是我的CustomerDao

public class CustomerDaoMysql implements CustomerDao{
 private static final String SELECT_PAGING_QUERY
        = "SELECT * FROM CUSTOMER LIMIT ?,?";
  private static final String SELECT_BY_ID_QUERY
        = "SELECT * FROM CUSTOMER WHERE ID=?";
 private Connection connection;

public CustomerDaoMysql(Connection connection) {
    this.connection = connection;
}
@Override
   public List<Customer> getCustomers(Integer indexStart, Integer numOfRows) {
    try {
        PreparedStatement selectWithPagingPreparedStatement
                = connection.prepareStatement(SELECT_PAGING_QUERY);
        selectWithPagingPreparedStatement.setInt(1, indexStart);
        selectWithPagingPreparedStatement.setInt(2, numOfRows);
        ResultSet customerResultSet = selectWithPagingPreparedStatement.executeQuery();
        List<Customer> customers = new ArrayList<>();
        while (customerResultSet.next()) {
            customers.add(extractCustomerFromResultSet(customerResultSet));
        }
        return customers;
    } catch (SQLException ex) {
        Logger.getLogger(CustomerDaoMysql.class.getName()).log(Level.SEVERE, null, ex);
    }
    return Collections.emptyList();
}

public Customer extractCustomerFromResultSet(ResultSet customerResultSet) throws SQLException {
    Customer customer = new Customer();
    customer.setId(customerResultSet.getInt("ID"));
    customer.setName(customerResultSet.getString("NAME"));
    customer.setEmail(customerResultSet.getString("EMAIL"));
    customer.setAddress(customerResultSet.getString("ADDRESS"));
    Date birthDate = customerResultSet.getDate("BIRTH_DATE");
    customer.setBirthDate(new java.util.Date(birthDate.getTime()));
    return customer;

}

2 个答案:

答案 0 :(得分:0)

List<Customer> customerFirst10Rows = customerDao.getCustomers(0, 10);
  

这只是意味着您将收到一份可能是10英寸的客户列表   你的情况。

在该列表中,每个元素都是Customer类型的对象,因此,System.out.println(customerFirst10Rows.get(i)); 打印toString()方法的内容

如果您已为Customer类中使用的字段定义了getter,则可以在此处调用它们,就像您有一个代表客户名称的字段“name”

然后你应该使用 System.out.println(customerFirst10Rows.get(i).getName())获取价值 Simillarly适用于所有领域

否则,您可以为您的Customer类覆盖toString()方法。您想要打印值的方式

答案 1 :(得分:0)

只需覆盖Customer数据类中的<%= select_tag("id", options_for_select(list), data: {url: your_path, remote: true, method: :post_or_get}, prompt: "please...") %> 方法即可。通过这种方式,您可以在每次外出操作中打印您的Customer对象。相关代码如下:

toString()

覆盖@Override public String toString() { return getName() + " , "+getSurname() + " , "+getAdress(); } 方法后,您的输出将是:

toString()