简体   繁体   中英

Null value is returned from stored procedure

I have stored procedure which returns username. I am calling stored procedure from java(EclipseIDE) . The returned value from Stored procedure is Null. How to avoid this Null Value.
This is my stored procedure

USE `employee`;
DROP procedure IF EXISTS `add1`;
DELIMITER $$
USE `employee`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `add1`(IN UserId int,
                                                   IN UserFirstName varchar(50),
                                                   IN UserLastName varchar(50),
                                                   IN Education varchar(50),
                                                   IN MarksInME int,
                                                   IN MarksInBE int,
                                                   IN MarksInPUC int,
                                                   IN Gender varchar(50),
                                                   OUT UserName varchar(50))
  BEGIN
    declare varName varchar(50);
    select UserFirstName into varName from User where UserId=3516;
    set UserName=varName;
    insert into   user(UserId,UserFirstName,UserLastName,Education,MarksInME,MarksInBE,MarksInPUC,Gender)
    values(UserId,UserFirstName,UserLastName,Education,MarksInME,MarksInBE, MarksInPUC,Gender);

  END$$
DELIMITER ;

I am calling Stored Procedure:

    java.sql.CallableStatement cStmt = conn.prepareCall("{call add1(?,?,?,?,?,?,?,?,?)}");
    cStmt.setInt(1,3541);    
    cStmt.setString(2,"pALLAVI"); 
    cStmt.setString(3,"K"); 
    cStmt.setString(4,"BE"); 
    cStmt.setInt(5,0);  
    cStmt.setInt(6,60); 
    cStmt.setInt(7,600);  
    cStmt.setString(8,"chetana@gmail.com"); 
    cStmt.registerOutParameter(9, java.sql.Types.VARCHAR);
    cStmt.execute();
    userName = cStmt.getString(9);

When I try to print the UserName its printed as Null?

UserId is hardcoded:

select UserFirstName into varName from User where UserId=3516;

Please check below query:

call add1(3541,'pALLAVI','K','BE',0,60,600,'chetana@gmail.com',@UserName);
SELECT @UserName;

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