gpt4 book ai didi

Java - MySQL-StoredProcedure 未执行

转载 作者:行者123 更新时间:2023-11-30 22:56:19 25 4
gpt4 key购买 nike

我从我的 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 执行呢?我现在真的很绝望。

最佳答案

我找到了解决方法。在通过 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);

关于Java - MySQL-StoredProcedure 未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26236804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com