【问题标题】:Java - MySQL-StoredProcedure not executedJava - MySQL-StoredProcedure 未执行
【发布时间】:2014-10-07 13:07:54
【问题描述】:

我从我的 java 类中调用了一些存储过程。这些存储过程中只有一个似乎没有被调用,但没有抛出异常。存储过程如下所示:

CREATE PROCEDURE `insertLoggingMessage` (IN userName VARCHAR(50), IN softwareId   VARCHAR(100), IN operationName VARCHAR(100), IN msg VARCHAR(1000), IN lvl VARCHAR(10))

BEGIN
    SELECT id INTO @userId FROM benutzer WHERE name = userName;
    INSERT INTO logging (benutzer_id, software_id, operation_name, message, level)
    VALUES (@userId, softwareId, operationName, msg, lvl);
END

运行查询的 java 代码如下所示:

private void logToDatabase(Authentication auth, String operationName, String message, LoggingLevel level) {
  String software = getSoftwareId(auth);
  String query = "CALL insertLoggingMessage(?, ?, ?, ?, ?)";
  try {
    connect(this.dataSource);
    CallableStatement statement = this.conn.prepareCall(query);
    statement.setString(1, auth.getUserName());
    setNullableStringParameter(statement, software, 2);
    statement.setString(3, operationName);
    setNullableStringParameter(statement, message, 4);
    statement.setString(5, level.name());
    statement.execute();
    disconnect();
  } catch (Exception exc) {
    logToFile(auth, "logToDatabase", exc, level);
  }
}

代码无异常执行。当我通过 HeidiSQL 调用存储过程时,它工作正常。 那么为什么存储过程不通过CallableStatement来执行呢?我现在真的很绝望。

【问题讨论】:

    标签: java mysql stored-procedures


    【解决方案1】:

    我找到了解决方法。在通过 select 语句设置值之前,我在存储过程中声明了变量。这行得通。

    DECLARE userId INT(10);
    SELECT id INTO userId FROM benutzer WHERE name = userName;
    INSERT INTO logging (benutzer_id, software_id, operation_name, message, level)
    VALUES (userId, softwareId, operationName, msg, lvl);
    

    【讨论】:

      猜你喜欢
      • 2010-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-30
      • 1970-01-01
      • 1970-01-01
      • 2018-12-28
      相关资源
      最近更新 更多