简体   繁体   中英

why does my Java code override my sql code?

My Sql code works perfectly in sql server but when I use my Java code it changes values in the main table when it should not be changing. the code is to accept an engine number and search through a vehicle table to see if it's available which would be indicated by it's status if not available it would then go to the vehicle return table search for it. if found in the table to which it was available the table should be updated if not found then an error message would be displayed but its only updating the vehicle table even when it shouldn't be updating and also not sending the error message when an invalid engine number is sent

my sql code:

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 code:

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

Can someone please tell me what the problem is and how do i fix it ?

Your main problem is that CallableStatement.executeUpdate() returns:

either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing

You seem to expect it to return the result of your outbound procedure, which it does not.

You may find this of interest. It seems to suggest you can use an InOutParam to return values but I've never used that technique.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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