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);
}