【澳门新萄京8522】sqlserver 存款和储蓄进程重回游标的拍卖

【澳门新萄京8522】sqlserver 存款和储蓄进程重回游标的拍卖

 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

5.创制函数:

澳门新萄京8522 1View
Code

澳门新萄京8522 2

--创建函数: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;

澳门新萄京8522 3

</span>

2.插入测量试验数据:

澳门新萄京8522 4

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

五    ——1_____4: 关闭游标 

澳门新萄京8522 5

View Code

2.插入测试数据:

澳门新萄京8522 6View
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);

六   ——2: 自定义十一分

澳门新萄京8522 7

创建表:

3.一无全部二个回到游标: PKG_PUB_UTILS

澳门新萄京8522 8View
Code

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

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

代码示例:JDBCoracle10G_INVOKEFUNCTION.java

澳门新萄京8522 9

澳门新萄京8522 10

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

三     —— 1:基本数据类型

澳门新萄京8522 11


4.创建和仓库储存进度:P_GET_PRICE

澳门新萄京8522 12View
Code

澳门新萄京8522 13

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

澳门新萄京8522 14

</span>

java下促成调用oracle的存放进度和函数

在oracle下开创四个test的账户,然后按一出手续施行:

四    ——1_____ 2:采纳语句   if …then … else  语句

澳门新萄京8522 15

澳门新萄京8522 16

澳门新萄京8522 17澳门新萄京8522 18

  1 SQL> set serveroutput on
  2 SQL> declare
  3   2   age int:=55;/*定义整型变量并赋值*/
  4   3  begin
  5   4    if age >=56 then
  6   5      dbms_output.put_line('您可以申请退休了!');
  7   6    else
  8   7       dbms_output.put_line('您小于56岁,不可以申请退休了!');
  9   8    end if;
 10   9  end;
 11  10  /
 12 
 13 您小于56岁,不可以申请退休了!
 14 
 15 PL/SQL procedure successfully completed
 16 

View Code

澳门新萄京8522 19


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

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

澳门新萄京8522 20

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

三     —— 1_____3:日期类型

澳门新萄京8522 21

 使用存款和储蓄进程重临的游标:

6.JAVA调用存款和储蓄进程重临结果集

代码示例:JDBCoracle10G_INVOKEPROCEDURE.java

澳门新萄京8522 22View
Code

澳门新萄京8522 23

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

澳门新萄京8522 24

4.开立和累积进程:P_GET_PRICE

澳门新萄京8522 25

澳门新萄京8522 26

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

澳门新萄京8522 27

二     —— 2_____3:PL/SQL字符集

澳门新萄京8522 28

澳门新萄京8522 29澳门新萄京8522 30

1.创建表:STOCK_PRICES

澳门新萄京8522 31View
Code

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

6.JAVA调用存款和储蓄过程再次来到结果集

代码示例:JDBCoracle10G_INVOKEPROCEDURE.java

澳门新萄京8522 32

澳门新萄京8522 33

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

澳门新萄京8522 34

Pl/SQL 编程

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

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

代码示例:JDBCoracle10G_INVOKEFUNCTION.java

澳门新萄京8522 35View
Code

澳门新萄京8522 36

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

澳门新萄京8522 37

 

 

 

5.创建函数:

澳门新萄京8522 38View
Code

五    —1:突显游标 

澳门新萄京8522 39

 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

1.创建表:STOCK_PRICES

澳门新萄京8522 40

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

三     —— 2_____3:   %rowtype 类型

澳门新萄京8522 41

澳门新萄京8522 42

澳门新萄京8522 43澳门新萄京8522 44

  1 SQL> set serveroutput  on
  2 SQL> declare
  3   2       rowVar_emp emp%rowtype;/*定义能够储存emp表中一行数据的变量 rowVar_emp*/
  4   3  begin
  5   4    select * into rowVar_emp from emp where empno=7839 ;/*检索数据*/
  6   5      dbms_output.put_line( '雇员'||rowVar_emp.ename||'的职位是'||rowVar_emp.job||'、工资是'||rowVar_emp.sal);
  7   6
  8   7  end;
  9   8  /
 10 
 11 雇员KING的职位是PRESIDENT、工资是5000
 12 
 13 PL/SQL procedure successfully completed
 14 
 15 SQL>

View Code

澳门新萄京8522 45

澳门新萄京8522 46

创制再次来到游标的积累进程:

四    ——1_____3:选用语句   if …then … elseif  语句

澳门新萄京8522 47

澳门新萄京8522 48

澳门新萄京8522 49澳门新萄京8522 50

  1 SQL> set serveroutput on
  2 SQL> declare
  3   2   month int :=10; /*声明整型变量并赋值*/
  4   3
  5   4  begin
  6   5     if month >=0 and month<=3 then /*判断春节*/
  7   6      dbms_output.put_line('这是春季');
  8   7     elsif month >=4 and month <=6 then /*片段夏季*/
  9   8      dbms_output.put_line('这是夏季');
 10   9     elsif month >=7 and month<=9 then
 11  10      dbms_output.put_line('这是秋季');
 12  11     elsif month >=10 and month<=12 then
 13  12      dbms_output.put_line('这是冬季');
 14  13     else
 15  14      dbms_output.put_line('对不起,月份不合法!');
 16  15     end if;
 17  16  end;
 18  17  /
 19 
 20 这是冬季
 21 
 22 PL/SQL procedure successfully completed
 23 
 24 SQL>

View Code

澳门新萄京8522 51

澳门新萄京8522 52澳门新萄京8522 53

二     ——  1: Pl/Sql块结构

澳门新萄京8522 54

  1 【declare】
  2 --声明部分,可选
  3 begin
  4 --执行部分,必须
  5 [exception]
  6 --异常处理部分,可选
  7 end

澳门新萄京8522 55澳门新萄京8522 56

澳门新萄京8522 57澳门新萄京8522 58

  1 SQL> set serveroutput on;
  2 SQL>
  3 SQL> declare
  4   2  a int:=10;
  5   3  b int:=200;
  6   4  c number;
  7   5  begin
  8   6    c:=(a+b)/(a-b);
  9   7    dbms_output.put_line(c);
 10   8  exception
 11   9     when zero_divide then
 12  10       dbms_output.put_line('除数不许为零');
 13  11   end;
 14  12  /
 15 
 16 -1.10526315789473684210526315789473684211
 17 
 18 PL/SQL procedure successfully completed
 19 
 20 SQL>

View Code

澳门新萄京8522 59

澳门新萄京8522 60

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图