繁体   English   中英

如何在 mysql 中创建存储过程并在 JAVA 中调用它。 我需要一些帮助来创建一个可调用的过程。 下面是我的代码

[英]How to create stored procedure in mysql and call it in JAVA. i need some help on creating a callable procedure. below id my code

 drop PROCEDURE if exists insert_poo;
 DELIMITER $$

 CREATE PROCEDURE insert_poo(IN barcode varchar(250),IN qty float,IN amount float,IN vat float,IN 
description varchar(250),IN clrk_code varchar(20),IN mechno varchar(20),IN bill_date datetime)
BEGIN
DECLARE unit_pric float;
DECLARE itemcode varchar(150);
SET  unit_pric =(select retail1 FROM `mytable` WHERE  `mytable`.barcode = barcode);
SET  itemcode =(select prod_id FROM `mytable` WHERE `mytable`.barcode = barcode);
INSERT into mytable2(clrk_code,tran_code,tran_desc,tran_qty,unit_price,tran_amt,bill_date,tax)values(clrk_code,barcode,description,qty, unit_pric,amount,bill_date,vat)

END $$
DELIMITER ;

请提供有关如何创建和调用它的任何解决方案的帮助。 提前致谢

也许您可以使用以下示例作为参考:

下面是存储过程本身:

connect anyDbName/anyDbName

CREATE OR REPLACE PROCEDURE any_storeProcedure_name


-- Following are some example parameters you may use in your SP
(
  id varchar2,
  name_param            varchar2,

-- The control status of the operation
   statusOperation_out   OUT   VARCHAR2
)

AS
BEGIN
    statusOperation_out := 'in_proccess';

  insert into property_name       values('Name',        id, name_param);

  COMMIT;
statusOperation_out := 'ok';


EXCEPTION 
  WHEN OTHERS THEN
  statusOperation_out := 'error';
      ROLLBACK;
END;
/

以下是使用之前SP的Java方法调用:

public long addProperties(String id, String  name) {


        //The string sql syntax for calling the store procedure
        String sql = "{CALL any_storeProcedure_name("
                + "id    => ?, "
                + "name  => ?)}";

        try (Connection conn = CreateConnection.getDSConnection();
                CallableStatement cs = conn.prepareCall(sql)) {


            //The following are the parameters for the store procedure
            cs.setString    (1, id);
            cs.setString    (2, name);

            //Following are the parameters to get some outputs from the store procedure
            cs.registerOutParameter(3, Types.VARCHAR);

            cs.executeQuery();

            //The return varible from the store procedure is the one
            //that is being used for feedback on whether the SP ran fine. 
            if (cs.getString(3).equalsIgnoreCase("ok")) {
                System.out.println("Feedback from SP is: " + cs.getString(3));
                return 1;
            } else {
                return 0;
            }

        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

所以希望这可以提供一些参考,顺便说一下我使用的DB是Oracle 11g。 但我记得它与 MySQL DB 非常相似。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM