在SpringJDBC中我需要调用一个SP,其参数是
TABLE OF NUMBER
类型,即数字数组。
PROCEDURE MY_SP( PARAM1 IN TABLE OF NUMBER,
PARAM2 IN NUMBER,
[1]
(1 元素整数数组)的输入有效。但是当我尝试根据此参数的声明直接将其传递为 OracleTypes.ARRAY
declareParameter(new SqlParameter("PARAM1", OracleTypes.ARRAY));
我明白了
Caused by: java.sql.SQLException: Fail to convert to internal representation: [Ljava.lang.Integer;@16a25973
at oracle.sql.ARRAY.toARRAY(ARRAY.java:308)
这里有一个旧示例,它使用已弃用的
ArrayDescriptor
并构建 SQL Array
。那一定来自较旧的 SpringJDBC。 ArrayDescriptor
已被弃用,而且我也无权访问 connection
。我正在使用现代 SpringJDBC 方法来实现 StoredProcedure
接口(使用 compile()
),它不会公开 connection
。有什么想法吗?
@Component
public class MySP extends StoredProcedure {
private RowMapper<List<Object>> rowMapper = new MyMapper();
public MySP(@Autowired DataSource dataSource) {
super(dataSource, "MYPACKAGE.MY_SP");
declareParameter(new SqlParameter("PARAM1", OracleTypes.ARRAY));
//...
compile();
}
public String callSP(Integer[] intArray) {
//intArray contains a 1-element integer array, e.g. [1]
Map<String, Object> inputParameters = new HashMap<>();
inputParameters.put("PARAM1", intArray);
Map<String, Object> output = super.execute(inputParameters);
// Process results...
}
您可以使用 JdbcTemplate 来访问连接并执行您的操作。
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
import java.sql.Connection;
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private DataSource dataSource;
// ...
// Access the connection using JdbcTemplate
Connection connection = jdbcTemplate.execute(con -> {
// Here, you have access to the connection object.
// You can perform operations using the connection if needed.
create your array like mentioned here: https://stackoverflow.com/a/76571542/7565792
return con;
});
// Perform your Oracle array creation or other operations using 'connection'.
// Remember to close the connection when you're done.
connection.close();