oracle bulk collects 之 java select
stmt.execute("CREATE TYPE EMPLOYEE AS OBJECT
(EmpName VARCHAR2(50),EmpNo INTEGER))");
stmt.execute("CREATE TYPE EMPLOYEE_LIST AS TABLE OF EMPLOYEE");
stmt.execute("CREATE TABLE EMPLOYEE_TABLE (DeptName VARCHAR2(20),
Employees EMPLOYEE_LIST) NESTED TABLE Employees STORE AS ntable1");
stmt.execute("INSERT INTO EMPLOYEE_TABLE VALUES ("SALES", EMPLOYEE_LIST
(EMPLOYEE('Susan Smith', 123), EMPLOYEE('Scott Tiger', 124)))");
If you want to retrieve all the employees belonging to the SALES department into an array of instances of the custom object class EmployeeObj, then you must add an entry to the type map to specify mapping between the EMPLOYEE SQL type and the EmployeeObj custom object class.
To do this, first create your statement and result set objects, then select the EMPLOYEE_LIST associated with the SALES department into the result set. Cast the result set to OracleResultSet so you can use the getARRAY method to retrieve the EMPLOYEE_LIST into an ARRAY object (employeeArray in the following example).
The EmployeeObj custom object class in this example implements the SQLData interface.
Statement s = conn.createStatement();
OracleResultSet rs = (OracleResultSet)s.executeQuery
("SELECT Employees FROM employee_table WHERE DeptName = 'SALES'");
// get the array object
ARRAY employeeArray = ((OracleResultSet)rs).getARRAY(1);
Now that you have the EMPLOYEE_LIST object, get the existing type map and add an entry that maps the EMPLOYEE SQL type to the EmployeeObj Java type.
// add type map entry to map SQL type
// "EMPLOYEE" to Java type "EmployeeObj"
Map map = conn.getTypeMap();
map.put("EMPLOYEE", Class.forName("EmployeeObj"));
Next, retrieve the SQL EMPLOYEE objects from the EMPLOYEE_LIST. To do this, call the getArray method of the employeeArray array object. This method returns an array of objects. The getArray method returns the EMPLOYEE objects into the employees object array.
// Retrieve array elements
Object[] employees = (Object[]) employeeArray.getArray();
Finally, create a loop to assign each of the EMPLOYEE SQL objects to the EmployeeObj Java object emp.
// Each array element is mapped to EmployeeObj object.
for (int i=0; i
EmployeeObj emp = (EmployeeObj) employees[i];
...
}
---------------------------------------------------------------------------------------------------------------------
create or replace type varchar_number as object (
v varchar2(10),
i number
);
/
The nested-table type using varchar_number as elements is created: create or replace type t_varchar_number as table of varchar_number;
/
A table containing some values: create table vn (
v varchar2(10),
i number
);
insert into vn values ('uno',1);
insert into vn values ('due',2);
insert into vn values ('tre',3);
commit;
The following function bulk collects the values of the table vn and returns it as a collection. create or replace function fct_t return t_varchar_number as
ret t_varchar_number;
begin
select varchar_number(v,i) bulk collect into ret from vn;
return ret;
end;
/
The function in action set serveroutput on size 10000
declare
x t_varchar_number;
begin
x := fct_t;
for r in (select * from table(cast(x as t_varchar_number))) loop
dbms_output.put_line(r.v || ', ' || r.i);
end loop;
end;
/
uno, 1
due, 2
tre, 3
Cleaning up... drop type t_varchar_number;
drop type varchar_number;
drop function fct_t;
drop table vn;
------------------------------------------------------------------------------------------------------
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/197458/viewspace-1058833/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 使用oracle bulk collect 提高select FETCH LOOP的速度OracleOOP
- Oracle 的 bulk collect用法Oracle
- Oracle資料庫的BULK COLLECT用法之批量增刪改<轉>Oracle資料庫
- 使用Bulk Collect提高Oracle查詢效率Oracle
- Oracle table selectOracle
- oracle select for updateOracle
- 多行資料的批處理之bulk collect
- Oracle Create Table as SelectOracle
- mysql之select into outfileMySql
- Oracle中巧用bulk collect實現cursor批次fetchOracle
- Oracle PL/SQL 優化與調整 -- Bulk 說明OracleSQL優化
- [Oracle Script] select db parameterOracle
- [20180110]Oracle Bulk Collect and LimitOracleMIT
- oracle bulk Insert 比insert 快不是一點啊.Oracle
- oracle中bulk collect into用法 (批次效率提取遊標資料)Oracle
- Elasticsearch——mget及bulkElasticsearch
- oracle10g_plsql_rercursor_type_table of_小引例_bulk collect intoOracleSQL
- Oracle中select ... for update的用法Oracle
- oracle java 之jrockit_jdkOracleJavaJDK
- Elasticsearch的Bulk API使用ElasticsearchAPI
- [20180111]Oracle Bulk Collect and Limit2OracleMIT
- Oracle DML(非select) 操作不commit 對select的影響OracleMIT
- sqlserver bulk insert報錯Cannot bulk load because the file could not be opened.SQLServer
- ORACLE SELECT INTO NO_DATA_FOUND問題Oracle
- Oracle SQL select練習語句OracleSQL
- oracle connection,select,update 工作原理Oracle
- jQuery原始碼分析之select()方法jQuery原始碼
- mongodb批量操作, bulk_write,MongoDB
- Bulk Insert命令具體
- 【PL/SQL】初試 bulk collectSQL
- dbms_sql之通用general及bulk批量(大體積)type 型別SQL型別
- Oracle 樹操作(select…start with…connect by…prior)Oracle
- oracle SELECT INTO 和 INSERT INTO SELECT 兩種表複製語句詳解Oracle
- select、poll、epoll之間的區別
- 前端外掛之Select2使用前端
- AlloyTouch之select選擇外掛
- mysql innodb之select for update nowaitMySqlAI
- SQL入門之1 select 聯接SQL