java云存储代码 java云服务( 二 )


stmt.setInt(1, 5);
stmt.setString(2, "测试");
stmt.registerOutParameter(3, Types.VARCHAR);
stmt.execute();
System.out.println(stmt.getString(3));
} finally {
conn.close();
}
}
55.5 Java向存储过程传入传出对象的数组
1:在数据中创建对象
create or replace type UserModel as object(
uuid varchar2(20),
name varchar2(20)
);
2:在数据库中建立对象的集合类型
create or replace type userCol as table of UserModel;
create or replace type retUserCol as table of UserModel;
3:在数据库中建立包
包头:
create or replace package MyTestPackage is
TYPE dbRs IS REF CURSOR;
procedure MyP3(a1 in userCol,a2 out dbRs);
end MyTestPackage;
包体:
create or replace package body MyTestPackage is
procedure MyP3(a1 in userCol,a2 out dbRs) as
umCol retUserCol := retUserCol();
begin
for i in 1.. a1.count loop
insert into tbl_test values (a1(i).uuid,a1(i).name);
end loop;
commit;
umCol.Extend;
umCol(1):=UserModel('retUuid11','retName11');
umCol.Extend;
umCol(2):=UserModel('retUuid22','retName22');
open a2 for select * from table(cast(umCol as retUserCol));
end;
begin
null;
end MyTestPackage;
4:程序:
/**
* 测试向pl/sql传入对象集合,从pl/sql返回任意的对象的集合
* @param list
* @throws Exception
*/
private static void t5(List list) throws Exception {
CallableStatement stmt = null;
Connection con = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:orcl", "test", "test");
if (con != null) {
ARRAY aArray = getArray(con, "USERMODEL", "USERCOL", list);//该函数调用的第二三个参数必须大写
stmt = con.prepareCall("{call MyTestPackage.MyP3(?,?)}");
((OracleCallableStatement) stmt).setARRAY(1, aArray);
stmt.registerOutParameter(2, OracleTypes.CURSOR);
stmt.execute();
ResultSetrs=(ResultSet)stmt.getObject(2);
while(rs.next()){
String uuid = rs.getString("uuid");
String name = rs.getString("name");
System.out.println("the uuid="+uuid+",name="+name);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private static ARRAY getArray(Connection con, String OracleObj, String Oraclelist,
List objlist) throws Exception {
ARRAY list = null;
if (objlist != nullobjlist.size()0) {
【java云存储代码 java云服务】StructDescriptor structdesc = new StructDescriptor(OracleObj, con);
STRUCT[] structs = new STRUCT[objlist.size()];
Object[] result = new Object[0];
for (int i = 0; iobjlist.size(); i++) {
result = new Object[2];//数组大小应和java云存储代码你定义的数据库对象(UserModel)的属性的个数
result[0] = ((UserModel)(objlist.get(i))).getUuid(); //将list中元素的数据传入result数组
result[1] = ((UserModel)(objlist.get(i))).getName(); //
structs[i] = new STRUCT(structdesc, con, result);
}
ArrayDescriptor desc = ArrayDescriptor.createDescriptor(Oraclelist,
con);
list = new ARRAY(desc, con, structs);
}
return list;
}
如果使用Tomcat的DBCP的连接池,需要把连接进行转换
public Connection getNativeConnection(Connection con) throws SQLException {
if (con instanceof DelegatingConnection) {
Connection nativeCon = ((DelegatingConnection) con).getInnermostDelegate();
return (nativeCon != null ? nativeCon : con.getMetaData().getConnection());
}
return con;
}

推荐阅读