1

I am a novice to use jdbc and I have some problems.

I use hql to search data in MySQL, and the result is Query type. I don't know how to get the data from the "Query".This is my code:

final String hql = "select app.appkey,app.type from " + getClassName() +
 "app where app.appkey<>'no-appkey' group by app.type";
Query query = getEntityManager().createQuery(hql);

Thanks a lot.

Dherik
  • 17,757
  • 11
  • 115
  • 164
Shuitian Wei
  • 81
  • 11

2 Answers2

1

You have to do the following:

final String hql = "select app.appkey,app.type from " + getClassName() + " app where app.appkey<>'no-appkey' group by app.type";
Query query = getEntityManager().createQuery(hql);
query.list(); //or query.getSingleResult();

query.list() will give a list of results.

query.getSingleResult() will give you a object.

You can check this.

DMC19
  • 825
  • 2
  • 14
  • 33
  • Thanks! I use this method and get the object. But how can I get data in the object? This is my code: ```for (RequestLog list:lists){ System.out.println(list.getAppKey()); System.out.println(list.getType()); }``` It is wrong, exception is "can not cast Object to RequestLog(RequestLog is the JavaBean)". – Shuitian Wei Apr 06 '18 at 15:45
0

If you are expecting a list of results, so:

List<Object[]> results = query.getResultList();

If you are expect one single result:

Object[] result = query.getSingleResult(); // if more than one result was found, this method will throw a NonUniqueResultException

If column information will be stored in a position of the Object array. Example:

String appKey = (String) result[0];
String appType = (String) result[1];

But work with Object array is not good. Try to use Dto, like explained here.

Dherik
  • 17,757
  • 11
  • 115
  • 164