0

i have code

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

But i get result like this

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

How to print and get the data?

here my 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 Answers2

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

This simply means you are going to receive a list of Customers may be 10 in your case.

In that list each element is an object of type Customer and Therefore, System.out.println(customerFirst10Rows.get(i)); prints the contents of the toString() method

If you have defined getters for the fields used in your Customer class you could call them here like you have a field "name" which represents the customer name

Then you should use System.out.println(customerFirst10Rows.get(i).getName()) to get the value Simillarly for all the fields

Else you could overide the toString() method for you Customer class. The way you want to print you values

Yati Sawhney
  • 1,372
  • 1
  • 12
  • 19
0

Just override toString() method in your Customer data class. By this way you can pretty print your Customer object in every out operation. Related code is like below:

@Override
public String toString() {
    return getName() + " , "+getSurname() + " , "+getAdress();
}

After you override toString() method, your outputs will be like that:

Lionel , Messi , Barcelona
Cristiano , Ronaldo , Madrid
Raptor
  • 187
  • 1
  • 2
  • 11