【问题标题】:Transactions with Prepared Statements fails带有准备好的语句的事务失败
【发布时间】:2017-03-08 15:58:19
【问题描述】:

我尝试使用 transactionsprepared statements 来更新表 INVENTORY 其中:

CREATE TABLE "INVENTORY" (
    "product" VARCHAR NOT NULL,
    "version" VARCHAR NOT NULL,
    "lot" VARCHAR,
    "barcode" VARCHAR,
    "quantity" INTEGER,

    PRIMARY KEY ("barcode")
);

我想做的是插入一个新行,但是如果在我的表中存在具有相同主键的行,则通过添加旧值 + 新值来更新它。

我阅读了thisthis,我认为我正确地遵循了说明。

String sqInsert = "INSERT INTO INVENTORY VALUES ('100541026044','01','301610101','10054102604401301610101','5000')";
String sqUpdate = "UPDATE INVENTORY set quantity = quantity + 5000 where barcode='10054102604401301610101'";

// transaction block start   
c.setAutoCommit(false);         

stm = c.prepareStatement(sqInsert);    
System.out.println("try to insert");
result = stm.executeUpdate();

System.out.println("try to update");
stm = c.prepareStatement(sqUpdate);
result = stm.executeUpdate();

c.commit(); //transaction block end
System.out.println("Done!");

在我的表中存在barcode=10054102604401301610101 的行,因此我希望执行更新。但是,我得到了

try to insert
Error inserting products :[SQLITE_CONSTRAINT]  Abort due to constraint violation (UNIQUE constraint failed: INVENTORY.barcode)

注意:(我没有设置查询中的值,而是使用stm.setString(1, "string");)我只想在这里缩短代码

【问题讨论】:

  • 您正在执行插入和更新。 (如果插入成功,则更新新行。)您需要一个 if/else。
  • @CL。你确定这两个查询都被执行了吗?如果是,为什么我没有进入控制台消息try to update
  • 我先反转了查询,更新然后插入,它起作用了..

标签: sqlite jdbc transactions


【解决方案1】:

当然,由于条形码上的主键重复,它会失败。 先做更新

stm = c.prepareStatement(sqUpdate);
result = stm.executeUpdate();

然后检查结果的值,这将是受更新影响的行数。如果为零,则不存在具有给定条形码的产品,因此在更新后执行

if (0==result) {
    stm = c.prepareStatement(sqInsert);    
    stm.executeUpdate();
}

您需要将所有内容都放在 try {} catch {} 下以避免资源泄漏或使连接不可用

try {
    c.setAutoCommit(false);
    stm = c.prepareStatement(sqUpdate);
    result = stm.executeUpdate();
    if (0==result) {
        stm.close();
        stm = null;
        stm = c.prepareStatement(sqInsert);    
        stm.executeUpdate();
        stm.close();
        stm = null;
    }
    c.commit();
} catch (SQLException e) {
    if (stm!=null) stm.close();
    c.rollback();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2014-01-29
    • 2021-04-22
    • 2015-07-27
    • 2011-07-19
    • 2014-03-16
    相关资源
    最近更新 更多