zoukankan      html  css  js  c++  java
  • Java 如何调用 oracle 的存储过程

    通过命令行创建存储过程

    create or replace procedure emp_sal(eno emp.empno%type,esal out emp.sal%type)
    as
    begin
      select sal into esal from emp where empno=eno;
    end;
    

    PL/SQL 中测试存储过程

    依次打开 File -> New -> TestWindow 窗口,执行如下代码:

    -- Created on 2018/11/19 by ADMINISTRATOR 
    declare 
      -- Local variables here
      v_sal emp.sal%TYPE;
    begin
      -- Test statements here
      emp_sal(7369,v_sal);
      dbms_output.put_line(v_sal);
    end;
    

    Java 中如何调用存储过程

    导入 jdbc 连接 oracle 的架包 ojdbc7

    import java.sql.CallableStatement;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Types;
    
    public class TestJDBCProcedure {
        public static void main(String[] args) {
            Connection conn = null;
            CallableStatement cs = null;
            try {
                Class.forName("oracle.jdbc.driver.OracleDriver");
                conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "123456");
                cs = conn.prepareCall("call emp_sal(?,?)");
                cs.setInt(1, 7369);
                cs.registerOutParameter(2, Types.FLOAT);
                cs.execute();
                float sal = cs.getFloat(2);
                System.out.println("薪资:" + sal);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                if (conn != null) {
                    try {
                        cs.close();
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    

    运行结果
    薪资:800.0

  • 相关阅读:
    Tomcat支持多少并发
    Redis高可用架构—Keepalive+VIP
    MapReduce运行原理
    hihoCoder 1015 KMP算法(kmp)
    <html>
    UI--仿IOS控件之ActionSheet样式 and more..
    redis集群
    mongodb及mongoclient在win7下的编译和使用
    @property与@synthesize的差别
    传智播客c/c++公开课学习笔记--邮箱账户的破解与邮箱安全防控
  • 原文地址:https://www.cnblogs.com/hgnulb/p/9981927.html
Copyright © 2011-2022 走看看