0

I am importing the Excel data into my database using java code. Suppose there are more than 200 rows and I want to stop the import.Is it possible to cancel the import in between and rollback all the data which got created in database while importing ?

2
  • I don't see why not if your db is transaction-capable. You could for instance prompt at the end and ask the user if they want to commit or roll back Commented Aug 3, 2021 at 13:23
  • Yes, you can initiate a transaction before the first INSERT is executed and then you COMMIT after the last INSERT. If you don't commit, no record will be inserted in the database. Commented Aug 3, 2021 at 13:23

1 Answer 1

1

Yes it is. You just have to deactivate autoCommit and commit or rollback whenever you are done.

A simple example:


 DriverManager.registerDriver(new com.mysql.jdbc.Driver());
 String url = "jdbc:mysql://localhost/mydatabase/icpc";
 Connection conn = DriverManager.getConnection("url", "username", "pass");

 // Set the auto commit false. This will execute all
 // SQL statements as individual transactions
 con.setAutoCommit(false);

 // Do your thing with the Excel file...

 // In the end you could either rollback or commit like this
 conn.commit();

 // OR

 conn.rollback();
Sign up to request clarification or add additional context in comments.

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.