用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数据库

解决方案 »

  1.   

    更正一点:sql语句的字符串是String sql3
      

  2.   

    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);";
      

  3.   

    先把编辑好的sql语句放到mysql工具中执行一次,看看能否正确执行。
    确保sql语法正确,然后再在java 中调式
      

  4.   

    mysql的JDBC驱动不支持这种写法 不过sqlserver的倒是支持你换成batch 用executeBatch吧
      

  5.   

    或者将你的url改成这样:
    jdbc.url=jdbc:mysql://localhost:3306/ibatis?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
    加个allowMultiQueries=true
      

  6.   

    mysql服务开了不,数据库连接有问题不,sql语句能不能别写的那么...先不要sql语句,把数据库连接弄好先吧