使用SimpleJdbcCall和Array作为IN参数(DB2)

时间:2016-04-21 03:31:08

标签: java stored-procedures db2 spring-jdbc

我创建了一个存储过程,其中Array of ROW类型为IN参数,并且能够使用JDBC调用它(Java 1.6) 代码如下

//Create Array Product List
Struct[] productList = new Struct[1];
Object[] customObject = new Object[]{"Fruits"};
productList[0] = con.createStruct("ProductRow", customObject);

// Create product Response List 
Struct[] responseList = new Struct[1];
customObject = new Object[]{new Integer(1), new Integer(2)};
responseList[0] = con.createStruct("ResponseRow", customObject);

Array products = con.createArrayOf("ProductRow", productList);
Array responses = con.createArrayOf("ResponseRow", responseList);       


// Prepare the call statement 
CallableStatement callStmt = con.prepareCall("CALL SP_create(?, ?)"); 

// Set IN parameters 
callStmt.setArray(1, products); 
callStmt.setArray(2, responses); 

// Call the procedure 
callStmt.execute(); 

知道如何使用SimpleJdbcCall做同样的事情吗? 我使用的是Spring 2.5和DB2 9.7版本

1 个答案:

答案 0 :(得分:0)

我终于明白了。我正在粘贴示例代码。

Map<String, Object> in = new HashMap<String, Object>();
        in.put("products", new AbstractSqlTypeValue() {
            @Override
            protected Object createTypeValue(Connection con, int type, String typeName) throws SQLException {
                Struct[] productList = new Struct[1];
                Object[] customObject = new Object[]{"Vegetables"};
                productList[0] = con.createStruct("ProductRow", customObject);

                Array products = con.createArrayOf("ProductRow", productList);
                return products;
            }
        });