我已经创建了一个名为 SET 的过程,并且我正在尝试通过 java 向它发送值,有点像这样。
stmt.executeQuery("call set("+id+",'"+name+"','"+sal+"','"+dept+"')");
name、sal 和 dept 都是字符串格式。
但我收到错误。
check the manual that corresponds to your MySQL server version for the right syntax to use near 'set(12,'D','12','cse')' at line 1
最佳答案
“设置”是 reserve word in Mysql所以这可能是你的麻烦的问题。一般建议是避免保留字并有意义地命名您的过程(例如 setAccountRecrod)。
当我成功创建具有该名称的过程时,我必须使用转义名称(带有反勾)来调用它:
stmt.executeQuery("call `set`("+id+",'"+name+"','"+sal+"','"+dept+"')");
但正如建议的那样,最好使用 prepareCall
:
callableStatement = dbConnection.prepareCall("call `set`(" + id + ",'" +
name + "','" + sal + "','" + dept + "')");
callableStatement.executeUpdate();
或者更好:
callableStatement = dbConnection.prepareCall("call `set`(?,?,?,?)");
callableStatement.setInt(1, id);
callableStatement.setString(2, name);
callableStatement.setString(3, sal);
callableStatement.setString(4, dept);
callableStatement.executeUpdate();
当我创建这样的过程时,以上所有内容都适用于最新版本的 MariaDb:
DELIMITER $$
DROP PROCEDURE IF EXISTS `set`;
CREATE PROCEDURE `set` (IN id INT, name VARCHAR(25), sal VARCHAR(25), dept VARCHAR(25))
BEGIN
SELECT VERSION();
END $$
DELIMITER ;
关于java - 如何使用过程将值插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33560526/