当前位置:编程学习 > JAVA >>

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

用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 JDBC 数据库 --------------------编程问答-------------------- 更正一点:sql语句的字符串是String sql3 --------------------编程问答-------------------- 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);";
--------------------编程问答-------------------- 建议一条条执行,看的还清楚点,太挤出错难找 --------------------编程问答-------------------- 先把编辑好的sql语句放到mysql工具中执行一次,看看能否正确执行。
确保sql语法正确,然后再在java 中调式 --------------------编程问答-------------------- mysql的JDBC驱动不支持这种写法 不过sqlserver的倒是支持

你换成batch 用executeBatch吧 --------------------编程问答-------------------- 或者将你的url改成这样:


jdbc.url=jdbc:mysql://localhost:3306/ibatis?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true


加个allowMultiQueries=true --------------------编程问答-------------------- mysql服务开了不,数据库连接有问题不,sql语句能不能别写的那么...先不要sql语句,把数据库连接弄好先吧 --------------------编程问答-------------------- 好吧~我已经解决的~ --------------------编程问答--------------------
引用 1 楼 qiuyuncanxue 的回复:
更正一点:sql语句的字符串是String sql3


+1
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,