其實單就這個問題網上有很多博客,但是有個問題就是我們傳入的如果是array數組的數據的時候,最終oracle會接收不到數據,但是基本上所有博客都未曾提及,在這裏說明一下,需要maven引用一下orai18n這個包,詳細maven如下:
<!-- https://mvnrepository.com/artifact/cn.easyproject/orai18n -->
<dependency>
<groupId>cn.easyproject</groupId>
<artifactId>orai18n</artifactId>
<version>12.1.0.2.0</version>
</dependency>
其次,調用存儲過程如果需要傳入數組參數的時候,需要創建type:
CREATE OR REPLACE TYPE ARRAY AS TABLE OF VARCHAR2
ARRAY 是數組的聲明
VARCHAR2是數組具體內容的聲明
下面貼上測試代碼,引用了網上的工具類,一起貼上:
package com.com.com.com;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCUtils {
private static String driver ="";
private static String url = "";
private static String user = "";
private static String password = "";
//1.註冊數據庫驅動。通過靜態代碼塊來編寫
static{
try {
//反射
Class.forName(driver);
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
}
//2.獲取數據庫連接
public static Connection getConnection(){
try {
return DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
//3.獲取數據庫連接對象
public static Statement getStatement() {
Statement st = null;
try {
st = getConnection().createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
return st;
}
//6.關閉資源
public static void release(Connection conn, Statement st, ResultSet rs){
//關閉數據庫連接資源
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//關閉數據庫連接對象資源
if(st != null){
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//關閉查詢結果資源
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
package com.com.com.com;
import java.sql.CallableStatement;
import oracle.jdbc.OracleConnection;
import oracle.jdbc.OracleTypes;
import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;
public class TestProcedure {
public static void main(String[] args) {
OracleConnection con = null;
String execuSql = "{call H2.CPROC_GET_SALEINFO(?,?,?,?,?)}";
CallableStatement call = null;
String[] a = new String[]{"1"};
String[] b = new String[]{"2"};
try {
con = (OracleConnection) JDBCUtils.getConnection();
call = con.prepareCall(execuSql);
ArrayDescriptor desc1 = ArrayDescriptor.createDescriptor("H2.VARCHAR2_ARRAY", con);
ARRAY vArray = new ARRAY(desc1, con, a);
ARRAY vArray2 = new ARRAY(desc1, con, b);
call.setArray(1, vArray);
call.setArray(2, vArray2);
call.setString(3, "2");
call.setString(4, "1");
call.registerOutParameter(5, OracleTypes.VARCHAR);
call.execute();
System.out.println(call.getMoreResults());
System.out.println(call.getString(5));
} catch (Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.release(con,call,null);
}
}
}
我主要被oracle接收參數卡了大半天,寫出這個博客也是想大家能夠少走彎路!