调用后存储过程不起作用,但是准备好了Statement

时间:2015-05-18 10:52:24

标签: java mysql stored-procedures

我的问题是,在mysql中我创建了一个存储过程。我试图通过我的java代码调用此过程。

  

我没有得到任何异常,但数据库不受影响。如果我做了   同样的声明通过一个准备好的陈述一切正常。   如果我在工作台中手动调用该过程也可以。

存储过程

CREATE DEFINER=`root`@`localhost`PROCEDURE `delete`(IN id INT(11))
BEGIN SET SQL_SAFE_UPDATES=0; Delete from sender where id = id; 
END

致电代码

        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) {

        }

任何想法??

1 个答案:

答案 0 :(得分:0)

在存储过程中,避免将其参数命名为其列的名称。将输入参数_id重命名为CREATE DEFINER=`root`@`localhost` PROCEDURE `delete`(IN _id INT(11)) BEGIN SET SQL_SAFE_UPDATES := 0; DELETE FROM sender WHERE id = _id; END; (例如),保留存储过程如下:

{{1}}