0

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();
4
  • 1
    Any reason you can't run them separately? Commented Dec 9, 2015 at 22:53
  • looks good. what's the question? Commented Dec 9, 2015 at 23:00
  • how can I execute a multi sql line in Java? is the question Commented Dec 10, 2015 at 18:57
  • the reason is I have to reconnect to db twice :) Commented Dec 10, 2015 at 18:59

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Please format your code readably. See the help pages for how to post code in the SO editor.
0

Interestingly, you don't need to. Because you are not using order by, you can use:

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.