简体   繁体   English

如何在Java中执行多SQL行?

[英]how can I execute a multi sql line in Java?

These three line of code are have to be execute at the same time. 这三行代码必须同时执行。

  SET @num := 0;

        UPDATE your_table SET id = @num := (@num+1);

        ALTER TABLE tableName AUTO_INCREMENT = 1;
    ********************************************************
    Statement s1 = DataCon.getDataCon().createStatement();
                            s1.execute("SET @num := 0; UPDATE tblstaff SET staffid = @num := (@num+1); ALTER TABLE tblstaff AUTO_INCREMENT = 1");
                            s1.close();

Try this! 尝试这个! I Found it very useful if you wanna keep data in numerical order 我发现如果要按数字顺序保留数据非常有用

Statement stm = DataCon.getDataCon().createStatement(); 
String stm1 = "SET @num := 0"; 
String stm2 = "UPDATE tblstaff SET staffid = @num := (@num+1)"; 
String stm3 = "ALTER TABLE tblstaff AUTO_INCREMENT = 1"; 
stm.addBatch(stm1); 
stm.addBatch(stm2); 
stm.addBatch(stm3); 
stm.executeBatch(); 
stm.close();

Interestingly, you don't need to. 有趣的是,您不需要。 Because you are not using order by , you can use: 由于您未使用order by ,因此可以使用:

UPDATE your_table CROSS JOIN (SELECT @num := 0) params
    SET id = @num := (@num+1);

Having said that, ORDER BY is often desirable in this situation. 话虽如此,在这种情况下通常需要ORDER BY

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM