【问题标题】:executeUpdate method is not working properlyexecuteUpdate 方法无法正常工作
【发布时间】:2016-09-30 21:22:16
【问题描述】:

我正在使用preparedStatement.executeUpdate() 在数据库中插入一些行,但它不起作用。虽然当我调试时它返回值 1 这意味着该行已被插入但是当我检查数据库时它没有插入任何行。不知道为什么会这样。任何形式的帮助将不胜感激。以下是我的代码:

Connection connection = null;
PreparedStatement preparedStatement = null;

try {
    connection = getOracleConnection(requestId);
    preparedStatement = connection.prepareStatement(createAndSaveSummarySQL());
    preparedStatement.setString(1, siteId);
    preparedStatement.setString(PBNConstants.TWO, taskId);
    preparedStatement.setString(PBNConstants.THREE, notificationType);
    preparedStatement.setString(PBNConstants.FOUR, clusterId);


    final long insertStartTime = System.currentTimeMillis();

    final int returnValue = preparedStatement.executeUpdate();

【问题讨论】:

  • 你使用缓存,插入后回滚?你如何检查行是否被插入?我的你有不同的数据库连接?
  • 请显示捕获 .. 最后 ... 块
  • 请添加您得到的异常。

标签: java database prepared-statement


【解决方案1】:

请确保以正确的方式关闭连接,如下所示。

 try {
            connection = getOracleConnection(requestId);     

    }finally{
     if (preparedStatement != null) preparedStatement.close();
      if (connection != null) connection.close();
    }

【讨论】:

  • @Jen 嗨,感谢您的回复。我最后正确地关闭了连接。还有一件事,我在调试时检查插入。所以最终 int returnValue =preparedStatement.executeUpdate();调试时返回值 1,但之后当我检查数据库时,我没有看到任何插入的行。奇怪的是我立即检查了这个语句,它给了我一个错误,上面写着“SQL唯一完整性约束违规”所以相信一行被插入但我没有看到它。我看到了一些奇怪的行为,但没有得出任何结论
  • if (connection != null) { try { connection.close(); } catch (final SQLException e) { } }
  • @Rohit - 我没有任何例外。只是该行没有被插入并且我的应用程序失败,因为插入后我根据 header_id 更新了该行,并且在更新期间它失败了,因为它没有找到任何要更新的行。
猜你喜欢
  • 2019-06-02
  • 2021-12-29
  • 2014-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-01
相关资源
最近更新 更多