簡體   English   中英

用SimpleJdbcCall調用存儲過程將從Temp表中返回空結果集。 存儲過程沒有輸入和輸出參數

[英]Calling a Stored procedure with SimpleJdbcCall returns null result set from Temp table. The stored procedure have no input and out put paramets

我必須從java spring調用存儲過程。 該過程沒有輸入和輸出參數。 當我使用SimpleJdbcCall調用存儲過程時,它返回空結果。 如何解決此問題? 謝謝

儲存程序

  ALTER PROCEDURE "DBA"."Records" () 
 RESULT ( Number    integer 
 , Alias     varchar(255) 
 , File      varchar(255) 
 , ConnCount integer 
 , PageSize  integer 
 , LogName   varchar(255) 
 ) 
 BEGIN 
 DECLARE dbid integer; 
 DECLARE local temporary table t_db_info 
 ( Number    integer      null 
 , Alias     varchar(255) null 
 , File      varchar(255) null 
 , ConnCount integer      null 
 , PageSize  integer      null 
 , LogName   varchar(255) null 
 ) on commit delete rows; 
 set dbid=next_database(null); 
 lbl: loop 
 if dbid is null then 
 leave lbl 
 end if 
 ; 
 insert into t_db_info values( 
 dbid,db_property('Alias', 
 dbid),db_property('File', 
 dbid),db_property('ConnCount', 
 dbid),db_property('PageSize', 
 dbid),db_property('LogName', 
 dbid)); 
 set dbid=next_database(dbid) 
 end loop lbl 
 ; 
 select * from t_db_info 
 ORDER BY alias 
 END

Java代碼


public class Generic <T>{

protected NamedParameterJdbcTemplate namedJdbcTemplate;
protected JdbcTemplate jdbcTemplate;
protected SimpleJdbcCall procReadAllorgs;

public GenericDAO(DataSource ds) {
    this.procReadAllorgs = new SimpleJdbcCall(jdbcTemplate)
                .withProcedureName("DBA.Records")
                .returningResultSet("records",
                BeanPropertyRowMapper.newInstance(Records.class));                       
        init();
    }
}


DAO Impl class



    public class  OrganDAOImpl implements Generic <T> {

public List getList() {
        Map m = procReadAllorgs.execute(new HashMap<String, Object>(0));
        return (List) m.get("records");
    }
   }

控制器類

@RequestMapping("/org")
public class TestController {
      @Autowired
      protected  OrganDAOImpl organDAOImpl;
@RequestMapping(value = "/sunday", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
         public @ResponseBody List getAll() {
             logger.debug("calling the controller...");
             return  OrganDAOImpl.getList();

} 
 }

模型類

public class Records {
    private Integer Number;
    private String Alias;
    private String File;
    private Integer ConnCount;
    private Integer PageSize;
    private String LogName;
       //getter and setter 

    }

我不認為此過程返回任何東西嗎? 它沒有out參數可以返回它(我不是plsql專家)

.withProcedureName("Records")
.withCatalogName("DBA")
.withSchemaName(schema);

在兩種情況下,您將從數據庫獲得空值。

1-如果調用錯誤,並且沒有異常處理/嘗試捕獲,則2-該過程不返回任何內容。

問題 :如何從simplejdbccall獲取輸出參數! 我正在將MSSQL Server與Java Spring Framework結合使用。

我檢查了存儲過程,發現忘了將@out_personId變量標記為輸出:

ALTER PROCEDURE [dbo].[spNewUser]( @in_firstName nvarchar(20), @in_lastName nvarchar(20),@in_addressLine1 nvarchar(50), @in_addressLine2 nvarchar(50), @in_addressLine3 nvarchar(50), @in_addressLine4 nvarchar(50),@in_dob datetime, @in_gender nvarchar(1), @in_email nvarchar(100), @in_mobileNo nvarchar(50), @in_landLineNo nvarchar(50),@out_personId int output )

進行此更改后(以粗體顯示),我能夠通過SimpleJDBCCall讀取返回的int值。 這是代碼示例:

public Person createPerson(final Person person) {

        SimpleJdbcCall storedProcedure = new SimpleJdbcCall(dataSource)
                .withProcedureName("spNewUser")
                .withReturnValue();

        SqlParameterSource params = new MapSqlParameterSource()
                                    .addValue("in_firstName", person.getFirstName())
                                    .addValue("in_lastName", person.getLastName())
                                    .addValue("in_addressLine1", person.getAddressLine1())
                                    .addValue("in_addressLine2", person.getAddressLine2())
                                    .addValue("in_addressLine3", person.getAddressLine3())
                                    .addValue("in_addressLine4", person.getAddressLine4())
                                    .addValue("in_dob", new java.sql.Date(person.getDob().getTime()))
                                    .addValue("in_gender", person.getGender())
                                    .addValue("in_email", person.getEmail())
                                    .addValue("in_mobileNo", person.getMobileNo())
                                    .addValue("in_landLineNo", person.getLandLineNo())
                                    .addValue("out_personId", 0);

        Map<String, Object> output = storedProcedure.execute(params);
        Integer id = (Integer) output.get("out_personId");
        person.setId(id);
        return person;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM