[英]Stored Procedure does not work when called but prepared Statement does
my problem is, that in mysql I created a stored procedure. 我的问题是,在mysql中我创建了一个存储过程。 Im trying to call this procedure through my java-code.
我试图通过我的java代码调用此过程。
I dont get any exception but the db is not affected.
我没有任何异常,但数据库不受影响。 If I make the same Statement through a preparedstatement everything works just fine.
如果我通过preparedstatement发表相同的声明,一切都会很好。 And if i call the procedure manuallys in the workbench it works too.
如果我在工作台中手动调用该程序,它也可以工作。
Stored Procedure 储存程序
CREATE DEFINER=`root`@`localhost`PROCEDURE `delete`(IN id INT(11))
BEGIN SET SQL_SAFE_UPDATES=0; Delete from sender where id = id;
END
Call in Code 调用代码
public void deleteSender(int id){
CallableStatement cStmt = null;
PreparedStatement pStmt = null;
PreparedStatement pStmt_2 = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/info", "root","");
String call = "{call delete(?)}";
cStmt = connection.prepareCall(call);
cStmt.setInt(1, id);
boolean b = cStmt.execute();
} catch (Exception e) {
}
}
As prepared Statement:
public void deleteSender(int id){
CallableStatement cStmt = null;
PreparedStatement pStmt = null;
PreparedStatement pStmt_2 = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/info", "root","");
pStmt = connection.prepareStatement("Delete from sender where id="+id);
pStmt_2 = connection.prepareStatement("Delete from table2 where sender_id="+id);
boolean b = pStmt.execute();
boolean b_2 = pStmt_2.execute();
if(b == false || b_2 == false){
JOptionPane.showMessageDialog(null, "Data deleted");
}
} catch (Exception e) {
}
Any ideas ?? 有任何想法吗 ??
In the stored procedure, avoid naming its parameters as the name of its columns. 在存储过程中,避免将其参数命名为其列的名称。 Rename the input parameter
id
by _id
(for example), leaving your stored procedure as follows: 用
_id
重命名输入参数id
(例如),使存储过程如下:
CREATE DEFINER=`root`@`localhost` PROCEDURE `delete`(IN _id INT(11))
BEGIN
SET SQL_SAFE_UPDATES := 0;
DELETE FROM sender WHERE id = _id;
END;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.