【问题标题】:Calling a Stored procedure with SimpleJdbcCall returns null result set from Temp table. The stored procedure have no input and out put paramets使用 SimpleJdbcCall 调用存储过程会从 Temp 表返回空结果集。存储过程没有输入和输出参数
【发布时间】:2016-06-03 09:07:36
【问题描述】:

我必须从 java spring 调用存储过程。这些过程没有输入和输出参数。当我使用 SimpleJdbcCall 调用存储过程时,它返回 null 结果。我该如何解决这个问题?谢谢

存储过程

  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 

    }

【问题讨论】:

    标签: java spring stored-procedures


    【解决方案1】:

    问题:如何从 simplejdbccall 中获取输出参数!!我正在使用带有 Java Spring Framework 的 MSSQL Server。

    我检查了我的存储过程,发现我忘记将@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 intoutput)

    在此更改(粗体)之后,我能够通过 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;
    }
    

    【讨论】:

    • 我在上面的示例中尝试过,但出现错误“[TeraJDBC 16.20.00.13] [错误 5531] [SQLState HY000] 过程的参数不支持命名列表。”有什么想法吗?
    【解决方案2】:

    我认为这个程序不会返回任何东西?! 它没有一个out参数来返回它,(我不是plsql专家)

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

    在两种情况下,您将从 DB 中获得空值。

    1- 如果调用错误并且您没有异常处理/尝试并捕获 2- 该过程不返回任何内容。

    【讨论】:

      猜你喜欢
      • 2018-07-28
      • 2018-06-14
      • 2019-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多