JAVA调用ORACLE存储过程

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) {
			}
		}
	}
}

你可能感兴趣的:(java,oracle,sql,c,jdbc)