【问题标题】:Importing Excel file in Database using java使用java在数据库中导入Excel文件
【发布时间】:2021-10-08 16:44:51
【问题描述】:

我正在使用 java 代码将 Excel 数据导入我的数据库。假设有超过 200 行,我想停止导入。是否可以取消中间的导入并回滚导入时在数据库中创建的所有数据?

【问题讨论】:

  • 如果您的数据库具有事务能力,我不明白为什么不这样做。例如,您可以在最后提示并询问用户是否要提交或回滚
  • 是的,您可以在执行第一个 INSERT 之前启动事务,然后在最后一个 INSERT 之后提交。如果不提交,则不会在数据库中插入任何记录。

标签: java sql import transactions


【解决方案1】:

是的。 您只需停用 autoCommit 并在完成后提交或回滚。

一个简单的例子:


 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();

【讨论】:

    猜你喜欢
    • 2018-06-01
    • 2021-06-23
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    相关资源
    最近更新 更多