java下实现调用oracle的存储过程和函数

在oracle下创建一个test的账户,然后

1.创建表:STOCK_PRICES

 

  1. --创建表格 
  2. CREATE TABLE STOCK_PRICES( 
  3.     RIC VARCHAR(6) PRIMARY KEY, 
  4.     PRICE NUMBER(7,2), 
  5.     UPDATED DATE ); 
--创建表格
CREATE TABLE STOCK_PRICES(
    RIC VARCHAR(6) PRIMARY KEY,
    PRICE NUMBER(7,2),
    UPDATED DATE );

 

2.插入测试数据:

 

  1. --插入数据 
  2. INSERTINTO stock_prices values('1111',1.0,SYSDATE); 
  3. INSERTINTO stock_prices values('1112',2.0,SYSDATE); 
  4. INSERTINTO stock_prices values('1113',3.0,SYSDATE); 
  5. INSERTINTO stock_prices values('1114',4.0,SYSDATE); 
--插入数据
INSERT INTO stock_prices values('1111',1.0,SYSDATE);
INSERT INTO stock_prices values('1112',2.0,SYSDATE);
INSERT INTO stock_prices values('1113',3.0,SYSDATE);
INSERT INTO stock_prices values('1114',4.0,SYSDATE);

 

3.建立一个返回游标:

PKG_PUB_UTILS

 

  1. --建立一个返回游标 
  2. CREATE OR REPLACE PACKAGE PKG_PUB_UTILS IS 
  3.     --动态游标 
  4.     TYPE REFCURSOR IS REF CURSOR; 
  5. END PKG_PUB_UTILS; 
--建立一个返回游标
CREATE OR REPLACE PACKAGE PKG_PUB_UTILS IS
    --动态游标
    TYPE REFCURSOR IS REF CURSOR;
END PKG_PUB_UTILS;

4.创建和存储过程:P_GET_PRICE

 

 

  1. --创建存储过程 
  2. CREATEORREPLACEPROCEDURE P_GET_PRICE 
  3.   AN_O_RET_CODE OUT NUMBER, 
  4.   AC_O_RET_MSG  OUT VARCHAR2, 
  5.   CUR_RET OUT PKG_PUB_UTILS.REFCURSOR, 
  6.   AN_I_PRICE IN NUMBER 
  7. )  
  8. IS 
  9. BEGIN 
  10.     AN_O_RET_CODE := 0; 
  11.     AC_O_RET_MSG  := '操作成功'
  12.      
  13.     OPEN CUR_RET FOR 
  14.         SELECT * FROM STOCK_PRICES WHERE PRICE<AN_I_PRICE; 
  15. EXCEPTION 
  16.     WHEN OTHERS THEN 
  17.         AN_O_RET_CODE := -1; 
  18.         AC_O_RET_MSG  := '错误代码:' || SQLCODE || CHR(13) || '错误信息:' || SQLERRM; 
  19. END P_GET_PRICE; 
--创建存储过程
CREATE OR REPLACE PROCEDURE P_GET_PRICE
(
  AN_O_RET_CODE OUT NUMBER,
  AC_O_RET_MSG  OUT VARCHAR2,
  CUR_RET OUT PKG_PUB_UTILS.REFCURSOR,
  AN_I_PRICE IN NUMBER
) 
IS
BEGIN
    AN_O_RET_CODE := 0;
    AC_O_RET_MSG  := '操作成功';
    
    OPEN CUR_RET FOR
        SELECT * FROM STOCK_PRICES WHERE PRICE<AN_I_PRICE;
EXCEPTION
    WHEN OTHERS THEN
        AN_O_RET_CODE := -1;
        AC_O_RET_MSG  := '错误代码:' || SQLCODE || CHR(13) || '错误信息:' || SQLERRM;
END P_GET_PRICE;

5.创建函数:

 

  1. --创建函数:F_GET_PRICE 
  2. CREATE OR REPLACE FUNCTION F_GET_PRICE(v_price IN NUMBER) 
  3.     RETURN PKG_PUB_UTILS.REFCURSOR 
  4. AS 
  5.     stock_cursor PKG_PUB_UTILS.REFCURSOR; 
  6. BEGIN 
  7.     OPEN stock_cursor FOR 
  8.     SELECT * FROM stock_prices WHERE price < v_price; 
  9.     RETURN stock_cursor; 
  10. END; 
--创建函数:F_GET_PRICE
CREATE OR REPLACE FUNCTION F_GET_PRICE(v_price IN NUMBER)
    RETURN PKG_PUB_UTILS.REFCURSOR
AS
    stock_cursor PKG_PUB_UTILS.REFCURSOR;
BEGIN
    OPEN stock_cursor FOR
    SELECT * FROM stock_prices WHERE price < v_price;
    RETURN stock_cursor;
END;

6.JAVA调用存储过程返回结果集

JDBCoracle10G_INVOKEPROCEDURE.java

  1. import java.sql.*; 
  2. import oracle.jdbc.OracleCallableStatement; 
  3. import oracle.jdbc.OracleTypes; 
  4.  
  5. /* 本例是通过调用oracle的存储过程来返回结果集:
  6. * oracle 9i、10G 的jdbc由1个jar包组成:classes12.zip
  7. */ 
  8. publicclass JDBCoracle10G_INVOKEPROCEDURE { 
  9.     Connection conn = null
  10.     Statement statement = null
  11.     ResultSet rs = null
  12.     CallableStatement stmt = null
  13.  
  14.     String driver; 
  15.     String url; 
  16.     String user; 
  17.     String pwd; 
  18.     String sql; 
  19.     String in_price; 
  20.  
  21.     public JDBCoracle10G_INVOKEPROCEDURE()  
  22.     { 
  23.         driver = "oracle.jdbc.driver.OracleDriver"
  24.         url = "jdbc:oracle:thin:@localhost:1521:ORCL"
  25.         // oracle 用户 
  26.         user = "test"
  27.         // oracle 密码 
  28.         pwd = "test"
  29.         init(); 
  30.         // mysid:必须为要连接机器的sid名称,否则会包以下错: 
  31.         // java.sql.SQLException: Io 异常: Connection 
  32.         // refused(DESCRIPTION=(TMP=)(VSNNUM=169870080)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4)))) 
  33.         // 参考连接方式: 
  34.         // Class.forName( "oracle.jdbc.driver.OracleDriver" ); 
  35.         // cn = DriverManager.getConnection( 
  36.         // "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd ); 
  37.  
  38.     } 
  39.  
  40.     publicvoid init() { 
  41.         System.out.println("oracle jdbc test"); 
  42.         try
  43.             Class.forName(driver); 
  44.             System.out.println("driver is ok"); 
  45.             conn = DriverManager.getConnection(url, user, pwd); 
  46.             System.out.println("conection is ok"); 
  47.             statement = conn.createStatement(); 
  48.             // conn.setAutoCommit(false); 
  49.             // 输入参数 
  50.             in_price = "3.0"
  51.             // 调用函数 
  52.             stmt = conn.prepareCall("call P_GET_PRICE(?,?,?,?)"); 
  53.             stmt.registerOutParameter(1, java.sql.Types.FLOAT); 
  54.             stmt.registerOutParameter(2, java.sql.Types.CHAR); 
  55.             stmt.registerOutParameter(3, oracle.jdbc.OracleTypes.CURSOR); 
  56.             stmt.setString(4, in_price); 
  57.             stmt.executeUpdate(); 
  58.             int retCode = stmt.getInt(1); 
  59.             String retMsg = stmt.getString(2); 
  60.             if (retCode == -1) { // 如果出错时,返回错误信息 
  61.                 System.out.println("报错!"); 
  62.             } else
  63.                 // 取的结果集的方式一: 
  64.                 rs = ((OracleCallableStatement) stmt).getCursor(3); 
  65.                 // 取的结果集的方式二: 
  66.                 // rs = (ResultSet) stmt.getObject(3); 
  67.                 String ric; 
  68.                 String price; 
  69.                 String updated; 
  70.                 // 对结果进行输出 
  71.                 while (rs.next()) { 
  72.                     ric = rs.getString(1); 
  73.                     price = rs.getString(2); 
  74.                     updated = rs.getString(3); 
  75.                     System.out.println("ric:" + ric + ";-- price:" + price 
  76.                             + "; --" + updated + "; "); 
  77.                 } 
  78.             } 
  79.  
  80.         } catch (Exception e) { 
  81.             e.printStackTrace(); 
  82.         } finally
  83.             System.out.println("close "); 
  84.         } 
  85.     } 
  86.  
  87.     publicstaticvoid main(String args[])// 自己替换[] 
  88.     { 
  89.         new JDBCoracle10G_INVOKEPROCEDURE(); 
  90.     } 
import java.sql.*;
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleTypes;

/* 本例是通过调用oracle的存储过程来返回结果集:
 * oracle 9i、10G 的jdbc由1个jar包组成:classes12.zip
 */
public class JDBCoracle10G_INVOKEPROCEDURE {
	Connection conn = null;
	Statement statement = null;
	ResultSet rs = null;
	CallableStatement stmt = null;

	String driver;
	String url;
	String user;
	String pwd;
	String sql;
	String in_price;

	public JDBCoracle10G_INVOKEPROCEDURE() 
	{
		driver = "oracle.jdbc.driver.OracleDriver";
		url = "jdbc:oracle:thin:@localhost:1521:ORCL";
		// oracle 用户
		user = "test";
		// oracle 密码
		pwd = "test";
		init();
		// mysid:必须为要连接机器的sid名称,否则会包以下错:
		// java.sql.SQLException: Io 异常: Connection
		// refused(DESCRIPTION=(TMP=)(VSNNUM=169870080)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4))))
		// 参考连接方式:
		// Class.forName( "oracle.jdbc.driver.OracleDriver" );
		// cn = DriverManager.getConnection(
		// "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd );

	}

	public void init() {
		System.out.println("oracle jdbc test");
		try {
			Class.forName(driver);
			System.out.println("driver is ok");
			conn = DriverManager.getConnection(url, user, pwd);
			System.out.println("conection is ok");
			statement = conn.createStatement();
			// conn.setAutoCommit(false);
			// 输入参数
			in_price = "3.0";
			// 调用函数
			stmt = conn.prepareCall("call P_GET_PRICE(?,?,?,?)");
			stmt.registerOutParameter(1, java.sql.Types.FLOAT);
			stmt.registerOutParameter(2, java.sql.Types.CHAR);
			stmt.registerOutParameter(3, oracle.jdbc.OracleTypes.CURSOR);
			stmt.setString(4, in_price);
			stmt.executeUpdate();
			int retCode = stmt.getInt(1);
			String retMsg = stmt.getString(2);
			if (retCode == -1) { // 如果出错时,返回错误信息
				System.out.println("报错!");
			} else {
				// 取的结果集的方式一:
				rs = ((OracleCallableStatement) stmt).getCursor(3);
				// 取的结果集的方式二:
				// rs = (ResultSet) stmt.getObject(3);
				String ric;
				String price;
				String updated;
				// 对结果进行输出
				while (rs.next()) {
					ric = rs.getString(1);
					price = rs.getString(2);
					updated = rs.getString(3);
					System.out.println("ric:" + ric + ";-- price:" + price
							+ "; --" + updated + "; ");
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.out.println("close ");
		}
	}

	public static void main(String args[])// 自己替换[]
	{
		new JDBCoracle10G_INVOKEPROCEDURE();
	}
}

7.开发JAVA调用函数返回结果集

JDBCoracle10G_INVOKEFUNCTION.java

 

  1. import java.sql.*; 
  2. import oracle.jdbc.OracleCallableStatement; 
  3. import oracle.jdbc.OracleTypes; 
  4.  
  5. /*
  6. /* 本例是通过调用oracle的函数来返回结果集:
  7. * oracle 9i、10G 的jdbc由1个jar包组成:classes12.zip 
  8. */ 
  9. publicclass JDBCoracle10G_INVOKEFUNCTION { 
  10.     Connection conn = null
  11.     Statement statement = null
  12.     ResultSet rs = null
  13.     CallableStatement stmt = null
  14.  
  15.     String driver; 
  16.     String url; 
  17.     String user; 
  18.     String pwd; 
  19.     String sql; 
  20.     String in_price; 
  21.  
  22.     public JDBCoracle10G_INVOKEFUNCTION() 
  23.     { 
  24.         driver = "oracle.jdbc.driver.OracleDriver"
  25.         url = "jdbc:oracle:thin:@localhost:1521:ORCL"
  26.         // oracle 用户 
  27.         user = "test"
  28.         // oracle 密码 
  29.         pwd = "test"
  30.         init(); 
  31.         // mysid:必须为要连接机器的sid名称,否则会包以下错: 
  32.         // java.sql.SQLException: Io 异常: Connection 
  33.         // refused(DESCRIPTION=(TMP=)(VSNNUM=169870080)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4)))) 
  34.         // 参考连接方式: 
  35.         // Class.forName( "oracle.jdbc.driver.OracleDriver" ); 
  36.         // cn = DriverManager.getConnection( 
  37.         // "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd ); 
  38.     } 
  39.  
  40.     publicvoid init() { 
  41.         System.out.println("oracle jdbc test"); 
  42.         try
  43.             Class.forName(driver); 
  44.             System.out.println("driver is ok"); 
  45.             conn = DriverManager.getConnection(url, user, pwd); 
  46.             System.out.println("conection is ok"); 
  47.             statement = conn.createStatement(); 
  48.             // conn.setAutoCommit(false); 
  49.             // 输入参数 
  50.             in_price = "5.0"
  51.             // 调用函数 
  52.             stmt = conn.prepareCall("{? = call F_GET_PRICE(?)}"); 
  53.             // stmt.registerOutParameter(1, java.sql.Types.FLOAT); 
  54.             // stmt.registerOutParameter(2, java.sql.Types.CHAR); 
  55.             stmt.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR); 
  56.             stmt.setString(2, in_price); 
  57.             stmt.executeUpdate(); 
  58.             // 取的结果集的方式一: 
  59.             rs = ((OracleCallableStatement) stmt).getCursor(1); 
  60.             // 取的结果集的方式二: 
  61.             // rs = (ResultSet) stmt.getObject(1); 
  62.             String ric; 
  63.             String price; 
  64.             String updated; 
  65.  
  66.             while (rs.next()) { 
  67.                 ric = rs.getString(1); 
  68.                 price = rs.getString(2); 
  69.                 updated = rs.getString(3); 
  70.                 System.out.println("ric:" + ric + ";-- price:" + price + "; --" 
  71.                         + updated + "; "); 
  72.             } 
  73.  
  74.         } catch (Exception e) { 
  75.             e.printStackTrace(); 
  76.         } finally
  77.             System.out.println("close "); 
  78.         } 
  79.     } 
  80.  
  81.     publicstaticvoid main(String args[])// 自己替换[] 
  82.     { 
  83.         new JDBCoracle10G_INVOKEFUNCTION(); 
  84.     } 

来自:http://blog.csdn.net/xw13106209/article/details/6905259

posted @ 2014-04-24 21:39  shuenjian901  阅读(426)  评论(0编辑  收藏  举报