我可以使用String作为WHERE CLAUSE

时间:2022-01-06 16:29:50

Can anybody help me with this JSP problem. Im trying to update the database using code similar to this:

任何人都可以帮我解决这个JSP问题。我试图使用类似于此的代码更新数据库:

So, I have this on my Servlet:

所以,我在我的Servlet上有这个:

String QueryCondition = "id = 1";

That will be passed to this stored procedure:

这将传递给此存储过程:

CREATE 
    DEFINER=`root`@`localhost` 
    PROCEDURE `storedprocedure_1`(QueryCondition TEXT)
BEGIN
    UPDATE users SET name = 'John'
    WHERE QueryCondition;
END

I was thinking if this is possible because the update always fail.
If this isn't possible can you recommend how can i do such thing

我在想是否可能,因为更新总是失败。如果这是不可能的,你能推荐我怎么做这样的事情

1 个解决方案

#1


0  

You can use it in a stored procedure but with a prepared statement.

您可以在存储过程中使用它,但使用准备好的语句。

Example:

DELIMITER //

CREATE 
  DEFINER=root@localhost 
  PROCEDURE storedprocedure_1(QueryCondition TEXT) 
BEGIN 
  SET @query := CONCAT( 'UPDATE users SET name = \'John\' WHERE ',
                        QueryCondition );

  PREPARE stmt FROM @query;
  EXECUTE stmt;
  DROP PREPARE stmt;
END;
//

DELIMITER ;

#1


0  

You can use it in a stored procedure but with a prepared statement.

您可以在存储过程中使用它,但使用准备好的语句。

Example:

DELIMITER //

CREATE 
  DEFINER=root@localhost 
  PROCEDURE storedprocedure_1(QueryCondition TEXT) 
BEGIN 
  SET @query := CONCAT( 'UPDATE users SET name = \'John\' WHERE ',
                        QueryCondition );

  PREPARE stmt FROM @query;
  EXECUTE stmt;
  DROP PREPARE stmt;
END;
//

DELIMITER ;