简体   繁体   中英

Stored Procedure Returning Null

This question is similar to: Mysql - Stored procedure OUT variable return null , sadly their solutions did not work for me.

I have a database populated with data about who used my program and when, and I am trying to create a stored procedure in MySql to retrieve all the data and send it to my java servlet, where it will be processed.

This is what I have currently for my Stored Procedure:

CREATE DEFINER=`root`@`localhost` 

PROCEDURE `getTableData`(OUT time VARCHAR(45), OUT fName VARCHAR(45), 
                        OUT lName VARCHAR(45), OUT rVar INT)

BEGIN
    SELECT rqTime, name, lastName, requestVar FROM pdata.userlist;

END

Here is how I call the query in Java:

CallableStatement stmt = null;
ResultSet rs = null;        
String sqlQuery = "{call getTableData (?, ?, ?, ?)}";       
stmt = conn.prepareCall(sqlQuery);

    stmt.registerOutParameter(1, java.sql.Types.VARCHAR);
    stmt.registerOutParameter(2, java.sql.Types.VARCHAR);
    stmt.registerOutParameter(3, java.sql.Types.VARCHAR);
    stmt.registerOutParameter(4, java.sql.Types.INTEGER);

stmt.execute();

When I try to call this stored procedure in Java (I've tried using result sets and the normal getString(), etc) all values return null.

My table design is along the lines of:

Request Time (millis), name, lastName, requestID 

1402341252155, John, Doe, 11

Any help would be greatly appreciated. I feel as though the problem lies in the stored procedure itself, particularly the how I select the variables from the the table and assign them to the "out" parameters.

Thanks!

The problem is that you never set the values for your out parameters in your procedure. Fix your query:

SELECT
    rqTime, name, lastName, requestVar
INTO
    time, fName, lName, rVar
FROM pdata.userlist;
-- Probably you need a WHERE or another statement to make sure you only retrieve 1 row as result
-- Otherwise, this stored procedure will fail

Also, in Java side, you should register the out parameters and retrieve the results directly from CallableStatement , do not retrieve them from a ResultSet .

public static  List<MonhthyBranchWiseSalesPurchesesDTO> getMonBranWiseDetails(Connection con){

    List<MonhthyBranchWiseSalesPurchesesDTO> monhthyBranchWiseSalesPurchesesDTOList = null;
    ResultSet rset=null;
    CallableStatement proc_stmt = null;
    CachedRowSet cRowSet = null;

    try{        
        MonhthyBranchWiseSalesPurchesesDTO objTarget = new MonhthyBranchWiseSalesPurchesesDTO();
     System.out.println("okkkkhhhh");
        proc_stmt = con.prepareCall("xremit.dbo.p_getBHDMBReport"); 
        //p_getBHDMBReport
        //proc_stmt = con.prepareCall("uaeexdw.dbo.DBA.p_getBHDMBReport");

        rset = proc_stmt.executeQuery();
        System.out.println("after   ------");
        cRowSet = new CachedRowSetImpl();
        cRowSet.populate(rset);

        System.out.println("lll");
        monhthyBranchWiseSalesPurchesesDTOList = (List) ResultSetUtils.getCollection(objTarget, cRowSet);


    }

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