0

I have a query to get the size of databases I have. I have written the following:

@SuppressWarnings("unchecked")
List<Long> results = (List<Long>) em.createQuery("SELECT sum( data_length + index_length ) / 1024 / 1024 \"DataBaseSizeinMB\" FROM information_schema.TABLES GROUP BY table_schema ").getResultList();

where em is the EntityManager. The query on its own runs successfully in phpadmin. But when I run it in my java code, I get exception.

EDIT: The answer is this:

List<Object> list = (List<Object>) em.createNativeQuery(
         "SELECT sum( data_length + index_length ) / 1024 / 1024 FROM information_schema.TABLES GROUP BY table_schema "
).getResultList();


for (Object record : list) {
    double sum = (double) Double.parseDouble(record.toString());
    System.out.println(sum);
}
3
  • What exception do you get? Commented Mar 16, 2016 at 13:23
  • 1
    add StackTrace Please .. :) Commented Mar 16, 2016 at 13:24
  • the trace is there now. Commented Mar 16, 2016 at 13:57

2 Answers 2

3

It works on phpmyadmin because it's a native query. Try using em.createNativeQuery(). That method should be used for plain SQL queries. em.createQuery() accept queries written in JPQL (Java Persistence Query Language).

Docs of createNativeQuery(String sqlString):

@param sqlString a native SQL query string

Docs of createQuery(String qlString):

@param qlString a Java Persistence query string

Sign up to request clarification or add additional context in comments.

Comments

0

It probably the \"DataBaseSizeinMB\"

Try to remove this.

Additionally you should always post the Stack Trace here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.