zoukankan      html  css  js  c++  java
  • Java程序调用Oracle存储过程和存储函数

    oracle不同数据库版本所对应的驱动 jar 包
      oracle10g  ojdbc14.jar
      oracle11g  ojdbc6.jar

    1.导坐标:

    <dependency>
                <groupId>com.oracle</groupId>
                <artifactId>ojdbc14</artifactId>
                <version>10.2.0.4.0</version>
                <scope>runtime</scope>
    </dependency>

    2.测试当前环境:

    public class OracleDemo {
    
        public static void main(String[] args) throws Exception {
            // 加载驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            // 获取连接
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "zhangsan", "aaa");
            // 得到预编译对象
            PreparedStatement pstm = conn.prepareStatement("select * from emp where empno = ?");
            pstm.setObject(1, 7788);
            // 执行 SQL 语句
            ResultSet rs = pstm.executeQuery();
            // 输出结果
            while (rs.next()) {
                System.out.println(rs.getString("ename"));
            }
            // 释放资源
            rs.close();
            pstm.close();
            conn.close();
        }
    }

    3.java调用存储过程

    /**
         * java调用存储过程
         */
        @Test
        public void javaCallProcedure() throws Exception {
            // 加载驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            // 获取连接
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "zhangsan", "aaa");
            // 得到预编译对象
            // CallableStatement:用于执行存储过程和存储函数的接口
            // {?= call <procedure-name>[(<arg1>,<arg2>, ...)]}   调用存储函数使用
            // {call <procedure-name>[(<arg1>,<arg2>, ...)]}   调用存储过程使用
            CallableStatement cstm = conn.prepareCall("{call p_yearsal(?, ?)}"); // {call <procedure-name>[(<arg1>,<arg2>, ...)]}
            cstm.setObject(1, 7788);
            cstm.registerOutParameter(2, OracleTypes.NUMBER);
            // 执行 SQL 语句
            cstm.execute();
            // 输出结果[第二个参数]
            System.out.println(cstm.getObject(2));
            // 释放资源
            cstm.close();
            conn.close();
        }

    4.java调用存储函数

    /**
         * java调用存储函数
         */
        @Test
        public void javaCallFunction() throws Exception {
            // 加载驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            // 获取连接
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "zhangsan", "aaa");
            // 得到预编译对象
            // CallableStatement:用于执行存储过程和存储函数的接口
            // {?= call <procedure-name>[(<arg1>,<arg2>, ...)]}   调用存储函数使用
            // {call <procedure-name>[(<arg1>,<arg2>, ...)]}   调用存储过程使用
            CallableStatement cstm = conn.prepareCall("{? = call f_yearsal(?)}"); // {?= call <procedure-name>[(<arg1>,<arg2>, ...)]}
            cstm.setObject(2, 7788);
            cstm.registerOutParameter(1, OracleTypes.NUMBER);
            // 执行 SQL 语句
            cstm.execute();
            // 输出结果[第一个参数]
            System.out.println(cstm.getObject(1));
            // 释放资源
            cstm.close();
            conn.close();
        }
  • 相关阅读:
    git 命令速查及使用
    Centos6.5 LAMP环境源码包安装与配置,附安装包百度网盘地址 (转做笔记)
    不再为Apache进程淤积、耗尽内存而困扰((转))
    centos6.5 安装linux 环境
    window 配置wnmp(转下整理 ,全)
    mac下安装 xampp 无法启动apache (转,留用)
    Git命令行(转用于学习和记录)
    apache 局域网访问
    华为云GaussDB(for opengauss)如何绑定公网,实现putty的远程访问gaussdb数据库。
    Day9 打卡acwing.429 奖学金
  • 原文地址:https://www.cnblogs.com/roadlandscape/p/12345953.html
Copyright © 2011-2022 走看看