论坛首页 入门技术论坛

JAVA调用ORACLE存储过程

浏览 2760 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-11-20   最后修改:2008-11-20
ORACLE代码
--创建一个包
CREATE OR REPLACE PACKAGE ZHL  AS
 TYPE Test_CURSOR IS REF CURSOR;
end ZHL;
--创建存储过程 返回一个游标
CREATE OR REPLACE PROCEDURE ZHL_PACKAGE(c_cur out ZHL.Test_CURSOR) AS
BEGIN
  OPEN c_cur for
    select * from zhl_table;
END;

JAVA代码
package com.zhl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class CURSORTEST {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String driver = "oracle.jdbc.driver.OracleDriver";
		String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521:fpk";
		Statement stmt = null;
		ResultSet rs = null;
		Connection conn = null;
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(strUrl, "zhl", "zhl");
			CallableStatement proc = null;
			proc = conn.prepareCall("{call ZHL_PACKAGE(?)}");
			proc.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
			proc.execute();
			rs = (ResultSet) proc.getObject(1);
			while(rs.next()){
				System.out.println(rs.getString(1)+","+rs.getString(2));
			}
		}
		catch (SQLException ex2) {
			ex2.printStackTrace();
		}
		catch (Exception ex2) {
			ex2.printStackTrace();
		}
		finally {
			try {
				if (rs != null) {
					rs.close();
					if (stmt != null) {
						stmt.close();
					}
					if (conn != null) {
						conn.close();
					}
				}
			}
			catch (SQLException ex1) {
			}
		}
	}
}
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics