java如何调用存储过程 代码如下,最好能解释下相关函数的作用,不胜感激
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java如何调用存储过程 代码如下,最好能解释下相关函数的作用,不胜感激相关的知识,希望对你有一定的参考价值。
CommonRecordSet recordSet = new CommonRecordSet();
Hashtable<Integer,ParamObject> param=new Hashtable<Integer,ParamObject>();
param.put(new Integer(1), new ParamObject("i_vSiteCode", DataTypes.VARCHAR, DataTypes.IN,siteID));
param.put(new Integer(2), new ParamObject("i_vSpecialty", DataTypes.VARCHAR, DataTypes.IN,specialtyID));
param.put(new Integer(3), new ParamObject("i_vRegionid", DataTypes.VARCHAR, DataTypes.IN,regionID));
param.put(new Integer(4), new ParamObject("i_iconfigType", DataTypes.VARCHAR, DataTypes.IN,type));
param.put(new Integer(5), new ParamObject("i_idipObject", DataTypes.VARCHAR, DataTypes.IN,dipObject));
param.put(new Integer(6), new ParamObject("i_imonObject", DataTypes.VARCHAR, DataTypes.IN,monObject));
param.put(new Integer(7), new ParamObject("i_inetName", DataTypes.VARCHAR, DataTypes.IN,netName));
param.put(new Integer(8), new ParamObject("i_isiteName", DataTypes.VARCHAR, DataTypes.IN,siteName));
param.put(new Integer(9), new ParamObject("i_ifindSpecialty", DataTypes.VARCHAR, DataTypes.IN,findSpecialty));
param.put(new Integer(10), new ParamObject("i_inetvendor", DataTypes.VARCHAR, DataTypes.IN,netvendor));
param.put(new Integer(11), new ParamObject("i_iequipType", DataTypes.VARCHAR, DataTypes.IN,equipType));
param.put(new Integer(12), new ParamObject("i_iPageRdNum", DataTypes.NUMBER, DataTypes.IN, new Integer(
pageSize))); //每页记录数
param.put(new Integer(13), new ParamObject("i_iPageNo", DataTypes.NUMBER, DataTypes.IN, new Integer(
pageIndex))); //页码
param.put(new Integer(14), new ParamObject("o_iocursor", DataTypes.CURSOR, DataTypes.INOUT, null)); //游标
param.put(new Integer(15), new ParamObject("o_iRecordNum", DataTypes.SMALLINT, DataTypes.OUT, null)); //总记录数
param.put(new Integer(16), new ParamObject("o_sRetVal", DataTypes.SMALLINT, DataTypes.OUT, null)); //1:成功、0:失败
CommonOperate commonOperate = new CommonOperate(); //调用BASE类
recordSet = commonOperate.queryProcedurePage("SVR_CP_NET_GET_DISPTACINFO", param, pageIndex, pageSize);//调用存储过程
问题已解决!主要是通过recordSet = commonOperate.queryProcedurePage("SVR_CP_NET_GET_DISPTACINFO", param, pageIndex, pageSize);来实现的,pageIndex为显示的页码,pageSize为每页显示的行数,不同的要求通过设置不同的pageIndex和pageSize来实现。感谢大家的帮助!
===================================================
55 java跟oracle 调用(存储过程,函数等)
55.1 Java调用无参的函数
1:函数为:
create or replace function MyF1 return varchar2 is
Result varchar2(20);
begin
dbms_output.put_line(\'now in My F1\');
Result := \'Now MyF1 return\';
return(Result);
end MyF1;
2:Java程序
/**
* 演示调用有一个没有参数的函数
* @throws Exception
*/
private static void t1() throws Exception
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:orcl", "test", "test");
try
CallableStatement stmt = conn
.prepareCall("?=call MyF1()");
stmt.registerOutParameter(1, Types.VARCHAR);
stmt.execute();
System.out.println(stmt.getString(1));
finally
conn.close();
55.2 Java调用无参但有返回值的存储过程
1:存储过程
create or replace procedure MyP1(str out Varchar2) is
begin
dbms_output.put_line(\'Hello Procedure.\');
str :=\'Haha,Hello Procedure\';
end MyP1;
2:程序
/**
* 如何调用无参但有返回值的存储过程 测试的存储过程
* @throws Exception
*/
private static void t2() throws Exception
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:orcl", "test", "test");
try
CallableStatement stmt = conn.prepareCall("call MyP1(?)");
// 注意,这里的stmt.getInt(1)中的数值1并非任意的,而是和存储过程中的out列对应的,
// 如果out是在第一个位置,那就是 stmt.getInt(1),如果是第三个位置,就是getInt.getInt(3),
// 当然也可以同时有多个返回值,那就是再多加几个out 参数了。
stmt.registerOutParameter(1, Types.VARCHAR);
stmt.execute();
System.out.println(stmt.getString(1));
finally
conn.close();
55.3 Java调用有参的(传入)函数
1:函数
create or replace function MyF2(a number,b varchar2) return varchar2 is
Result varchar2(50);
begin
dbms_output.put_line(\'a===\'||a||\',b==\'||b);
Result := a||b;
return(Result);
end MyF2;
2:程序
/**
* 调用有参的函数
* @throws Exception
*/
private static void t3() throws Exception
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:orcl", "test", "test");
try
CallableStatement stmt = conn
.prepareCall("?=call MyF2(?,?)");
stmt.setInt(2, 15);
stmt.setString(3, "HelloF2");
stmt.registerOutParameter(1, Types.VARCHAR);
stmt.execute();
System.out.println(stmt.getString(1));
finally
conn.close();
55.4 Java调用有参的(传入传出)存储过程
1:存储过程
create or replace procedure MyP2(a in number,b in varchar2,c out varchar2) is
begin
dbms_output.put_line(\'a==\'||a||\',b==\'||b);
c := \'ret==\'||a||\',b==\'||b;
end MyP2;
2:程序
/**
* 调用有参数和返回值的存储过程
* @throws Exception
*/
private static void t4() throws Exception
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:orcl", "test", "test");
try
CallableStatement stmt = conn.prepareCall("call MyP2(?,?,?)");
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();
ResultSet rs=(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 != null && objlist.size() > 0)
StructDescriptor structdesc = new StructDescriptor(OracleObj, con);
STRUCT[] structs = new STRUCT[objlist.size()];
Object[] result = new Object[0];
for (int i = 0; i < objlist.size(); i++)
result = new Object[2];//数组大小应和你定义的数据库对象(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;
参考技术A 真的忘饿了。。追问
可以说下你记得的,谢谢!
追答调用存储过程我记得的。
追问那说一下啊,方便的话,加q聊一下!
追答下面是调用上面存储过程的Java代码:
public static void setDeathAge(Poet dyingBard, int age) throws SQLException
Connection con = null;
CallableStatement proc = null;
try
con = connectionPool.getConnection();
proc = con.prepareCall(" call set_death_age(?, ?) ");
proc.setString(1, dyingBard.getName());
proc.setInt(2, age); proc.execute();
finally
try
proc.close();
catch (SQLException e)
con.close();
oracle 存储过程(分析理解)
我的理解 比较简单(仅供参考) 存储过程就相当于java里面的方法 简单讲就是一串代码能够实现某个特定的功能,想要使用这个方法直接调用方法名就能够使用他的功能,这就是方法
oracle 存储过程也相当于是写方法来调用 用图片的形式来解释 通俗易懂 有中文解释
1.无参存储过程 or replace一般不要写 仅供学习的时候写上 工作不要写
2.带参存储过程 多个参数用逗号隔开
3.定义变量以及赋值
4.打印语句
5.简单的操作
写一存储过程,打印1-n n是传入的参数 要用到循环
--创建一个存储过程 for 循环
CREATE OR replace PROCEDURE xunhuan(n NUMBER) AS
BEGIN
FOR a IN 1..n
LOOP
dbms_output.put_line(a);
END LOOP;
END;
--sql语句
DECLARE
n NUMBER:=15;
BEGIN
xunhuan(n);
END;
-- while 循环
CREATE PROCEDURE zzzz(n NUMBER)
AS
a NUMBER:=1;
BEGIN
WHILE a<=n
LOOP
dbms_output.put_line(a);
a:=a+1;
END LOOP;
END;
DECLARE
cc NUMBER:=15;
BEGIN
zzzz(cc);
END;
--结果
6.稍微难一点的
--写一存储过程,求m-n的累加和并返回
jiashu和jiash后面的in是入参的意思 就是写入参数,out是返回值(装返回内容的)
CREATE OR REPLACE PROCEDURE fangfa1
(
jiashu IN NUMBER ,
jiash IN NUMBER ,
he OUT NUMBER--返回结果
)
AS
v_sum NUMBER:=0;--装结果
BEGIN
FOR j IN jiashu..jiash
LOOP
v_sum:=v_sum+j;
END LOOP;
he:=v_sum;
END;
--调用
DECLARE
qq number:=1;
ww NUMBER:=100;
he number;--用来接收返回值的
BEGIN
fangfa1(qq,ww,he);
dbms_output.put_line(he);
end;
--简单的分享到这
以上是关于java如何调用存储过程 代码如下,最好能解释下相关函数的作用,不胜感激的主要内容,如果未能解决你的问题,请参考以下文章
PHP如何调用SQLServer2012的存储过程并获取返回结果集及出参?
delphi 三层架构如何调用oracle 存储过程,应该使用哪个部件,最好有实例