如何在java中处理oracle中的Index-by表类型?
在java中处理oracle中的Index-by表类型:
注意:这种处理方式必须要在oci连接模式下才能进行!
sp文件:
createtabletesttab(col1number);
createorreplacepackagedemo_pckas
typenumbers_tistableofnumberindexbybinary_integer;
end;
createorreplaceprocedureprocin(p1indemo_pck.numbers_t)is
begin
foriinp1.FIRST..p1.LASTloop
insertintotesttabvalues(i);
endloop;
end;
createorreplaceprocedureprocout(p1outdemo_pck.numbers_t)is
begin
p1(1):=4;
p1(2):=5;
p1(3):=6;
end;
createorreplaceprocedureprocinout(p1inoutdemo_pck.numbers_t)is
begin
foriinp1.FIRST..p1.LASTloop
p1(i):=p1(i)+6;
endloop;
end;
createorreplacefunctionfuncnonereturndemo_pck.numbers_tis
ndemo_pck.numbers_t;
begin
n(1):=10;
n(2):=11;
n(3):=12;
returnn;
end;
createorreplacepackagedemo_pckas
typenumbers_tistableofnumberindexbybinary_integer;
end;
createorreplaceprocedureprocin(p1indemo_pck.numbers_t)is
begin
foriinp1.FIRST..p1.LASTloop
insertintotesttabvalues(i);
endloop;
end;
createorreplaceprocedureprocout(p1outdemo_pck.numbers_t)is
begin
p1(1):=4;
p1(2):=5;
p1(3):=6;
end;
createorreplaceprocedureprocinout(p1inoutdemo_pck.numbers_t)is
begin
foriinp1.FIRST..p1.LASTloop
p1(i):=p1(i)+6;
endloop;
end;
createorreplacefunctionfuncnonereturndemo_pck.numbers_tis
ndemo_pck.numbers_t;
begin
n(1):=10;
n(2):=11;
n(3):=12;
returnn;
end;
java文件:
/*
*ThissampleshowshowtoaccessPL/SQLindex-bytablefromJDBC.
*/
importjava.sql.*;
importjava.math.BigDecimal;
importoracle.sql.*;
importoracle.jdbc.driver.*;
classIndexTableExample{
publicstaticvoidmain(Stringargs[])throwsSQLException,ClassNotFoundException{
//Loadthedriver
DriverManager.registerDriver(neworacle.jdbc.driver.OracleDriver());
Stringurl="jdbc:oracle:oci8:@avex14";
try{
Stringurl1=System.getProperty("JDBC_URL");
if(url1!=null)
url=url1;
}catch(Exceptione){
//Ifthereisanysecurityexception,ignoreit
//andusethedefault
}
//Connecttothedatabase
Connectionconn=DriverManager.getConnection(url,"scott","tiger");
func_test(conn);
procin_test(conn);
procinout_test(conn);
procout_test(conn);
//Closetheconnection.
conn.close();
}
/**
*Utilityfunctiontodumpthecontentsofthe"testtab"table
*/
staticvoiddumpTestTable(Connectionconn)throwsSQLException{
Statementstmt=conn.createStatement();
ResultSetrset=stmt.executeQuery("select*fromtesttab");
while(rset.next())
System.out.println("testtabRecord:"+rset.getInt(1));
stmt.execute("deletefromtesttab");
rset.close();
stmt.close();
}
/**
*BindaPL/SQLindex-bytableINparameter.
*/
staticvoidprocin_test(Connectionconn)throwsSQLException{
System.out.println("procin_test()");
//Preparethestatement
OracleCallableStatementprocin=(OracleCallableStatement)conn
.prepareCall("beginprocin(?);end;");
//index-bytablebindvalue
int[]values={1,2,3};
//maximumlengthoftheindex-bytablebindvalue.This
//valuedefinesthemaximumpossible"currentLen"forbatch
//updates.Forstandalonebinds,"maxLen"shouldbethe
//sameas"currentLen".
intmaxLen=values.length;
//actualsizeoftheindex-bytablebindvalue
intcurrentLen=values.length;
//index-bytableelementtype
intelemSqlType=OracleTypes.NUMBER;
//index-bytableelementlengthincasetheelementtype
//isCHAR,VARCHARorRAW.Thisvalueisignoredforother
//types.
intelemMaxLen=0;
//setthevalue
procin.setPlsqlIndexTable(1,values,maxLen,currentLen,elemSqlType,
elemMaxLen);
//executethecall
procin.execute();
//verifytheresult
dumpTestTable(conn);
//closethestatement
procin.close();
}
/**
*BindaPL/SQLindex-bytableOUTparameter,andaccessesthevalueusing
*JDBCdefaultmapping.
*/
staticvoidprocout_test(Connectionconn)throwsSQLException{
System.out.println("procout_test()");
OracleCallableStatementprocout=(OracleCallableStatement)conn
.prepareCall("beginprocout(?);end;");
//maximumlengthoftheindex-bytablebindvalue.This
//valuedefinesthemaximumtablesizeintheOUTparameter.
intmaxLen=10;
//index-bytableelementtype
intelemSqlType=OracleTypes.NUMBER;
//index-bytableelementlengthincasetheelementtype
//isCHAR,VARCHARorRAW.Thisvalueisignoredforother
//types
intelemMaxLen=0;
//registertheOUTparameter
procout.registerIndexTableOutParameter(1,maxLen,elemSqlType,
elemMaxLen);
//executethecall
procout.execute();
//accessthevalueusingJDBCdefaultmapping
BigDecimal[]values=(BigDecimal[])procout.getPlsqlIndexTable(1);
//printtheelements
for(inti=0;i<values.length;i++)
System.out.println(values[i].intValue());
//closethestatement
procout.close();
}
/**
*BindaPL/SQLindex-bytableIN/OUTparameter,andaccessthevalueusing
*OracleJDBCmapping(Datummapping).
*/
staticvoidprocinout_test(Connectionconn)throwsSQLException{
System.out.println("procinout_test()");
OracleCallableStatementprocinout=(OracleCallableStatement)conn
.prepareCall("beginprocinout(?);end;");
//index-bytableINbindvalue
int[]values={1,2,3};
//maximumlengthoftheindex-bytablebindvalue.This
//valuedefinesthemaximumpossible"currentLen"forbatch
//updates.Forstandalonebinds,"maxLen"shouldbethe
//sameas"currentLen".
intmaxLen=values.length;
//actualsizetobindindex-bytable
intcurrentLen=values.length;
*ThissampleshowshowtoaccessPL/SQLindex-bytablefromJDBC.
*/
importjava.sql.*;
importjava.math.BigDecimal;
importoracle.sql.*;
importoracle.jdbc.driver.*;
classIndexTableExample{
publicstaticvoidmain(Stringargs[])throwsSQLException,ClassNotFoundException{
//Loadthedriver
DriverManager.registerDriver(neworacle.jdbc.driver.OracleDriver());
Stringurl="jdbc:oracle:oci8:@avex14";
try{
Stringurl1=System.getProperty("JDBC_URL");
if(url1!=null)
url=url1;
}catch(Exceptione){
//Ifthereisanysecurityexception,ignoreit
//andusethedefault
}
//Connecttothedatabase
Connectionconn=DriverManager.getConnection(url,"scott","tiger");
func_test(conn);
procin_test(conn);
procinout_test(conn);
procout_test(conn);
//Closetheconnection.
conn.close();
}
/**
*Utilityfunctiontodumpthecontentsofthe"testtab"table
*/
staticvoiddumpTestTable(Connectionconn)throwsSQLException{
Statementstmt=conn.createStatement();
ResultSetrset=stmt.executeQuery("select*fromtesttab");
while(rset.next())
System.out.println("testtabRecord:"+rset.getInt(1));
stmt.execute("deletefromtesttab");
rset.close();
stmt.close();
}
/**
*BindaPL/SQLindex-bytableINparameter.
*/
staticvoidprocin_test(Connectionconn)throwsSQLException{
System.out.println("procin_test()");
//Preparethestatement
OracleCallableStatementprocin=(OracleCallableStatement)conn
.prepareCall("beginprocin(?);end;");
//index-bytablebindvalue
int[]values={1,2,3};
//maximumlengthoftheindex-bytablebindvalue.This
//valuedefinesthemaximumpossible"currentLen"forbatch
//updates.Forstandalonebinds,"maxLen"shouldbethe
//sameas"currentLen".
intmaxLen=values.length;
//actualsizeoftheindex-bytablebindvalue
intcurrentLen=values.length;
//index-bytableelementtype
intelemSqlType=OracleTypes.NUMBER;
//index-bytableelementlengthincasetheelementtype
//isCHAR,VARCHARorRAW.Thisvalueisignoredforother
//types.
intelemMaxLen=0;
//setthevalue
procin.setPlsqlIndexTable(1,values,maxLen,currentLen,elemSqlType,
elemMaxLen);
//executethecall
procin.execute();
//verifytheresult
dumpTestTable(conn);
//closethestatement
procin.close();
}
/**
*BindaPL/SQLindex-bytableOUTparameter,andaccessesthevalueusing
*JDBCdefaultmapping.
*/
staticvoidprocout_test(Connectionconn)throwsSQLException{
System.out.println("procout_test()");
OracleCallableStatementprocout=(OracleCallableStatement)conn
.prepareCall("beginprocout(?);end;");
//maximumlengthoftheindex-bytablebindvalue.This
//valuedefinesthemaximumtablesizeintheOUTparameter.
intmaxLen=10;
//index-bytableelementtype
intelemSqlType=OracleTypes.NUMBER;
//index-bytableelementlengthincasetheelementtype
//isCHAR,VARCHARorRAW.Thisvalueisignoredforother
//types
intelemMaxLen=0;
//registertheOUTparameter
procout.registerIndexTableOutParameter(1,maxLen,elemSqlType,
elemMaxLen);
//executethecall
procout.execute();
//accessthevalueusingJDBCdefaultmapping
BigDecimal[]values=(BigDecimal[])procout.getPlsqlIndexTable(1);
//printtheelements
for(inti=0;i<values.length;i++)
System.out.println(values[i].intValue());
//closethestatement
procout.close();
}
/**
*BindaPL/SQLindex-bytableIN/OUTparameter,andaccessthevalueusing
*OracleJDBCmapping(Datummapping).
*/
staticvoidprocinout_test(Connectionconn)throwsSQLException{
System.out.println("procinout_test()");
OracleCallableStatementprocinout=(OracleCallableStatement)conn
.prepareCall("beginprocinout(?);end;");
//index-bytableINbindvalue
int[]values={1,2,3};
//maximumlengthoftheindex-bytablebindvalue.This
//valuedefinesthemaximumpossible"currentLen"forbatch
//updates.Forstandalonebinds,"maxLen"shouldbethe
//sameas"currentLen".
intmaxLen=values.length;
//actualsizetobindindex-bytable
intcurrentLen=values.length;
本文地址:http://www.45fan.com/a/question/69769.html