JDBC执行创建数据库表的语句报错~急

蓝羽笙箫 2013-05-06 10:29:06
用JDBC执行创建数据库表并批量插入数据的sql语句,在mysql中语句执行正确,但用JDBC执行却报语法错误?什么原因?
String sql = "create table rtyrty18 ( dataId int auto_increment not null primary key, ww int(50),ee int(50));insert into rtyrty18 (ww,ee)values(1,1);insert into rtyrty18 (ww,ee)values(2,2);insert into rtyrty18 (ww,ee)values(3,3);insert into rtyrty18 (ww,ee)values(4,4);";
stmt = conn.createStatement();
stmt.executeUpdate(sql3);

错误是:
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 'insert into rtyrty18 (ww,ee)values(1,1);insert into rtyrty18 (ww,ee)values(2,2);' at line 1
...全文
458 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 1 楼 qiuyuncanxue 的回复:
更正一点:sql语句的字符串是String sql3
、 +1
蓝羽笙箫 2013-05-07
  • 打赏
  • 举报
回复
好吧~我已经解决的~
lvzg_005 2013-05-06
  • 打赏
  • 举报
回复
先把编辑好的sql语句放到mysql工具中执行一次,看看能否正确执行。 确保sql语法正确,然后再在java 中调式
q35335010 2013-05-06
  • 打赏
  • 举报
回复
建议一条条执行,看的还清楚点,太挤出错难找
q35335010 2013-05-06
  • 打赏
  • 举报
回复
sql语法错了 原因是3条insert语句不能写在一行 String sql = "create table rtyrty18 ( dataId int auto_increment not null primary key, ww int(50),ee int(50));\n " + " insert into rtyrty18 (ww,ee) values (1,1); insert into rtyrty18 (ww,ee)values(2,2);\n " + " insert into rtyrty18 (ww,ee) values (3,3); \n" + " insert into rtyrty18 (ww,ee)values(4,4);";
蓝羽笙箫 2013-05-06
  • 打赏
  • 举报
回复
更正一点:sql语句的字符串是String sql3
ywj852752270 2013-05-06
  • 打赏
  • 举报
回复
mysql服务开了不,数据库连接有问题不,sql语句能不能别写的那么...先不要sql语句,把数据库连接弄好先吧
liuyuhua0066 2013-05-06
  • 打赏
  • 举报
回复
或者将你的url改成这样:

jdbc.url=jdbc:mysql://localhost:3306/ibatis?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
加个allowMultiQueries=true
liuyuhua0066 2013-05-06
  • 打赏
  • 举报
回复
mysql的JDBC驱动不支持这种写法 不过sqlserver的倒是支持 你换成batch 用executeBatch吧

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧