用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 1JDBC数据库
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 1JDBC数据库
原因是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语法正确,然后再在java 中调式
jdbc.url=jdbc:mysql://localhost:3306/ibatis?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
加个allowMultiQueries=true