仓库储存进度再次来到游标的管理,java下促成调

2019-10-05 20:36 来源:未知

创建表:

在oracle下开创五个test的账户,然后按一下步骤推行:

图片 1图片 2

1.创建表:STOCK_PRICES

图片 3View Code

--创建表格CREATETABLE STOCK_PRICES(    RIC VARCHAR(6) PRIMARYKEY,    PRICE NUMBER(7,2),    UPDATED DATE );

java下促成调用oracle的囤积进度和函数

在oracle下创办一个test的账户,然后按一出手续实行:

1 1 create table tb1(
2 2 
3 3 id int ,
4 4 
5 5 name nvarchar(20)
6 6 
7 7 )

2.插入测验数据:

图片 4View Code

--插入数据INSERTINTO stock_prices values('1111',1.0,SYSDATE);INSERTINTO stock_prices values('1112',2.0,SYSDATE);INSERTINTO stock_prices values('1113',3.0,SYSDATE);INSERTINTO stock_prices values('1114',4.0,SYSDATE);

1.创建表:STOCK_PRICES

图片 5

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

View Code

3.创建八个回来游标: PKG_PUB_UTILS

图片 6View Code

--建立一个返回游标CREATEORREPLACE PACKAGE PKG_PUB_UTILS IS--动态游标    TYPE REFCURSOR IS REF CURSOR;END PKG_PUB_UTILS;

2.插入测量试验数据:

图片 7

--插入数据
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);

4.创造和存款和储蓄进度:P_GET_PRICE

图片 8View Code

图片 9

--创建存储过程CREATEORREPLACEPROCEDURE 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 INNUMBER) ISBEGIN    AN_O_RET_CODE :=0;    AC_O_RET_MSG  :='操作成功';        OPEN CUR_RET FORSELECT*FROM STOCK_PRICES WHERE PRICE< span>AN_I_PRICE;EXCEPTION    WHEN OTHERS THEN        AN_O_RET_CODE :=-1;        AC_O_RET_MSG  :='错误代码:'|| SQLCODE || CHR(13) ||'错误信息:'|| SQLERRM;END P_GET_PRICE;

图片 10

</span>

3.起家一个赶回游标: PKG_PUB_UTILS

图片 11

--建立一个返回游标
CREATE OR REPLACE PACKAGE PKG_PUB_UTILS IS
    --动态游标
    TYPE REFCURSOR IS REF CURSOR;
END PKG_PUB_UTILS;

始建重临游标的积存进程:

5.创设函数:

图片 12View Code

图片 13

--创建函数:F_GET_PRICECREATEORREPLACEFUNCTION F_GET_PRICE(v_price INNUMBER)    RETURN PKG_PUB_UTILS.REFCURSORAS    stock_cursor PKG_PUB_UTILS.REFCURSOR;BEGINOPEN stock_cursor FORSELECT*FROM stock_prices WHERE price < span> v_price;    RETURN stock_cursor;END;

图片 14

</span>

4.开立和存款和储蓄进度:P_GET_PRICE

图片 15

图片 16

--创建存储过程
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;

图片 17

图片 18图片 19

6.JAVA调用存款和储蓄进度重回结果集

代码示例:JDBCoracle10G_INVOKEPROCEDURE.java

图片 20View Code

图片 21

import java.sql.*;import oracle.jdbc.OracleCallableStatement;import oracle.jdbc.OracleTypes;/* 本例是通过调用oracle的存储过程来返回结果集: * oracle 9i、10G 的jdbc由1个jar包组成:classes12.zip */publicclass 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 );    }    publicvoid 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 ");        }    }    publicstaticvoid main(String args[])// 自己替换[]    {        new JDBCoracle10G_INVOKEPROCEDURE();    }}

图片 22

5.创立函数:

图片 23View Code

 1 create proc tb1_proc (
 2 
 3 @cur cursor varying output
 4 
 5 )
 6 
 7 as
 8 
 9 begin
10 
11   set @cur=cursor for
12 
13   select * from tb1
14 
15 end
16 
17 open @cur

7.开辟JAVA调用函数重返结果集

代码示例:JDBCoracle10G_INVOKEFUNCTION.java

图片 24View Code

图片 25

import java.sql.*;import oracle.jdbc.OracleCallableStatement;import oracle.jdbc.OracleTypes;/* /* 本例是通过调用oracle的函数来返回结果集: * oracle 9i、10G 的jdbc由1个jar包组成:classes12.zip   */publicclass JDBCoracle10G_INVOKEFUNCTION {    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_INVOKEFUNCTION()    {        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 );    }    publicvoid 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 = "5.0";            // 调用函数            stmt = conn.prepareCall("{? = call F_GET_PRICE(?)}");            // stmt.registerOutParameter(1, java.sql.Types.FLOAT);            // stmt.registerOutParameter(2, java.sql.Types.CHAR);            stmt.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);            stmt.setString(2, in_price);            stmt.executeUpdate();            // 取的结果集的方式一:            rs = ((OracleCallableStatement) stmt).getCursor(1);            // 取的结果集的方式二:            // rs = (ResultSet) stmt.getObject(1);            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 ");        }    }    publicstaticvoid main(String args[])// 自己替换[]    {        new JDBCoracle10G_INVOKEFUNCTION();    }}

图片 26

 

 

 

6.JAVA调用存储过程再次回到结果集

代码示例:JDBCoracle10G_INVOKEPROCEDURE.java

图片 27

图片 28

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();
    }
}

图片 29

View Code

7.开辟JAVA调用函数再次来到结果集

代码示例:JDBCoracle10G_INVOKEFUNCTION.java

图片 30

图片 31

import java.sql.*;
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleTypes;

/*
 /* 本例是通过调用oracle的函数来返回结果集:
 * oracle 9i、10G 的jdbc由1个jar包组成:classes12.zip  
 */
public class JDBCoracle10G_INVOKEFUNCTION {
    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_INVOKEFUNCTION()
    {
        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 = "5.0";
            // 调用函数
            stmt = conn.prepareCall("{? = call F_GET_PRICE(?)}");
            // stmt.registerOutParameter(1, java.sql.Types.FLOAT);
            // stmt.registerOutParameter(2, java.sql.Types.CHAR);
            stmt.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
            stmt.setString(2, in_price);
            stmt.executeUpdate();
            // 取的结果集的方式一:
            rs = ((OracleCallableStatement) stmt).getCursor(1);
            // 取的结果集的方式二:
            // rs = (ResultSet) stmt.getObject(1);
            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_INVOKEFUNCTION();
    }
}

 使用存款和储蓄进度再次回到的游标:

图片 32图片 33

 1 declare @my_cur cursor
 2 
 3 declare @id int, @name nvarchar(20)
 4 
 5 exec tb1_proc @my_cur output
 6 
 7 --open @cursor    -- @cursor already opened
 8 
 9 fetch next from @my_cur into @id, @name
10 
11 while(@@fetch_status=0)
12 
13   begin
14 
15     print '编号:'   convert(nvarchar,@id)
16 
17     print '姓名:'   @name
18 
19     print '......................'
20 
21     fetch next from @my_cur into @id, @name
22 
23   end

View Code

 注意:存款和储蓄进程中开创游标后要开辟

TAG标签: 韦德娱乐1946
版权声明:本文由韦德娱乐1946_韦德娱乐1946网页版|韦德国际1946官网发布于韦德国际1946官网,转载请注明出处:仓库储存进度再次来到游标的管理,java下促成调