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代码,让其变成只有一句(简单来说就是一个分号),然后运行,正常
展开
 我来答
育知同创教育
2017-05-16 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
展开全部
import java.sql.*;
public class DataBasePractice {

public static void main(String[] args) {
//声明Connection对象
Connection con;
//驱动程序名
String driver = "com.mysql.jdbc.Driver";
//URL指向要访问的数据库名mydata
String url = "jdbc:mysql://localhost:3306/mydata";
//MySQL配置时的用户名
String user = "root";
//MySQL配置时的密码
String password = "root";
//遍历查询结果集
try {
//加载驱动程序
Class.forName(driver);
//1.getConnection()方法,连接MySQL数据库!!
con = DriverManager.getConnection(url,user,password);
if(!con.isClosed())
System.out.println("Succeeded connecting to the Database!");
//2.创建statement类对象,用来执行SQL语句!!
Statement statement = con.createStatement();
//要执行的SQL语句
String sql = "select * from student";
//3.ResultSet类,用来存放获取的结果集!!
ResultSet rs = statement.executeQuery(sql);
System.out.println("-----------------");
System.out.println("执行结果如下所示:");
追问
程序我有,是mysql运行sql语句是报错了。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式