To print data separated by ;
, considering the data search of the database made by the colleague above
After Voce searched the data from your database or another, E has the data in a collection in java (Arraylist), just iterate the array like this:
public void printReport(Date initialDate, Date endDate) {
List<Sale> sales = getSales(initialDate, endDate);//chamando getSales
StringBuilder sb = new StringBuilder();
for(Sales sale: sales){
sb.append(sale.getStoreName() + ";" + sale.getVisa() + ";"+sale.getMaster() + ";" + sale.getDiners() + ";" + sale.getAmex() + ";" + sale.getTotal());
}
}
System.out.println(sb.toString());
can get even easier if you overwrite the toString method of the Sales class
@override
public String toString(){
return storeName+";"+visa+";"+master+";"+dinners+";"+amex+";"+total;
}
So no for Voce would do:
for(Sales sale: sales){
sb.append(sale.toString());
}
Edit: Consider the Store object as an attribute of the Sale class (Code snippets of other answers in this question)
Store class.
public class Store{
private int id;
private String name;
//gets and sets
}
Sale class
public class Sale {
//por questões didáticas coloquei como String a maioria.
private Store store;
private String visa ;
private String master ;
private String diners;
private String amex ;
private Double total;
//getts e setts
}
An example of Select that would bring the BD data
private static final String SALES_BY_DATES_SQL = ""
+ "SELECT store.name store_name, store.id store_id, visa, master, diners, amex "
+ "FROM Sale, Store "
+ "WHERE date >= ? AND date <= ?"
+ " and sale.store_id = store.id";
Method retrieving data from BD
public List<Sale> getSales(Date startDate, Date endDate) {
try (
Connection c = params.conectar();
PreparedStatement ps = c.prepareStatement(SALES_BY_DATES_SQL);
)
{
ps.setDate(1, startDate);
ps.setDate(2, endDate);
try (ResultSet rs = ps.executeQuery()) {
List<Sale> sales = new ArrayList<>();
while (rs.hasNext()) {
Sale s = new Sale(
//carregando o objeto store.
Store store = new Store();
store.setId(rs.getInt("store_id"));
store.setName(rs.getString("store_name"));
sale.setStore(store);
rs.getInt("visa"),
rs.getInt("master"),
rs.getInt("diners"),
rs.getInt("amex"));
sales.add(s);
}
return sales;
}
}
}
Then to create the layout:
for(Sales sale: sales){
sb.append(sale.getStore().getName() + ";" + sale.getVisa() + ";"+sale.getMaster() + ";" + sale.getDiners() + ";" + sale.getAmex() + ";" + sale.getTotal());
}
or:
@override
public String toString(){
return getStore().getName()+";"+visa+";"+master+";"+dinners+";"+amex+";"+total;
}
Edit: Whereas the data is already coming right from the database:
method that formats the print:
public StringBuilder formatarLayout(ArrayList<Sales> sales){
StirngBuilder sb = new StringBuilder();
for(Sales sale: sales){
sb.append(sale.getStore().getName() + ";" + sale.getVisa() + ";"+sale.getMaster() + ";" + sale.getDiners() + ";" + sale.getAmex() + ";" + sale.getTotal());
}
return sb;
}
printReport method
public void printReport(Date initialDate, Date endDate) {
List<Sale> sales = getSales(initialDate, endDate);
//isso ja retorna seus dados formatados
String layout = formatarLayout(sales).toString();
// agora pode fazer o que quiser com eles
}
Recommended reading 1: How Try-with-Resources works?
– Victor Stafusa
Recommended reading 2: Error storing data from a Select with Java
– Victor Stafusa
Recommended reading 3: How to migrate from Date and Calendar to the new Java 8 Date API?
– Victor Stafusa