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 ?
-
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 backg00se– g00se2021-08-03 13:23:47 +00:00Commented 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.The Impaler– The Impaler2021-08-03 13:23:59 +00:00Commented Aug 3, 2021 at 13:23
Add a comment
|
1 Answer
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();