Variables in sql statements are not recognized in jdbc

demand:
there is a table recording information about people. Now you want to find the median age of a person. The method is:
first sort the age in the table and add the row order to the sorted age to return to an intermediate table, and then check the intermediate table to return the average age of the line number as the median.
SQL code is as follows;

set @rowindex = 0;
select avg(temp2.age) from 
(select @rowindex:=@rowindex + 1 as rawindex,student.age as age from student order
by student.age) AS temp2 where temp2.rawindex IN (FLOOR((@rowindex+1) / 2) ,
CEIL((@rowindex+1) / 2));

problem:
this sql statement contains the sql variable @ rowindex, to record the line order, but it can correctly return the result in mysql, but it reports an error in the java code and prompts the syntax error. I hope you can advise me, how to solve it?
correct result executed in 1.sql (17 lines of red cross does not affect, because it is caused by sql above 17 lines)

clipboard.png
2.java:

clipboard.png

JDBCMysqlmysql-connector

clipboard.png

Mar.31,2021

this is actually multiple SQL statements. By default, you cannot execute multiple SQL statements at a time, but it seems that you can modify the jdbc connection settings.

Menu