please, help me! I've got the trouble. I've next SQL query
SET @sql = '';
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'getInterviewsNumber(\'', name,
'\', vtable.position)',
' AS \'',
name, '\''
) separator ','
) INTO @sql
FROM interview_portal.departments;
SET @sql = CONCAT('SELECT distinct vtable.position, ',@sql, ' FROM inter_count as vtable');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
It works well in MySQL workbench. But how can i execute this query and get ResultSet using Spring JdbcDaoSupport? I've tried a lot of variants, but i got MySQLSyntaxErrorException.
Thanks for your help!
Here is my Exception
SEVERE: Servlet.service() for servlet [generalDispatcher] in context with path [/InterviewPortal] threw exception [Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [SET @sql = ''; SELECT GROUP_CONCAT( DISTINCT CONCAT ( 'getInterviewsNumber(\'', name, '\', vtable.position)',' AS \'', name, '\'') separator ',') INTO @sql FROM interview_portal.departments; SET @sql = CONCAT('SELECT distinct vtable.position, ', @sql, ' FROM inter_count as vtable'); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;]; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT GROUP_CONCAT( DISTINCT CONCAT ( 'getInterviewsNumber(\'', name, '\', vtab' at line 1] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT GROUP_CONCAT( DISTINCT CONCAT ( 'getInterviewsNumber(\'', name, '\', vtab' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
Here is my code:
String sql = "SET @sql = '';" +
" SELECT" +
" GROUP_CONCAT(" +
" DISTINCT CONCAT ( " +
"'getInterviewsNumber(\\'', name, " +
"'\\', vtable.position)'," +
"' AS \\'', name, '\\'') " +
" separator ',') INTO @sql " +
"FROM interview_portal.departments;" +
" SET @sql = CONCAT('SELECT distinct vtable.position, '," +
" @sql, ' FROM inter_count as vtable');" +
" PREPARE stmt FROM @sql;" +
" EXECUTE stmt;" +
" DEALLOCATE PREPARE stmt;" ;
getJdbcTemplate().batchUpdate(temp);
//"ORDER BY " + query.getSortType().getColumnName() + ((query.isDescendingSort()) ? " DESC" : " ASC;");
Object[] args = new Object[0];
return getJdbcTemplate().query(sql, args,
positionItemMapper);
First you have to create a stored procedure with your SQL code, see MySQL documentation .
Then you'll be able to call it with JDBC template:
jdbcTemplate.update("call PROC_NAME (?, ?)", param1, param2);
Spring documentation for more details.
Spring JdbcDaoSupport allows you to execute one query at at time . What you are trying to execute is a complete procedure. You can either store the procedure in MySQL and call it as suggested by cy3er or break it into individual operations.
If I correctly understand you have first a SELECT ... INTO ...
operation that would result to an update
and then a SELECT ...
that would result in a query
.
As you are in java, you should even test that first operation gives a correct result before doing the second.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.