mysql存储过程的可调用语句在java中返回假值

时间:2019-03-15 12:23:59

标签: java mysql stored-procedures

我已经在mysql中编写了一个存储过程,它在mysql工作台中可以正常工作。但是,当从Java代码调用存储过程时,可调用语句返回的是假值,即该语句未正确执行。

存储过程如下。

CREATE DEFINER=`root`@`localhost` PROCEDURE `invoice_sale_price`(in p_name 
varchar(255), out sp int)
BEGIN
declare p_id int default 0;
select ProductID into p_id from products.products where ProductName = 
p_name;
select SalePrice into sp from products.purchase_details where ID = (select 
max(ID) from (select * from products.purchase_details where ProductID = 1) 
as t);
END

正在调用存储过程的Java代码是

public static int salePrice(String productName) throws SQLException {
   Connection conn ;
   int sp = 0;
   conn = DriverManager.getConnection("jdbc:mysql://localhost/products","root","root");
   CallableStatement cs = conn.prepareCall("{call invoice_sale_price(?,?)}");
   cs.setString("p_name",productName);
   cs.registerOutParameter("sp", Types.INTEGER);
   Boolean hasResult = cs.execute();
   System.out.println("hasResult : "+hasResult);//hasResult giving false value...

   if(hasResult){
       sp = cs.getInt("sp");
       System.out.println("Invoice Db : "+sp);
   }
   cs.close();
   conn.close();
   return sp;
   }

在上述结果中,hasResult = cs.execute()返回false。 调用该函数时,它不返回任何值,因为 cs.execute()返回false。

  public void addItem(MouseEvent mouseEvent) throws SQLException {

    System.out.println(salePrice("Ginger"));
}

以上代码的输出显示为“ 0”;

任何人都可以告诉代码有什么问题吗

0 个答案:

没有答案