简体   繁体   中英

Get the SELECT result from a stored procedure in JPA

I am trying to run a stored procedure that runs a SELECT statement that results in a single column saying YES or NO. But I could not find a way to get this result in java.

My code:

StoredProcedureQuery sp = em.createStoredProcedureQuery("sp_verify_solutuion")
                    .registerStoredProcedureParameter("cod_company", Integer.class, ParameterMode.IN)
                    .registerStoredProcedureParameter("cod_person", Integer.class, ParameterMode.IN)
                    .registerStoredProcedureParameter("result", Class.class, ParameterMode.REF_CURSOR);
sp.setParameter("cod_company", 552);
sp.setParameter("cod_person", cod_person);

sp.execute();
//here I want to save the result in a String variable

I have tried with Void.class, ParameterMode.OUT (in the third parameter), but no success.

I am using Microsoft SQL Server

Change the last parameter to be your OUT parameter, like you said

StoredProcedureQuery sp = em.createStoredProcedureQuery("sp_verify_solutuion")
                        .registerStoredProcedureParameter("cod_company", Integer.class, ParameterMode.IN)
                        .registerStoredProcedureParameter("cod_person", Integer.class, ParameterMode.IN)
                        .registerStoredProcedureParameter("result", Integer.class, ParameterMode.OUT);
                sp.setParameter("cod_company", 552);
                sp.setParameter("cod_person", cod_person);

                sp.execute();

And then call the getOutputParameterValue method to store the result

String res = (String)sp.getOutputParameterValue("result");

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