Eclipse中写Java代码实现Java连接MYSQL数据库
Java代码中有一句较长的sql语句,在执行此sql的行报错,打印此sql,直接复制至MYSQL数据库运行,正确无误。打印的sql如下:set@table_name=(s...
Java代码中有一句较长的sql语句,在执行此sql的行报错,打印此sql,直接复制至MYSQL数据库运行,正确无误。
打印的sql如下: set @table_name=(select concat("test_147.cfsb_kpi__",date_format(date_sub(curdate(),INTERVAL 1 DAY),"%y%m%d00"))) ; set @begin=(select date_format(date_sub(curdate(),INTERVAL 1 DAY),"%Y-%m-%d 08:00:00" )) ; set @end=(select date_format(date_sub(curdate(),INTERVAL 1 DAY),"%Y-%m-%d 09:00:00")) ; set @str=CONCAT(' select _city_,sum(mhs) / sum(wh) as xunhuchenggonglv ,sum(sgs_pag_res)/sum(wh) as beijiaohuiluochenggonglv, sum(mtc_suss)/sum(mtcs) as beijiaochenggonglv,sum(a_alert_delays)/sum(moc_delay) as zhujiaopingjunshiyan from ( select _city_ ,sum(sgs_pag_suss) as mhs,sum(sgs_pags ) as wh,sum(sgs_pag_res) as sgs_pag_res,sum(mtc_suss) as mtc_suss, sum(mtcs) as mtcs,sum(a_alert_delays) as a_alert_delays,sum(moc_delay) as moc_delay from ', @table_name , ' where _city_>0 and _city_ not in (303) and begin_time>=" ',@begin,' " and begin_time<" ', @end,' " group by _city_ )a group by _city_ order by _city_ '); SET @sqlstr=@str; PREPARE stmt from @sqlstr; EXECUTE stmt; deallocate prepare stmt;
eclipse控制台显示错误如下: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 'set @begin=(select date_format(date_sub(curdate(),INTERVAL 1 DAY),"%Y-%m-%d 08:0' at line 1
就是这句sql出了问题
已解决,希望对之后看到的网友有帮助,
经验证,之前写的sql语句包括很多条单独的sql 其中包括定义存储过程,修改sql代码,让其变成只有一句(简单来说就是一个分号),然后运行,正常 展开
打印的sql如下: set @table_name=(select concat("test_147.cfsb_kpi__",date_format(date_sub(curdate(),INTERVAL 1 DAY),"%y%m%d00"))) ; set @begin=(select date_format(date_sub(curdate(),INTERVAL 1 DAY),"%Y-%m-%d 08:00:00" )) ; set @end=(select date_format(date_sub(curdate(),INTERVAL 1 DAY),"%Y-%m-%d 09:00:00")) ; set @str=CONCAT(' select _city_,sum(mhs) / sum(wh) as xunhuchenggonglv ,sum(sgs_pag_res)/sum(wh) as beijiaohuiluochenggonglv, sum(mtc_suss)/sum(mtcs) as beijiaochenggonglv,sum(a_alert_delays)/sum(moc_delay) as zhujiaopingjunshiyan from ( select _city_ ,sum(sgs_pag_suss) as mhs,sum(sgs_pags ) as wh,sum(sgs_pag_res) as sgs_pag_res,sum(mtc_suss) as mtc_suss, sum(mtcs) as mtcs,sum(a_alert_delays) as a_alert_delays,sum(moc_delay) as moc_delay from ', @table_name , ' where _city_>0 and _city_ not in (303) and begin_time>=" ',@begin,' " and begin_time<" ', @end,' " group by _city_ )a group by _city_ order by _city_ '); SET @sqlstr=@str; PREPARE stmt from @sqlstr; EXECUTE stmt; deallocate prepare stmt;
eclipse控制台显示错误如下: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 'set @begin=(select date_format(date_sub(curdate(),INTERVAL 1 DAY),"%Y-%m-%d 08:0' at line 1
就是这句sql出了问题
已解决,希望对之后看到的网友有帮助,
经验证,之前写的sql语句包括很多条单独的sql 其中包括定义存储过程,修改sql代码,让其变成只有一句(简单来说就是一个分号),然后运行,正常 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询