为什么我的Java代码会覆盖我的sql代码?

时间:2015-10-07 12:20:31

标签: java sql-server

我的Sql代码在sql server中运行得很好但是当我使用我的Java代码时,它会更改主表中不应该更改的值。代码是接受发动机编号并搜索车辆表以查看它是否可用,如果不可用则表示它的状态,然后它将进入车辆返回表搜索它。如果在可用的表中找到,则应该更新表,如果没有找到则会显示错误消息,但它只更新车辆表,即使它不应该更新,也不会在无效时发送错误消息发送号码

我的sql代码:

ALTER procedure outbound
(
   @eng  VARCHAR(25)
) 
AS
BEGIN
     DECLARE @ENG1 VARCHAR(25)
     DECLARE @ENG2 VARCHAR(25)
     DECLARE @ENG3 VARCHAR(25)
     SET @ENG1=@eng

     SELECT @ENG2= Engine_num from Vehicle_retuns where Engine_num=@eng and Status=1
     select @ENG3=Engine_num from Vehicle where Engine_num=@eng and Status=1

     IF (@eng=@ENG3)
       BEGIN
           UPDATE Vehicle SET Description_of_Vehicle='Vehicle have been sent back to the manufactory',Status=0 where Engine_num=@ENG3
           /*SELECT'Vehicle have been sent back to the manufacture'*/
           RETURN (1)

        END

     ELSE  IF(@eng=@ENG2)
       BEGIN
           UPDATE Vehicle_retuns SET purpose ='Vehicle have been sent back to the manufactory',Status=0 where Engine_num=@ENG2    
           /*SELECT'Vehicle have been sent back to the manufacture'*/
           RETURN (2)

       END

    ELSE
       BEGIN
           /*SELECT'THIS ENGINE NUMBER IS EITHER NOT IN THE DATABASE OR INCORRECT'*/
           RETURN (3)

       END

END

Java代码:

public static void outbound(String Eng_num, Connection con)
 {
      try
      {

         CallableStatement cs =  con.prepareCall("{Call outbound(?)}");
         cs.setString(1, Eng_num);
        // cs.executeQuery();// excutequery is used to return object from the database
          int i = cs.executeUpdate();

          if(i==1)
          {
             System.out.println("Vehicle have been sent back to the manufacture"); 
          }//end of if

           else if(i==2)
           {
               System.out.println("Vehicle have been sent back to the manufacture");
           }//end of else if

           else if(i==3)
           {
               System.out.println("THIS ENGINE NUMBER IS EITHER NOT IN THE DATABASE OR INCORRECT");
           }//end of else if

      }// end of try

      catch(Exception e)
      {
        e.printStackTrace();  
      }//end of catch

 }//end of out bound

有人可以告诉我问题是什么以及如何解决?

1 个答案:

答案 0 :(得分:1)

您的主要问题是CallableStatement.executeUpdate()返回:

  

(1)SQL数据操作语言(DML)语句的行数或(2)0表示不返回任何内容的SQL语句

您好像希望它返回outbound程序的结果,而不是。{/ p>

您可能会感兴趣this。似乎建议你可以使用InOutParam来返回值,但我从未使用过该技术。