如何用java应用程序调用oracle的java存储过程?

zhugang 2003-12-07 01:03:48
如何用java应用程序调用oracle的java存储过程?
...全文
146 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lzlspb 2003-12-25
  • 打赏
  • 举报
回复
Calling PL/SQL Stored Procedures
PL/SQL stored procedures are called from within JDBC programs by means of the prepareCall() method of the Connection object created above. A call to this method takes variable bind parameters as input parameters as well as output variables and creates an object instance of the CallableStatement class.

The following line of code illustrates this:

CallableStatement stproc_stmt = conn.prepareCall
("{call procname(?,?,?)}");
Here conn is an instance of the Connection class.

The input parameters are bound to this object instance using the setXXX() methods on the CallableStatement object. For each input bind parameter, a setXXX() method (e.g., setInt(), setString(),) is called. The following line of code illustrates this:

stproc_stmt.setXXX(...)
The output parameters are bound to this object instance using registerOutParameter() method on the CallableStatement object, as shown below:

stproc_stmt.registerOutParameter(2, OracleTypes.CHAR);
The above statement registers the second parameter passed to the stored procedure as an OUT parameter of type CHAR. For each OUT parameter, a registerOutParameter() method is called.

Once a CallableStatement object has been constructed, the next step is to execute the associated stored procedure or function. This is done by using the executeUpdate() method of the CallableStatement object. The following line of code illustrates this using the stproc_stmt object created above:

stproc_stmt.executeUpdate();
prepareCall() Method

The three different kinds of stored sub-programs, namely, stored procedures, stored functions, and packaged procedures and functions can be called using the prepareCall() method of the CallableStatement object.

The syntax for calling stored functions is as follows:

CallableStatement stproc_stmt = conn.prepareCall
("{ ? = call _funcname(?,?,?)}");
The first ? refers to the return value of the function and is also to be registered as an OUT parameter.

Packaged Procedures and Functions

Packaged procedures and functions can be called in the same manner as stored procedures or functions except that the name of the package followed a dot "." prefixes the name of the procedure or function.

Once the stored procedure or function has been executed, the values of the out parameters can be obtained using the getXXX() methods (for example, getInt() and getString()) on the CallableStatement object. This is shown below:

String op1 stproc_stmt.getString(2);
This retrieves the value returned by the second parameter (which is an OUT parameter of the corresponding PL/SQL stored procedure being called and has been registered as an OUT parameter in the JDBC program) into the Java String variable op1.

A complete example is shown below. Consider a procedure that returns the highest paid employee in a particular department. Specifically, this procedure takes a deptno as input and returns empno, ename, and sal in the form of three out parameters.

The procedure is created as follows:

CREATE OR REPLACE PROCEDURE p_highest_paid_emp
(ip_deptno NUMBER,
op_empno OUT NUMBER,
op_ename OUT VARCHAR2,
op_sal OUT NUMBER)
IS
v_empno NUMBER;
v_ename VARCHAR2(20);
v_sal NUMBER;
BEGIN
SELECT empno, ename, sal
INTO v_empno, v_ename, v_sal
FROM emp e1
WHERE sal = (SELECT MAX(e2.sal)
FROM emp e2
WHERE e2.deptno = e1.deptno
AND e2.deptno = ip_deptno)
AND deptno = ip_deptno;
op_empno := v_empno;
op_ename := v_ename;
op_sal := v_sal;
END;
/
Here we assume that there is only one highest paid employee in a particular department.

Next we write the JDBC program that calls this procedure. This is shown below:

import java.sql.*;

public class StProcExample {
public static void main(String[] args)
throws SQLException {
int ret_code;
Connection conn = null;
try {
//Load and register Oracle driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
//Establish a connection

conn = DriverManager.getConnection("jdbc:oracle:thin:@training:1521:
Oracle", "oratest", "oratest");
int i_deptno = 10;
CallableStatement pstmt = conn.prepareCall("{call p_highest_
paid_emp(?,?,?,?)}");
pstmt.setInt(1, i_deptno);
pstmt.registerOutParameter(2, Types.INTEGER);
pstmt.registerOutParameter(3, Types.VARCHAR);
pstmt.registerOutParameter(4, Types.FLOAT);
pstmt.executeUpdate();

int o_empno = pstmt.getInt(2);
String o_ename = pstmt.getString(3);
float o_sal = pstmt.getFloat(4);
System.out.print("The highest paid employee in dept "
+i_deptno+" is: "+o_empno+" "+o_ename+" "+o_sal);
pstmt.close();
conn.close();
} catch (SQLException e) {ret_code = e.getErrorCode();
System.err.println(ret_code + e.getMessage()); conn.close();}
}
}
Calling Java Stored Procedures
Java stored procedures can also be called from JDBC programs using the corresponding call specifications created to publish the Java methods into the Oracle 8i database. In other words, calling the published call specs executes the corresponding Java methods and the syntax for calling these is the same as calling PL/SQL stored procedures.

Here we will use the Java stored procedures created in Chapter 2, "Java Stored Procedures." The following JDBC program calls the packaged procedure pkg_empmaster.fire_emp (a Java stored procedure that corresponds to the Java method empMaster.fireEmp()). Specifically it deletes the record in emp table where empno = 1002.

Before executing the above Java stored procedure, the record corresponding to empno 1002 in emp table is as follows:

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
1002 DAVID ANALYST 1001 01-JAN-01 6000 1000 10
The JDBC program to call the Java stored procedure is as follows:

import java.sql.*;
public class JavaProcExample {
public static void main(String[] args)
throws SQLException {
int ret_code;
Connection conn = null;
try {
//Load and register Oracle driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
//Establish a connection

conn = DriverManager.getConnection("jdbc:oracle:thin:@training:1521:
Oracle", "oratest", "oratest");
int i_empno = 1002;
CallableStatement pstmt =
conn.prepareCall("{call pkg_empmaster.fire_emp(?)}");
pstmt.setInt(1, i_empno);
pstmt.executeUpdate();

pstmt.close();
conn.close();
} catch (SQLException e) {ret_code = e.getErrorCode();
System.err.println(ret_code + e.getMessage()); conn.close();}
}
}
The output of the above program can be verified as follows:

SQL> select * from emp where empno = 1002;

no rows selected

SQL>
ljzcq 2003-12-24
  • 打赏
  • 举报
回复
up
zhugang 2003-12-19
  • 打赏
  • 举报
回复
up
liuyi8903 2003-12-07
  • 打赏
  • 举报
回复
:)
leecooper0918 2003-12-07
  • 打赏
  • 举报
回复
如果 RE_CURSOR 定义为IN OUT T_CURSOR, 就不用再定义v_cursor了.
leecooper0918 2003-12-07
  • 打赏
  • 举报
回复

楼上贴的缺了 ref cursor 的定义和procedure 的定义:

CREATE OR REPLACE PACKAGE MyTest
as
type T_CUSOR is ref cursor;

PROCEDURE zhbtest(P_CUSTOMER_ID c_well.wellno %TYPE,
Re_CURSOR OUT T_CURSOR);
end;
/
liuyi8903 2003-12-07
  • 打赏
  • 举报
回复
返回游标:
CREATE OR REPLACE PACKAGE BODY MyTest
IS

PROCEDURE zhbtest(P_CUSTOMER_ID c_well.wellno %TYPE, Re_CURSOR OUT T_CURSOR)
IS
V_CURSOR T_CURSOR;
BEGIN
OPEN V_CURSOR FOR
select wellname from c_well ;
Re_CURSOR := V_CURSOR;
END;
END;


public class Protest {
private static Connection conn = null;
private static oracle.jdbc.OracleCallableStatement call = null;
private static ResultSet rs = null;
private static String url = "jdbc:oracle:thin:@192.168.100.145:1521:kdc";
private static String name = "liuyi";
private static int cout = 0;

public static void main(String[] args){
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url,"kdcerp2","123");
call = (oracle.jdbc.OracleCallableStatement)conn.prepareCall("{call mytest.zhbtest(?,?)}");
call.setString(1, "4050608006");
call.registerOutParameter(2,oracle.jdbc.OracleTypes.CURSOR);
call.execute();
rs = call.getCursor(2);
while(rs.next()){
System.out.println(rs.getString(1));
cout++;
}
System.out.println(cout);
}catch(java.lang.ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
System.out.println(e.toString());
}
}
}

随着元宇宙概念兴起与虚拟数字人技术快速发展,虚拟主播、在线教育虚拟助教、元宇宙社交等应用场景对实时动作表情驱动系统提出更高需求。当前主流动作捕捉方案多依赖专业光学动捕设备或深度相机,设备成本高昂、部署复杂,难以在普通用户场景普及。本研究设计并实现一套基于普通单目摄像头的虚拟数字人实时动作表情驱动系统,旨在降低数字人内容生产门槛。系统采用Unity作为主开发引擎,结合Blender进行数字人模型构建与BlendShape表情系统制作,使用MediaPipe框架实现人脸关键点检测与人体姿态估计,通过C#脚本完成实时数据处理与渲染控制。系统包含五个关键模块:人脸关键点检测(MediaPipe Face Mesh提取468个关键点)、人体姿态估计(MediaPipe Pose识别33个骨骼关键点)、表情迁移映射、动作平滑优化(卡尔曼滤波与贝塞尔曲线插值消除抖动)、实时渲染(60fps流畅渲染,支持直播推流与视频录制)。实验结果表明,系统在普通笔记本上可实现60fps实时驱动,人脸关键点检测准确率达98.7%,人体姿态估计平均误差小于5像素,表情迁移相似度达92.3%。 【课程报告内容】 摘要 第1章 绪论 第2章 相关技术与理论基础 第3章 系统架构设计 第4章 关键模块设计与实现 第5章 实验与结果分析 第6章 总结与展望 参考文献
内容概要:本文提出了一种基于蒙特卡洛模拟与拉格朗日松弛法的分散式优化策略,用于解决电动汽车充电站在分时电价机制下的有序充电调度问题。通过蒙特卡洛方法模拟电动汽车充电行为的随机性,生成多样化负荷场景,构建以降低电网负荷波动和用户充电成本为目标的优化模型;采用拉格朗日松弛法对耦合约束进行解耦,实现分布式求解,在保障用户隐私的同时提升计算效率。文中设计了详细的仿真场景,系统分析了该调度策略对电网负荷特性和用户经济性的影响,验证了所提方法在实际应用中的有效性与可行性。; 适合人群:具备电力系统运行、优化算法理论及Matlab编程能力的研究生、科研人员以及从事智能电网与电动汽车相关领域的工程技术人员。; 使用场景及目标:①应用于城市规模化充电站集群的有序充电管理,实现电网“削峰填谷”;②为电力公司制定科学合理的分时电价政策提供量化分析工具;③支撑智能电网中需求侧资源的分布式协同优化调度研究与系统设计。; 阅读建议:建议结合提供的Matlab代码深入理解算法实现流程,重点掌握蒙特卡洛场景生成机制与拉格朗日松弛迭代求解过程,可通过调整参数设置复现不同工况下的仿真结果,以深化对分散式优化机制与调度效果之间关系的理解。
内容概要:本文研究了基于阶跃响应的V-Tiger自动增益调整PID控制器优化方法,并提供了完整的Matlab代码实现。通过深入分析PID控制的核心性能指标与V-Tiger控制器的动态特性,提出了一种融合阶跃响应特征提取与多目标协同优化的自动整定方案,设计了具备自适应迭代校正能力的优化机制,有效提升了控制系统的响应速度、稳定性和抗干扰能力。文中系统阐述了整定原理、算法架构设计及性能验证流程,通过仿真实验充分验证了该方法在复杂工业控制场景下实现高精度参数自整定的可行性与优越性,为智能PID控制提供了可复现、可拓展的技术路径。; 适合人群:具备自动控制理论基础和Matlab编程能力,从事控制工程、自动化、电气工程等领域研究的研发人员及高校研究生。; 使用场景及目标:①应用于需要高精度PID参数整定的工业控制系统中,如电机驱动、温度控制、电力电子变换器等;②为科研人员提供一种可复现、可扩展的智能PID整定方法,用于提升系统动态性能与鲁棒性;③作为教学案例帮助学生理解PID整定原理与现代优化算法的融合应用。; 阅读建议:建议读者结合文中的Matlab代码逐模块运行与调试,重点关注阶跃响应特征提取与增益优化策略的实现逻辑,同时可尝试将其应用于实际控制系统中进行对比验证,以深化对自动整定机制的理解。

17,134

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧