からリスト<>結果を印刷するにはどうすればコードデータベース
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));
}
があるが、私はこの
[email protected] ような結果を得ます[email protected] [email protected]
はどのように印刷したデータを取得しますか?
ここで私の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;
}
はCustomer' ''ためtoString'メソッドを定義します。 – Makoto
印刷する顧客のすべての要素は? – SpringLearner
私は何をすべきかデータから名前とIDを取得したい –