【问题标题】:Java create & update mysql table syntax errorJava 创建和更新 mysql 表语法错误
【发布时间】:2021-03-20 05:53:21
【问题描述】:

我不确定如何解决这个问题。我在 MySQLWorkbench 中测试了 SQL 语句,它可以工作。

    public void updateTable() throws SQLException
    {
        String query  = "SET SQL_SAFE_UPDATES = 0; drop table if exists studentCopy; create table studentCopy select * from student;\n" +
                "update studentCopy join (select ID, sum(credits) new_tot_cred\n" +
                "from takes left join course using(course_id) where grade is not null and grade<>'F'\n" +
                "group by ID) cred using(ID) set tot_cred=cred.new_tot_cred;\n" +
                "SET SQL_SAFE_UPDATES = 1;\n" +
                "select * from studentCopy;";

        resultSet = statement.executeQuery(query);
    }

java.sql.SQLSyntaxErrorException: 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 'drop table if exists studentCopy; create table studentCopy select * from student' at line 1
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.StatementImpl.executeQuery(StatementImpl.java:1198)
    at MyQuery.updateTable(MyQuery.java:157) // resultSet = statement.executeQuery(query);
    at TestMyQuery.main(TestMyQuery.java:38) // main method call

此查询在工作台中有效。

SET SQL_SAFE_UPDATES = 0;
drop table studentCopy;
create table studentCopy select * from student;
update studentCopy join (select ID, sum(credits) new_tot_cred
from takes left join course using(course_id) where grade is not null and grade<>'F'
group by ID) cred using(ID) set tot_cred=cred.new_tot_cred;
SET SQL_SAFE_UPDATES = 1;
select * from studentCopy;

【问题讨论】:

  • 一次执行 1 条 sql 语句。将它们放入一个数组中,然后循环执行。
  • 您认为SQL_SAFE_UPDATES 会做什么?为什么要复制表格?你不是在 SELECT 形式的 UPDATE 查询之后吗?
  • 这是一个作业。我想证明我知道如何在不影响现有表的情况下更新表。

标签: java mysql sql-update


【解决方案1】:

您可以使用 executeBatch() 在 JDBC 中同时执行多个查询。

您可以为要执行的每个查询创建单独的查询字符串,并将它们添加到您的批处理中。例如这里我创建了 4 个查询

String sql0 = "SET SQL_SAFE_UPDATES = 0;";
String sql1 = "insert into employees(first,last,age) values('Foo','Bar',18);";
String sql2 ="create or replace table employeesCopy as select  id, first, last, age  from employees;";
String sql3 ="update employees set first='changed' where id = 1;";

您可以将这些查询添加到您的语句对象中

 con.setAutoCommit(false); //to make sure data doesn't update if exception  occurs
 Statement stmt =  con.createStatement();
 stmt.addBatch(sql0);
 stmt.addBatch(sql1);
 stmt.addBatch(sql2);
 stmt.addBatch(sql3);

在批处理中添加每个查询后,您可以执行批处理

 stmt.executeBatch();
 con.commit(); // commiting all the data

限制:不能批量执行选择查询

如果还有其他我不知道的限制

【讨论】:

  • 这有帮助。我对addBatch 查询使用了一个数组和一个for 循环,并使用了一个单独的executeQuery 来返回select 值。
【解决方案2】:

不要照原样从workbench.insted中复制这个执行查询一个一个循环。

public void updateTable() throws SQLException {
  String query[] = {
    "SET SQL_SAFE_UPDATES = 0;",
    "drop table if exists studentCopy; ",
    "create table studentCopy select * from student;",
    "update studentCopy join (select ID, sum(credits) new_tot_cred;",
    "from takes left join course using(course_id) where grade is not null and grade<>'F';",
    "group by ID) cred using(ID) set tot_cred=cred.new_tot_cred;",
    "SET SQL_SAFE_UPDATES = 1;",
    "select * from studentCopy;"
  };
  for (int i = 0; i < query.length(); i++) {
    resultSet = statement.executeQuery(query[i]);
  }
}
  
 

<!-- begin snippet: js hide: false console: true babel: false -->

【讨论】:

  • 这是我实施您的解决方案时的错误。 Can not issue data manipulation statements with executeQuery()addBatchexecuteBatch 工作了。
猜你喜欢
  • 2014-04-29
  • 2014-04-09
  • 2016-02-07
  • 2014-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-14
相关资源
最近更新 更多