从JPA中的存储过程获取SELECT结果

时间:2022-10-01 09:27:37

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.

我试图运行一个运行SELECT语句的存储过程,导致单个列说YES或NO。但我找不到在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.

我尝试过使用Void.class,ParameterMode.OUT(在第三个参数中),但没有成功。

I am using Microsoft SQL Server

我正在使用Microsoft SQL Server

1 个解决方案

#1


0  

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

如上所述,将最后一个参数更改为OUT参数

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

然后调用getOutputParameterValue方法来存储结果

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

#1


0  

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

如上所述,将最后一个参数更改为OUT参数

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

然后调用getOutputParameterValue方法来存储结果

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