I am fetching data from database in method BillnAmountFetch
and getting value in main method I want to assign fetched velue to arrayList ar
.
My code to fetch data from database
public List<Object[]> BillnAmountFetch(long cid) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
List<Object[]> obj = null;
try {
String hql = "select count(billNo), sum(total), invoiceDate from BillDetails "
+ "where client.id=:cid "
+ "group by invoiceDate "
+ "order by invoiceDate DESC";
Query query = session.createQuery(hql);
query.setParameter("cid", cid);
obj = query.list();
tx.commit();
} catch (HibernateException e) {
if (tx != null) {
e.printStackTrace();
tx.rollback();
}
} finally {
session.close();
}
return obj;
}
Following is my code to print data received from above method
public static void main(String[] args) {
BillDAO bdo = new BillDAO();
List<Object[]> lst = bdo.BillnAmountFetch(1);
BillDetails bd = new BillDetails();
ArrayList<BillDetails> ar = new ArrayList<BillDetails>();
Object[] count = lst.get(0);
Object[] amount = lst.get(1);
Object[] invoice_dts = lst.get(2);
System.out.println("-----------Total Bills---------- ");
for (int x = 0; x < count.length; x++) {
System.out.println("Total bills " + count[x]);
}
System.out.println("-------Total Amount--------- ");
for (int x = 0; x < amount.length; x++) {
System.out.println("Amount " + amount[x]);
}
System.out.println("----------Total Invoice date---------- ");
for (int x = 0; x < invoice_dts.length; x++) {
System.out.println("dates " + invoice_dts[x]);
}
}
Output of the above program is
-----------Total Bills----------
Total bills 3
Total bills 7281.00
Total bills 2014-07-15
-------Total Amount---------
Amount 7
Amount 14841.00
Amount 2014-07-12
----------Total Invoice date----------
dates 3
dates 1294.00
dates 2014-07-11
BillDetails.java
public class BillDetails implements java.io.Serializable {
private Date invoiceDate;
private long totalBills;
private BigDecimal totalAmount;
//getter and setter
}
How to properly assing values to ArrayList
object ar
EDIT: If not able to assign in ar then can we assign all count to one array or list similar for others. I have to display values in jsp page.
I am fetching data from database in method
BillnAmountFetch
and getting value in main method I want to assign fetched velue toarrayList ar
.
You can't, you'll have to do the copy yourself and cast/convert the elements appropriately.
Note that BillnAmountFetch
returns List<Object[]>
(a list of object arrays ), and you're saying you want to assign it to ArrayList<BillDetails>
.
So there are three major issues there:
A List<Object[]>
is allowed to contain Object[]
instances, but an ArrayList<BillDetails>
is not allowed to contain Object[]
instances; the items in the list are required to be BillDetails
instances.
List<Object[]>
could be any kind of list ( LinkedList
, Stack
), it doesn't have to be an ArrayList
, but ar
is declared as an ArrayList
(specifically).
It seems unlikely that BillDetails
is assignment-compatible with Object[]
(an array of objects).
Apart from TJ Crowders suggestion you can map your model entity as Hibernate entity like below:
@Entity
public class BillDetails implements java.io.Serializable {
// some other related annotations
private Date invoiceDate;
private long totalBills;
private BigDecimal totalAmount;
//getter and setter
}
then you can get that automatic binding of ArrayList< BillDetails >
by Hibernate, when you do this :
Query query = session.createQuery(hql , BillDetails.Class);
you need to search furthermore about it though:
I got the solution. I may not be able to express my question properly. I want Scalar results . What I want is below
public List<BillDetails> BillnAmountFetch(long cid) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
ArrayList<BillDetails> ar = new ArrayList<BillDetails>();
BillDetails bd = null;
try {
String hql = "select count(billNo), sum(total), invoiceDate from BillDetails "
+ "where client.id=:cid "
+ "group by invoiceDate "
+ "order by invoiceDate DESC";
Query query;
query = session.createQuery(hql);
query.setParameter("cid", cid);
Iterator results = query.list().iterator();
while (results.hasNext()) {
Object[] row = (Object[]) results.next();
Long count = (Long) row[0];
BigDecimal amount = (BigDecimal) row[1];
Date dt = (Date) row[2];
System.out.println(count + " " + amount + " " + dt);
bd = new BillDetails();
bd.setTotalBills(count);
bd.setTotalAmount(amount);
bd.setInvoiceDate(dt);
ar.add(bd);
}
System.out.println("--------------------------");
tx.commit();
} catch (HibernateException e) {
if (tx != null) {
e.printStackTrace();
tx.rollback();
}
} finally {
session.close();
}
return ar;
}
public static void main(String[] args) {
BillDAO bdo = new BillDAO();
ArrayList<BillDetails> ar = (ArrayList<BillDetails>) bdo.BillnAmountFetch(1);
System.out.println("In main method");
for(BillDetails b:ar){
System.out.println(b.getTotalBills() + " " + b.getTotalAmount() + " " + b.getInvoiceDate());
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.