1

My objective is to be able to run Descriptive Statistics mySQL queries on my tables on Java. Some of these queries consist of several parts to get one result such as getting a median of a column which is comprised as follows:

set @ct := (select count(1) from employee);
set @row_id := 0;

select avg(VacationHours) as median
from (select * from employee order by VacationHours) AS T
where (select @row_id := @row_id + 1) 
between @ct/2.0 and @ct/2.0 + 1;

Basically I want to run that query all at once which technically consists of 3 parts. So far all the questions regarding running multiple queries at once consisted of either just SELECT or UPDATE which isn't exactly describing my situation. I have tried adding ?allowMultiQueries=true to the end of my dbURL to no avail.

The code I am using right to execute the above SQL is by using

 pStmt = conn.prepareStatement(sql);
 rs = pStmt.executeQuery(sql); 

which is giving me the error java.sql.SQLException: ResultSet is from UPDATE. No Data.

Any help would be much appreciated :)

thatOneGuy
  • 23
  • 3
  • Why can't you store value of (select count(1) from employee) in java variable. First execute above query, assign it to java var then use var in query statement which dynamically generate your sql statement. No need to use set @. Your @row_id usage is not clear, explain what you are doing with @row_id? – Parth Mar 16 '17 at 16:47
  • please refer this : http://stackoverflow.com/a/19673296/2068790 – Parth Mar 16 '17 at 16:53
  • To be honest I have no idea, I had found the code to this query in [this link](https://www.periscopedata.com/blog/medians-in-sql.html) – thatOneGuy Mar 16 '17 at 16:55
  • Can you use other libraries than Java's standard library? – Krzysztof Atłasik Mar 16 '17 at 16:59

0 Answers0