Link to home
Start Free TrialLog in
Avatar of tia_kamakshi
tia_kamakshiFlag for United Arab Emirates

asked on

Returning java resultset from MSSQL stored Procedure

How to we return a java 'ResultSet' from a MSSQL Stored procedure.
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

The example I posted was Oracle, but is same concept on the Java side.  To execute the stored procedure, though, in MS SQL you will need to use EXEC verb instead of CALL.

CallableStatement cstmt = dbConn.prepareCall("{exec stored_procedure_name()}");
Avatar of tia_kamakshi

ASKER

I am writting

CallableStatement cstmt = conn.prepareCall("{exec sp_NumberOfNewUsers_PB}");

ResultSet rs = cstmt.getResultSet();

I am getting ResultSet as null value
???

Any help

Thanks
Have you called execute on the CS?
Sorry I didnot understood CS??

I called my SP as
CallableStatement cstmt = conn.prepareCall("{exec sp_NumberOfNewUsers_PB}");

ResultSet rs = cstmt.getResultSet();

My SP is
Create procedure sp_NumberOfNewUsers_PB as
SELECT
            CLIENTTYPE,WEBSITE,COUNT(*) CT
      FROM
            RET_CLIENTS
      WHERE
            CLIENTTYPE IN ('RETAIL', 'INSTITUTIONAL')       AND
            YEAR(CREATIONDATE) = YEAR(GETDATE())

      GROUP BY
            CLIENTTYPE, WEBSITE
order by WEBSITE, CLIENTTYPE



>>Sorry I didnot understood CS??

CS = CallableStatement. Looks like you didn't
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
CallableStatement cstmt = conn.prepareCall("{exec sp_NumberOfNewUsers_PB}");
'execute callable statement here
cstmt.execute();
ResultSet rs = cstmt.getResultSet();

Just like in example I posted link to earlier.  Think CEHJ's had same thing as well.
>>Looks like you didn't
?? Sorry again

Can you please help me in fixing
Connection conn = DBConnection.getConnection();
        try
        {
Connection conn = DBConnection.getConnection();
 CallableStatement cstmt = conn.prepareCall("{exec sp_NumberOfNewUsers_PB}");
            ResultSet rs = cstmt.getResultSet();
while (rs.next())
             {
}
}catch(Exception e)
        {
            throw e;
        }finally
        {
              conn.close();
        }
Connection conn = DBConnection.getConnection();
try
{
      Connection conn = DBConnection.getConnection();
      CallableStatement cstmt = conn.prepareCall("{exec sp_NumberOfNewUsers_PB}");
        ResultSet rs = cstmt.getResultSet();

      while (rs.next())
        {
      }
}catch(Exception e)
{
     throw e;
}finally
{
       conn.close();
}
See two previous posts for how to fix.  CEHJ's is nice as you can then use the boolean flag later in code.
Many Thanks. This works great